我有一个简单的银行应用程序,其中有“帐户”和“交易”实体。一个帐户保留提款和存款交易(它们可以在两个帐户之间或account&ATM之间(在相应字段中为空)。 我使用@ManyToOne和@OneToMany批注,并希望将字段fromAccount和toAccount都保存在事务表ID-s中,但出现异常,您可以在下面看到。我了解发生这种情况是因为在同一字段(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
答案 0 :(得分:1)
我认为您忘记在Category
实体中设置联接列。
只需将RSpec::Matchers.define :be_successful do
match do |result|
result.status == :success &&
result.offers.length > 0 &&
result.comments.empty?
end
failure_message do |result|
"Should have #{result} equal to be successful"
end
failure_message_when_negated do |result|
"Should not have #{result} to be successful"
end
end
和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;
}
。
答案 1 :(得分:0)
如果有人感兴趣,对我有用的解决方案是使用@ManyToOne批注重命名实体中的映射字段,并在“ account_id” +“ referencedColumnName =“ account_id””之前添加前缀:
@ManyToOne
@JoinColumn(name = "from_account_id", referencedColumnName = "account_id") // add from_
private Account fromAcc;
@ManyToOne
@JoinColumn(name = "to_account_id", referencedColumnName = "account_id") // add to_
private Account toAcc;
@ t4dohx,谢谢您的指教!