[jboss-user] [EJB 3.0] - Re: Accessing EntityManagerFactory/SessionFactory via JNDI s

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


Hi Fiorenzo,

You can get a list of entity classes from an EntityManager. Then add code to scan the @NamedQuery and @NamedQueries({ ... }) annotations on those classes and make a list of NamedQuery names.

Like this:


  | // Get all entity classes
  | org.hibernate.SessionFactory sf = ((org.jboss.ejb3.entity.HibernateSession) em).getHibernateSession().getSessionFactory();
  | Map<?,?> allClassMetadata = sf.getAllClassMetadata();
  | List<String> queries = new ArrayList<String>();
  | for (Object value: allClassMetadata.values()) {
  |     EntityPersister ep = (EntityPersister) value;
  |     Class<?> entityClass = ep.getClassMetadata().getMappedClass(EntityMode.POJO);
  |     NamedQuery nqAnno = entityClass.getAnnotation(NamedQuery.class);
  |     if(nqAnno != null)
  |         queries.add(nqAnno.name());
  |     NamedQueries nqsAnno = entityClass.getAnnotation(NamedQueries.class);
  |     if(nqsAnno != null) {
  |         for(NamedQuery q: nqsAnno.value())
  |             queries.add(q.name());
  |     }
  | }
  | 

I haven't tested this code.

Good luck.

Regards,
Matt

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

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



More information about the jboss-user mailing list