[jboss-user] [Persistence, JBoss/CMP, Hibernate, Database] - Re: Acces Configuration/Persistent Class at run time for rea

matt10 do-not-reply at jboss.com
Wed Oct 8 11:57:47 EDT 2008


Hi Ilavenil,

If you only have one Persistence Unit, you can get a list of entity classes this way:


  | // Get all entity classes
  | org.hibernate.SessionFactory sf = ((org.jboss.ejb3.entity.HibernateSession) em).getHibernateSession().getSessionFactory();
  | Map<?,?> allClassMetadata = sf.getAllClassMetadata();
  | for (Object value: allClassMetadata.values()) {
  |     EntityPersister ep = (EntityPersister) value;
  |     Class<?> entityClass = ep.getClassMetadata().getMappedClass(EntityMode.POJO);
  |     // Do something with the class.....
  | }
  | 

If you have more than one persistence unit, you could use
@PersistenceContext(unitName="") or similar annotations to inject an EntityManager or SessionFactory for each.

If you can't do this or don't know the persistence units at compile-time, there is another way I know of to get all Persistence Units, which involves editing persistence.xml to register hibernate SessionFactory objects in JNDI and scanning for them.

Regards,
Matt

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4181061#4181061

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4181061



More information about the jboss-user mailing list