我有两个给定的表(posgres),其中包含以下字段(所有大的整数):
交易:
冲销:
交易和逆转之间存在可选的1:1关系:交易总是存在;如果交易已被撤销,则存在逆转。该关系是使用反转中的复合外键(reader_id,trxref)完成的。这些表是这样给出的,我需要在JPA中映射它们。我需要在Transaction中映射Reversal,我可以在命名查询中导航,即SELECT FROM Transaction t LEFT JOIN t.reversal rv ....
所以我所做的是:
@Entity
@Table(
indexes = {
@Index(columnList = "readerTrxRef,reader_id", unique = true),
}
)
public class Transaction
{
static final Long serialVersionUID = 1L;
@Id
@GeneratedValue
private Long transactionid;
private Long reader_id;
private Long readertrxref
@OneToOne(fetch = FetchType.EAGER, mappedBy = "transaction")
private Reversal reversal;
...
}
@Entity
@Table(
indexes = {
@Index(columnList = "readerID,trxRef", unique = true),
}
)
public class Reversal
{
static final Long serialVersionUID = 1L;
@Id
@GeneratedValue
private Long id;
private Integer trxref;
private Long readerid;
public Integer getTrxRef()
{
return trxRef;
}
@OneToOne
@JoinColumns(
{
@JoinColumn(updatable = false, insertable = false, name = "readerid", referencedColumnName = "reader_id"),
@JoinColumn(updatable = false, insertable = false, name = "trxref", referencedColumnName = "readertrxref"),
}
)
private Transaction transaction;
在部署期间,我收到以下异常:
14:24:12,455 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 24) MSC000001: Failed to start service jboss.persistenceunit."ROOT.war#primary": org.jboss.msc.service.StartException in service jboss.persistenceunit."ROOT.war#primary": javax.persistence.PersistenceException: [PersistenceUnit: primary] Unable to build Hibernate SessionFactory
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:179)
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:121)
at org.wildfly.security.manager.WildFlySecurityManager.doChecked(WildFlySecurityManager.java:667)
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1.run(PersistenceUnitServiceImpl.java:193)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
at org.jboss.threads.JBossThread.run(JBossThread.java:320)
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: primary] Unable to build Hibernate SessionFactory
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:954)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:882)
at org.jboss.as.jpa.hibernate5.TwoPhaseBootstrapImpl.build(TwoPhaseBootstrapImpl.java:44)
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:161)
... 7 more
Caused by: org.hibernate.MappingException: broken column mapping for: reversal.transaction of: ch.microtronic.evending.web.model.entities.Transaction
at org.hibernate.persister.entity.AbstractPropertyMapping.initPropertyPaths(AbstractPropertyMapping.java:165)
at org.hibernate.persister.entity.AbstractPropertyMapping.initIdentifierPropertyPaths(AbstractPropertyMapping.java:253)
at org.hibernate.persister.entity.AbstractPropertyMapping.initPropertyPaths(AbstractPropertyMapping.java:219)
at org.hibernate.persister.entity.AbstractEntityPersister.initOrdinaryPropertyPaths(AbstractEntityPersister.java:2194)
at org.hibernate.persister.entity.AbstractEntityPersister.initPropertyPaths(AbstractEntityPersister.java:2241)
at org.hibernate.persister.entity.AbstractEntityPersister.postConstruct(AbstractEntityPersister.java:3790)
at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:437)
at sun.reflect.GeneratedConstructorAccessor62.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:96)
at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:77)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:346)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:879)
... 9 more
有人能指出我的错吗?我只是尝试删除@JoinColumns
并只有一个@JoinColumn
。在这种情况下,我可以部署应用程序 - 这告诉我这是一个奇怪的错误,我不是很惊讶,如果它是一个hibernate的错误,即使我认为这是一个不是非常奇特的用例。 (我们在Wildfly 10.1.0中使用Hibernate 5.2.12。)
编辑澄清映射是事务(reader_id,readertrxref) - &gt; reversal(readerid,trxref)并且这是以这种方式给出的。我需要以这种方式映射它,并且我不能使用transactionid作为反转中的外键,因为它(由于技术原因)在此表中不存在。
答案 0 :(得分:0)
使用主键表是联合的。 在您的情况下,它将是以下:
@OneToOne
@JoinColumn(name = "transactionid")
private Transaction transaction;
@JoinColumns
注释用于复合主键。
我建议阅读wikibooks。
答案 1 :(得分:0)
无法解决这个问题。正如Tom和DN1所指出的,JoinColumns必须在PK上。 因为它们不是我用例中的PK而且我不能使它们成为复合PK(因为关系Transaction-&gt; Reversal是可选的,因此,我仍然需要一个真正的PK即transactionID),似乎不可能用JPA来解决这个问题。