我正在实施银行应用程序并在我的数据库中有三个表(User,Account和AccountActivity):
Account
和AccountActivity
类的实现如下所示:
@MappedSuperclass public abstract class AbstractDomain implements Serializable {
@Id @GeneratedValue
private long id = NEW_ID;
public static long NEW_ID = -1;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public boolean isNew() {
return id==NEW_ID;
}
}
@Table(name="ACCOUNT_ACTIVITY")
@Entity
public class AccountActivity extends AbstractDomain {
@Column(name="NAME")
private String Name;
@Column(name="XDATE")
private Date Date;
@Column(name="VALUE")
private double Value;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="ACCOUNTID")
private Account ACCOUNT;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="OTHERACCOUNTID")
private Account OTHERACCOUNT;
public String getName() {
return Name;
}
// ...
}
和
@Table(name="ACCOUNT")
@Entity
public class Account extends AbstractDomain {
@Column(name="NAME")
private String Name;
@Column(name="XDATE")
private Date Date;
@Column(name="VALUE")
private double Value;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="USERID")
private User USER;
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
private List<AccountActivity> AccountActivity = new ArrayList<AccountActivity>();
// ...
}
要在我的数据库中存储新帐户,我使用:
public Account storeAccount(Account ac) {
User x = ac.getUser();
x = em.merge(x);
ac = em.merge(ac);
return ac;
}
用于在我的数据库中存储新帐户。我想实现将帐户活动信息添加到已保存帐户时的功能, 该帐户将更新,并将添加的信息(帐户活动)级联到 AccountActivity表使用这段代码:
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
private List<AccountActivity> AccountActivity = new ArrayList<AccountActivity>();
当我测试此代码时,我收到错误:
java.sql.SQLException:完整性约束违规 - 无父级 FK670B7D019607336A表:声明中的ACCOUNT_ACTIVITY
有人可以帮我解决这个问题吗?
更新
我用这段junit代码测试:
public void testAddAccountActivities() {
User user = dummyPersistedUser();
User user2 = dummyPersistedUser();
Account account = getTestUtils().dummyEmptyAccount(user);
Account account2 = getTestUtils().dummyEmptyAccount(user2);
account=accountManager.storeAccount(account);
account2=accountManager.storeAccount(account2);
getTestUtils().fillAccounts(account, account2);
accountManager.storeAccount(account);
accountManager.storeAccount(account2);
assertEquals(2,accountManager.getAccount4Name(account.getName()).getAccountActivity().size());
assertEquals(2,accountManager.getAccount4Name(account2.getName()).getAccountActivity().size());
}
其中fillAccounts(account, account2)
只是插入一些应添加到图表中的AccountActivities。:
AccountActivity aa = new AccountActivity();
aa.setDate(new Date());
aa.setName("test activity");
aa.setAccount(a1);
aa.setValue(value);
aa.setOtherAccount(a2);
account.addAccountActivity(aa)
答案 0 :(得分:0)
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@JoinColumn(name ="accountid") // you have several references from AccountActivity to Account. You need to specify the join column in this case.
private List<AccountActivity> AccountActivity = new ArrayList<AccountActivity>();
答案 1 :(得分:-1)
作为第一个观察,我认为你不应该让变量名与该类相同。所以,而不是这个私人List&lt; AccountActivity&gt; AccountActivity,您可以编写类似私人列表&lt; AccountActivity&gt;的内容。 accountActivity。