我有3个表的遗留数据库:ENTITY1,ENTITY2,PROPERTY。每个实体都有几个属性。两个实体表都通过entity_id键与属性表绑定,该键位于属性表中。所以我没有真正的限制。
DB方案:
我更喜欢将所有属性存储在Entity1-class中的Map中,因此我必须在Property-class中进行多对一的关联(我还没有建立其他解决方案。它们是否存在?)。
那么如何将Entity2-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;
// What can I do?
private Map<Long, Property> properties;
}
无法从下一个例外的Property.class中删除@ManuToOne annoutation:
11:38:40,825 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 72) MSC000001: Failed to 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
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:172) [wildfly-jpa-8.2.0.Final.jar:8.2.0.Final]
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:117) [wildfly-jpa-8.2.0.Final.jar:8.2.0.Final]
at java.security.AccessController.doPrivileged(Native Method) [rt.jar:1.7.0_51]
at org.wildfly.security.manager.WildFlySecurityManager.doChecked(WildFlySecurityManager.java:474)
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1.run(PersistenceUnitServiceImpl.java:182) [wildfly-jpa-8.2.0.Final.jar:8.2.0.Final]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_51]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_51]
at java.lang.Thread.run(Thread.java:744) [rt.jar:1.7.0_51]
at org.jboss.threads.JBossThread.run(JBossThread.java:122)
Caused by: java.lang.NullPointerException
at org.hibernate.cfg.annotations.CollectionBinder.bindCollectionSecondPass(CollectionBinder.java:1460) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
at org.hibernate.cfg.annotations.CollectionBinder.bindOneToManySecondPass(CollectionBinder.java:864) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:779) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
at org.hibernate.cfg.annotations.MapBinder$1.secondPass(MapBinder.java:107) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:70) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1697) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1426) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:852) [hibernate-entitymanager-4.3.7.Final.jar:4.3.7.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:845) [hibernate-entitymanager-4.3.7.Final.jar:4.3.7.Final]
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:398) [hibernate-core-4.3.7.Final.jar:4.3.7.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:844) [hibernate-entitymanager-4.3.7.Final.jar:4.3.7.Final]
at org.jboss.as.jpa.hibernate4.TwoPhaseBootstrapImpl.build(TwoPhaseBootstrapImpl.java:44) [jipijapa-hibernate4-3-1.0.1.Final.jar:]
at 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
答案 0 :(得分:0)
您不需要ManyToOne。您只需要在OneToMany关联上指定JoinColumn批注:
@OneToMany
@JoinColumn(name = "entity_id")
private Map<Long, Property> properties;
AFAIK,在第二个实体上做同样的事情应该可以正常工作。我想你知道,只要Enity1与Entity2具有相同的ID,这个设计就会崩溃。