Results 1 to 6 of 6

Thread: List the entity of a project

  1. #1
    Join Date
    Jul 2010
    Posts
    6

    Default 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 at 11:39 PM. Reason: precise my goal

  2. #2
    Join Date
    Jul 2010
    Posts
    6

    Default

    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?

  3. #3
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    668

    Lightbulb

    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 at 01:06 AM. Reason: Added Hibernate info.
    Andrew Swan
    "Now is the EJB of our discontent made glorious Spring"

  4. #4
    Join Date
    Jul 2010
    Posts
    6

    Default

    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());
                }

  5. #5
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    668

    Question

    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().
    Andrew Swan
    "Now is the EJB of our discontent made glorious Spring"

  6. #6
    Join Date
    Jul 2010
    Posts
    6

    Default

    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>

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •