Java ConstraintViolationException:无法插入对象
我正在实现一个银行应用程序,数据库中有三个表(User、Account和AccountActivity):Java ConstraintViolationException:无法插入对象,java,orm,Java,Orm,我正在实现一个银行应用程序,数据库中有三个表(User、Account和AccountActivity): Account和AccountActivity类的实现如下所示: @MappedSuperclass public abstract class AbstractDomain implements Serializable { @Id @GeneratedValue private long id = NEW_ID; public static long NE
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;
}
// ...
}
以及:
它只在我的数据库中存储新帐户。我想实现这样一种功能,即当帐户活动信息添加到已保存的帐户时,
将更新该帐户,并将添加的信息(帐户活动)级联到
AccountActivity表使用以下代码:
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
private List<AccountActivity> AccountActivity = new ArrayList<AccountActivity>();
其中,fillAccounts(account,account2)
只插入一些应添加到图表中的AccountActivity:
AccountActivity aa = new AccountActivity();
aa.setDate(new Date());
aa.setName("test activity");
aa.setAccount(a1);
aa.setValue(value);
aa.setOtherAccount(a2);
account.addAccountActivity(aa)
首先,我认为变量名不应该与类名称相同。因此,您可以编写类似private List AccountActivity的内容来代替此private List AccountActivity。
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@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>();
@JoinColumn(name=“accountid”)//从AccountActivity到Account有多个引用。在这种情况下,需要指定联接列。
私有列表AccountActivity=new ArrayList();
实体类的@Id定义在哪里?我忘了提到那些@Id定义是从AbstractDomain类继承的(请参阅问题的更新版本)向我们展示了测试代码。如何测试应用程序?我认为这与Account.java中AccountActivity的配置有关。当我尝试在storeAccount中合并ac时抛出错误,这将导致帐户的merce,并应导致该AccountActivity的更新
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());
}
AccountActivity aa = new AccountActivity();
aa.setDate(new Date());
aa.setName("test activity");
aa.setAccount(a1);
aa.setValue(value);
aa.setOtherAccount(a2);
account.addAccountActivity(aa)
@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>();