Announcement Announcement Module
Collapse
No announcement yet.
List the entity of a project Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • List the entity of a project

    Hi,
    I'm writing an add-on to monitor entities of a roo project.

    To do this, I'd like to be able to list all the entity class and be able to execute query on them suing the persistence API.
    My code would live in a controller (at least at the beginning). Is there an API to do this?

    Thanks,
    Jeremi
    Last edited by jeremi; Jul 5th, 2010, 11:39 PM. Reason: precise my goal

  • #2
    Using the petstore demo, I'm doing :

    Code:
                resp.put("Owner", Owner.countOwners());
                resp.put("Pet", Pet.countPets());
                resp.put("Vet", Vet.countVets());
                resp.put("Visit", Visit.countVisits());
            }
    but I would like to be able to do something more like:

    Code:
            entities = magicFunctionToFind();
            for (Object entity : entities) {
                resp.put(entity.getClass().getName(), entity.count...());        
            }
    I need to find the magicFunctionToFind to give me the list of entities in this project. Does this function exist?

    Comment


    • #3
      Good question; it got me digging. If you're using JPA 2, you should be able to find out the managed entities via the Metamodel API.

      I'm still trying to figure out if it's possible with JPA 1. It might depend on what JPA provider you are using. Maybe there's a vendor specific API that you can access by casting the injected JPA EntityManager to its vendor-specific implementation.

      Latest update: if you're using a recent enough version of Hibernate and can get hold of the org.hibernate.ejb.EntityManagerFactoryImpl (maybe by casting an injected javax.persistence.EntityManagerFactory?), you can call getSessionFactory().getAllClassMetadata() to find out the entity classes.

      Please let us know if this works.
      Last edited by Andrew Swan; Jul 7th, 2010, 01:06 AM. Reason: Added Hibernate info.

      Comment


      • #4
        Great Thanks!

        If anyone is interested by the solution, add to the class:

        Code:
           @PersistenceContext
            EntityManager entityManager;
        And you can do:
        Code:
                     Metamodel metamodel = entityManager.getMetamodel();
        
                    for (EntityType entityType : metamodel.getEntities()) {
                        System.out.println("-------->>>>" + entityType.getName());
                    }

        Comment


        • #5
          Nice. So what version of Roo and JPA are you using? In Roo 1.0.2 with JPA declared thus:

          Code:
          <dependency>
              <groupId>javax.persistence</groupId>
              <artifactId>com.springsource.javax.persistence</artifactId>
              <version>1.0.0</version>
          </dependency>
          ... there's no method called EntityManager#getMetamodel().

          Comment


          • #6
            I like to live on the edge I'm using roo 1.1.0.M1.

            I think
            Code:
            com.springsource.javax.persistence
            has been replaced by
            Code:
            org.hibernate.javax.persistence
            Code:
                <dependency>
                        <groupId>org.hibernate.javax.persistence</groupId>
                        <artifactId>hibernate-jpa-2.0-api</artifactId>
                        <version>1.0.0.Final</version>
                    </dependency>
                <dependency>

            Comment

            Working...
            X