jpa - Hibernate. Associate two tables with one by one key column -
i have legacy database 3 tables: entity1, entity2, property. each entity have several properties. both entity tables bounded property-table entity_id key located in property table. haven't real constraints.
db scheme:
i prefer store properties in map in entity1-class must have many-to-one assotiation in property-class (i haven't founded other solutions. are exist?).
so how can associate entity2-class property-class?
enity1.class:
@entity @table(name = "entity1") public class entity1 { @id @column(name = "entity_id") private long id; @column(name = "some_field") private string field; @onetomany(mappedby = "entityid") @mapkey(name="key") private map<long, property> properties; }
property.class
@entity @table(name = "property") public class property { @id @column(name = "property_id") private long id; @column private long key; @column private string value; //are there other solutions? @manytoone @joincolumn(name = "entity_id", referencedcolumnname = "entity_id") private entity1 entity1; }
entity2.class skeleton
@entity @table(name = "entity1") public class entity2 { @id @column(name = "entity_id") private long id; @column(name = "some_field") private string field; // can do? private map<long, property> properties; }
can't remove @manutoone annoutation property.class couse of next exception:
11:38:40,825 error [org.jboss.msc.service.fail] (serverservice thread pool -- 72) msc000001: failed start service jboss.persistenceunit."sudir-gateway-ear-0.0.1-snapshot.ear#primary": org.jboss.msc.service.startexception in service jboss.persistenceunit."sudir-gateway-ear-0.0.1-snapshot.ear#primary": java.lang.nullpointerexception @ org.jboss.as.jpa.service.persistenceunitserviceimpl$1$1.run(persistenceunitserviceimpl.java:172) [wildfly-jpa-8.2.0.final.jar:8.2.0.final] @ org.jboss.as.jpa.service.persistenceunitserviceimpl$1$1.run(persistenceunitserviceimpl.java:117) [wildfly-jpa-8.2.0.final.jar:8.2.0.final] @ java.security.accesscontroller.doprivileged(native method) [rt.jar:1.7.0_51] @ org.wildfly.security.manager.wildflysecuritymanager.dochecked(wildflysecuritymanager.java:474) @ org.jboss.as.jpa.service.persistenceunitserviceimpl$1.run(persistenceunitserviceimpl.java:182) [wildfly-jpa-8.2.0.final.jar:8.2.0.final] @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1145) [rt.jar:1.7.0_51] @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:615) [rt.jar:1.7.0_51] @ java.lang.thread.run(thread.java:744) [rt.jar:1.7.0_51] @ org.jboss.threads.jbossthread.run(jbossthread.java:122) caused by: java.lang.nullpointerexception @ org.hibernate.cfg.annotations.collectionbinder.bindcollectionsecondpass(collectionbinder.java:1460) [hibernate-core-4.3.7.final.jar:4.3.7.final] @ org.hibernate.cfg.annotations.collectionbinder.bindonetomanysecondpass(collectionbinder.java:864) [hibernate-core-4.3.7.final.jar:4.3.7.final] @ org.hibernate.cfg.annotations.collectionbinder.bindstartomanysecondpass(collectionbinder.java:779) [hibernate-core-4.3.7.final.jar:4.3.7.final] @ org.hibernate.cfg.annotations.mapbinder$1.secondpass(mapbinder.java:107) [hibernate-core-4.3.7.final.jar:4.3.7.final] @ org.hibernate.cfg.collectionsecondpass.dosecondpass(collectionsecondpass.java:70) [hibernate-core-4.3.7.final.jar:4.3.7.final] @ org.hibernate.cfg.configuration.originalsecondpasscompile(configuration.java:1697) [hibernate-core-4.3.7.final.jar:4.3.7.final] @ org.hibernate.cfg.configuration.secondpasscompile(configuration.java:1426) [hibernate-core-4.3.7.final.jar:4.3.7.final] @ org.hibernate.cfg.configuration.buildsessionfactory(configuration.java:1846) [hibernate-core-4.3.7.final.jar:4.3.7.final] @ org.hibernate.jpa.boot.internal.entitymanagerfactorybuilderimpl$4.perform(entitymanagerfactorybuilderimpl.java:852) [hibernate-entitymanager-4.3.7.final.jar:4.3.7.final] @ org.hibernate.jpa.boot.internal.entitymanagerfactorybuilderimpl$4.perform(entitymanagerfactorybuilderimpl.java:845) [hibernate-entitymanager-4.3.7.final.jar:4.3.7.final] @ org.hibernate.boot.registry.classloading.internal.classloaderserviceimpl.withtccl(classloaderserviceimpl.java:398) [hibernate-core-4.3.7.final.jar:4.3.7.final] @ org.hibernate.jpa.boot.internal.entitymanagerfactorybuilderimpl.build(entitymanagerfactorybuilderimpl.java:844) [hibernate-entitymanager-4.3.7.final.jar:4.3.7.final] @ org.jboss.as.jpa.hibernate4.twophasebootstrapimpl.build(twophasebootstrapimpl.java:44) [jipijapa-hibernate4-3-1.0.1.final.jar:] @ org.jboss.as.jpa.service.persistenceunitserviceimpl$1$1.run(persistenceunitserviceimpl.java:154) [wildfly-jpa-8.2.0.final.jar:8.2.0.final] ... 8 more
you don't need manytoone. need specify joincolumn annotation on onetomany association:
@onetomany @joincolumn(name = "entity_id") private map<long, property> properties;
afaik, doing same thing on second entity should work fine. guess you're aware design fall apart enity1 has same id entity2.
Comments
Post a Comment