温馨提示:本文翻译自stackoverflow.com,查看原文请点击:java - Map one entity to different columns of one table using Hibernate

java - 使用Hibernate将一个实体映射到一个表的不同列

发布于 2020-03-27 11:00:09

我有一个简单的银行应用程序,其中有“帐户”和“交易”实体。一个帐户保留提款并存入交易(它们可以在两个帐户之间,也可以在account&ATM之间(在相应的字段中为null)。我使用@ManyToOne和@OneToMany批注,并希望将这两个字段保存在交易表id-s中fromAccount和toAccount但我遇到了一个异常,您可以在下面看到,我理解它是由于映射到同一字段(account_id)而发生的,但是如果我应该使用id字段,该如何解决呢?

public class Account {
@OneToMany(fetch = FetchType.LAZY,
        mappedBy = "fromAcc",
        cascade = CascadeType.ALL)
private List<Transaction> withdrawTransactions = new ArrayList<>();

@OneToMany(fetch = FetchType.LAZY,
        mappedBy = "toAcc",
        cascade = CascadeType.ALL)
private List<Transaction> depositTransactions = new ArrayList<>();

public class Transaction {
@ManyToOne
@JoinColumn(name = "account_id")
private Account fromAcc;
@ManyToOne
@JoinColumn(name = "account_id")
private Account toAcc;

我得到的例外:

 Exception in thread "main" javax.persistence.PersistenceException: [PersistenceUnit: BankAppSimple] Unable to build Hibernate SessionFactory
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:1012)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:938)
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:56)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at bank.app.simple.util.JpaUtil.getEntMngFactoryInstance(JpaUtil.java:18)
at bank.app.simple.util.JpaUtil.createEntityManager(JpaUtil.java:30)
at bank.app.simple.daoimpl.ExchangeRateDaoImpl.isCurrencyExist(ExchangeRateDaoImpl.java:19)
at bank.app.simple.serviceimpl.ExchangeRateServiceImpl.addRate(ExchangeRateServiceImpl.java:19)
at bank.app.simple.Main.fillTables(Main.java:46)
at bank.app.simple.Main.main(Main.java:26)

Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: bank.app.simple.entity.Transaction column: account_id (should be mapped with insert="false" update="false")
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:862)
at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:880)
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:902)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:634)
at org.hibernate.mapping.RootClass.validate(RootClass.java:267)
at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:343)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:461)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:935)
... 9 more

查看更多

查看更多

提问者
Reni21
被浏览
59
t4dohx 2019-07-03 21:12

我认为您忘记在Transaction实体中设置连接列只需用事务表中的FROM_ACC_COLUMN替换TO_ACC_COLUMN为相应的列名即可。

public class Transaction {

    @ManyToOne
    @JoinColumn(name = "FROM_ACC_COLUMN", referencedColumnName = "account_id")
    private Account fromAcc;

    @ManyToOne
    @JoinColumn(name = "TO_ACC_COLUMN", referencedColumnName = "account_id")
    private Account toAcc;

}

就我个人CascadeType.ALL而言,在这种情况下我不会去尝试{CascadeType.PERSIST, CascadeType.MERGE}

发布
问题

分享
好友

手机
浏览

扫码手机浏览