DB: oracle 10g;
entityName:com.signaldemand.flank.hibernate.model.实体名
1. 根据实体名获取DB表相对应的表名
Class<?> clazz = CrudServiceHelper.getClassForName(entityName);
AbstractEntityPersister classMetadata = (AbstractEntityPersister)_sessionFactory.getClassMetadata(clazz);
String tableName = classMetadata.getTableName();
2.获取DB中所有的表sequence name
SessionFactory sf = HibernateUtil.getInstance().getSf();
Map mds = sf.getAllClassMetadata();
for (Object entry : mds.entrySet()) {
Entry en = (Entry) entry;
EntityPersister ep = (EntityPersister) en.getValue();
IdentifierGenerator idGen = ep.getIdentifierGenerator();
if (idGen instanceof SequenceGenerator) {
SequenceGenerator seqGen=(SequenceGenerator)idGen;
seqGen.getSequenceName();//这里就得到sequenceName了。
}
}
3. 一次性取出多个序列值(通常用在导入数据时,表如果有多条记录,先查询所要的sequence再进行设置id)
select sequenceName.nextval from (select 1 from all_objects where rownum <= 100)
标注:一个足够大的表,确保满足想获取的序列数量(表的数据量至少要有你想要的序列数量那么多)
all_objects 为系统表