cschneider / Karaf-Tutorial

http://cschneider.github.io/Karaf-Tutorial/
Apache License 2.0
273 stars 303 forks source link

CriteriaQuery: not an entity class in db-examplejpa #46

Open guillaumefgn opened 6 years ago

guillaumefgn commented 6 years ago

Hello @cschneider ,

I tried your project db-examplejpa which works fine (I have just link this example with a Oracle DB instead of H2 DB). I can add, list and delete persons.

For the "list" action, I tried to rewrite the query to use the Criteria API as follow:

    public List<Person> getAll() {

        final CriteriaBuilder builder = em.getCriteriaBuilder();

        final CriteriaQuery<Person> criteria = builder.createQuery(Person.class);
        final Root<Person> personRoot = criteria.from(Person.class);
        criteria.select(personRoot);

        final TypedQuery<Person> query = em.createQuery(criteria);
        final List<Person> people = query.getResultList();

        return people;
        // return em.createQuery("select p from Person p",
        // Person.class).getResultList();
    }

But I have the following error:

karaf@root>person:list Error executing command: Not an entity: class net.lr.tutorial.karaf.db.examplejpa.Person

Do you have an idea why?

I am using Karaf 4.0.9.

Thanks a lot.

cschneider commented 6 years ago

Could be a problem with the annotation classes not being available or being different. Make sure the respective packages are imported, not embedded into your bundles.

guillaumefgn commented 6 years ago

What do you mean by "annotation classes not being available or being different"? I didn't change anything in your example except the getAll() mehod.

I have no jar embedded into my bundle (I didn't change the maven-bundle-plugin configuration).

The package for Criteria API seems to be loaded:

karaf@root>bundle:requirements 295 db-examplejpa [295] requires:

osgi.wiring.package; (&(osgi.wiring.package=org.apache.felix.service.command)(version>=0.6.0)(!(version>=1.0.0))) resolved by: osgi.wiring.package; org.apache.felix.service.command 0.16.2 from org.apache.karaf.shell.core [124] osgi.wiring.package; (&(osgi.wiring.package=org.apache.felix.gogo.commands)(version>=0.6.0)(!(version>=1.0.0))) resolved by: osgi.wiring.package; org.apache.felix.gogo.commands 0.16.2 from org.apache.karaf.shell.core [124] osgi.wiring.package; (&(osgi.wiring.package=javax.persistence)(version>=2.1.0)(!(version>=3.0.0))) resolved by: osgi.wiring.package; javax.persistence 2.1.0 from javax.persistence [254] osgi.wiring.package; (&(osgi.wiring.package=javax.persistence.criteria)(version>=2.1.0)(!(version>=3.0.0))) resolved by: osgi.wiring.package; javax.persistence.criteria 2.1.0 from javax.persistence [254] osgi.wiring.package; (&(osgi.wiring.package=org.osgi.service.blueprint)(version>=1.0.0)(!(version>=2.0.0))) resolved by: osgi.wiring.package; org.osgi.service.blueprint 1.0.0 from org.apache.aries.blueprint.core [28] osgi.wiring.package; (&(osgi.wiring.package=org.apache.karaf.shell.console)(version>=2.2.0)(!(version>=4.1.0))) resolved by: osgi.wiring.package; org.apache.karaf.shell.console 4.0.9 from org.apache.karaf.shell.core [124] osgi.wiring.package; (osgi.wiring.package=org.hibernate.proxy) resolved by: osgi.wiring.package; org.hibernate.proxy 4.3.6.Final from org.hibernate.core [258] osgi.wiring.package; (osgi.wiring.package=javassist.util.proxy) resolved by: osgi.wiring.package; javassist.util.proxy 3.18.1.GA from javassist [253] osgi.ee; (&(osgi.ee=JavaSE)(version=1.7)) resolved by: osgi.ee; JavaSE [1.0.0, 1.1.0, 1.2.0, 1.3.0, 1.4.0, 1.5.0, 1.6.0, 1.7.0, 1.8.0] from org.apache.felix.framework [0] service; [javax.naming.spi.InitialContextFactoryBuilder] provided by: org.apache.aries.jndi.core [237] service; [javax.naming.spi.InitialContextFactoryBuilder] provided by: org.apache.aries.jndi.legacy.support [238] service; [javax.naming.spi.InitialContextFactory] provided by: org.apache.karaf.jndi.core [241] service; [javax.naming.spi.ObjectFactory] provided by: org.apache.aries.jndi.url [240] service; [javax.sql.DataSource] provided by: org.ops4j.pax.jdbc.config [245] service; [javax.persistence.EntityManagerFactory] provided by: db-examplejpa [295] service; [javax.persistence.EntityManagerFactory, org.apache.aries.jpa.container.sync.Synchronization] provided by: db-examplejpa [295]

guillaumefgn commented 6 years ago

Hi,

I have checked the dependencies of my bundle but I don't see anything wrong :-(

cschneider commented 6 years ago

The package imports look correct indeed. Maybe it is a hibernate issue. You could ask on the hibernate user list. It seems like hibernate criteria query support does not see the persistence annotations on Person. This can happen if it uses a different classloader for these packages.