损坏的列映射:具有两个连接列的 OneToOne 单向

broken column mapping: OneToOne unidirectional with two join columns

我有两个给定的 tables (posgres) 具有以下字段(所有大整数):

交易:

反转:

Transaction 和 Reversal 之间存在可选的 1:1 关系:Transaction 始终存在;如果交易已被撤销,则存在撤销。该关系是通过反转复合外键 (reader_id, trxref) 完成的。 table 以这种方式给出,我需要在 JPA 中映射它们。我需要映射交易中的逆转,我可以在命名查询中导航,即 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.run(PersistenceUnitServiceImpl.java:179)
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.run(PersistenceUnitServiceImpl.java:121)
    at org.wildfly.security.manager.WildFlySecurityManager.doChecked(WildFlySecurityManager.java:667)
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.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.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。在这种情况下,我可以部署应用程序——这表明这是一个奇怪的错误,如果它是休眠中的错误,我并不感到惊讶,尽管我认为这不是一个非常奇特的用例。 (我们在 Wildfly 10.1.0 中使用 Hibernate 5.2.12)。

编辑 阐明了映射 transaction(reader_id, readertrxref)->reversal(readerid, trxref) 和这是以这种方式给出的。我需要以这种方式映射它,我不能在逆转中使用 transactionid 作为外键,因为它确实(由于技术原因)不存在于此 table.

表是通过主键连接的。 在您的情况下,它将是以下内容:

@OneToOne
@JoinColumn(name = "transactionid")
private Transaction transaction;

@JoinColumns注释用于复合主键。

我建议阅读 wikibooks

无法解决。正如 Tom 和 DN1 所指出的,JoinColumns 必须在 PK 上。 因为在我的用例中它们不是 PK,我不能使它们成为复合 PK(因为关系 Transaction->Reversal 是可选的,因此,我仍然需要一个真正的 PK,即 transactionID),似乎不可能用 JPA 解决这个问题。