javamongodbhibernatejpaobject-persistence

Load Object from MongoDB with JPA (hibernate)


I created an @Entity and stored it in a MongoDB via Hibernate using the Java Peristence API (JPA) to be able to switch to another JPA implementation (like DataNucleus, EclipseLink etc.), but now I cannot load it in anymore. I get:

java.lang.NoSuchMethodError: org.hibernate.engine.internal.TwoPhaseLoad.addUninitializedEntity(Lorg/hibernate/engine/spi/EntityKey;Ljava/lang/Object;Lorg/hibernate/persister/entity/EntityPersister;Lorg/hibernate/LockMode;ZLorg/hibernate/engine/spi/SessionImplementor;)V
    at org.hibernate.ogm.loader.impl.OgmLoader.loadFromResultSet(OgmLoader.java:1143)
    at org.hibernate.ogm.loader.impl.OgmLoader.instanceNotYetLoaded(OgmLoader.java:1065)
    at org.hibernate.ogm.loader.impl.OgmLoader.getRow(OgmLoader.java:959)
    at org.hibernate.ogm.loader.impl.OgmLoader.getRowFromResultSet(OgmLoader.java:498)
    at org.hibernate.ogm.loader.impl.OgmLoader.doQuery(OgmLoader.java:373)
    at org.hibernate.ogm.loader.impl.OgmLoader.doQueryAndInitializeNonLazyCollections(OgmLoader.java:283)
    at org.hibernate.ogm.loader.impl.OgmLoader.loadEntity(OgmLoader.java:201)
    at org.hibernate.ogm.loader.impl.OgmLoader.load(OgmLoader.java:157)
    at org.hibernate.ogm.loader.impl.OgmLoader.load(OgmLoader.java:149)
    at org.hibernate.ogm.loader.entity.impl.DynamicBatchingEntityLoaderBuilder$DynamicBatchingEntityLoader.load(DynamicBatchingEntityLoaderBuilder.java:116)
    at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:4002)
    at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:508)
    at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:478)
    at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:219)
    at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:278)
    at org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:121)
    at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:89)
    at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1142)
    at org.hibernate.internal.SessionImpl.access$2600(SessionImpl.java:167)
    at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.doLoad(SessionImpl.java:2762)
    at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.load(SessionImpl.java:2741)
    at org.hibernate.internal.SessionImpl.get(SessionImpl.java:978)
    at org.hibernate.engine.spi.SessionDelegatorBaseImpl.get(SessionDelegatorBaseImpl.java:629)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:1075)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:1033)
    at org.hibernate.ogm.jpa.impl.OgmEntityManager.find(OgmEntityManager.java:97)

All classes are something similar to this or using a subset of it:

// package and imports (lombok and jpa)

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Bla {

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    @Getter
    @Setter
    private long id;

    @Getter
    @Setter
    private <T extends Object> attribute;

    @OneToMany
    @Getter
    @Setter
    private List<<T extends Object>> list;

    @ElementCollection
    @JoinTable(name="TASKS", joinColumns=@JoinColumn(name="user"))
    @Getter
    @Setter
    private Map<<T extends Object>, Boolean> doneTask;

}

And I am using entityManager.find(Class<T> type, Object o) to load the Objects. Is there anything I may have been doing wrong?


Solution

  • For using a MongoDB you need

    runtime "org.mongodb:mongo-java-driver:$versions.mongodb"
    

    and your ogm/persitence framework, in my case Hibernate, with your framework/db connection:

    compile "org.hibernate.ogm:hibernate-ogm-core:$hibernate_version"
    compile "org.hibernate:hibernate-entitymanager:$hibernate_version"
    runtime "org.hibernate.ogm:hibernate-ogm-mongodb:$hibernate_version"
    

    and your persitence api (jpa, jdo or proprietary), in my case JPA with hibernate/jpa implementation:

    compile "org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final"
    compile "$versions.persistence_api"