【发布时间】:2014-11-18 17:37:22
【问题描述】:
我正在学习 JPA,我有 2 个实体(客户和 CheckingAccount),其中第一个实体与第二个实体具有一对多关系。 CheckingAccount 是 BankAccount 类的子类。我正在使用 MySQL。
我在一个测试程序中创建了 2 个支票账户,并将它们都分配给我创建的一个客户。
在我保留所有内容并检查我的数据库后,我希望在我所做的 CheckingAccount 表中看到 2 行,在 Customer 表中看到 2 行,表明该客户有 2 个支票账户,但我只看到一行和 Accounts 的值列是“blob” 我希望看到一个帐号。
我不应该在我的客户表中看到 2 行来表明客户有 2 个支票账户吗?
这是我的客户实体...
package com.gmail.gmjord;
import java.io.Serializable;
import java.lang.String;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.*;
/**
* Entity implementation class for Entity: Customer
*
*/
@Entity
public class Customer implements Serializable {
@Id
@GeneratedValue
private String id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private List<BankAccount> accounts;
@Column(nullable = false)
private String infoId;
@Column(nullable = false)
private String pin;
private static final long serialVersionUID = 1L;
public Customer() {
super();
accounts = new ArrayList<BankAccount>();
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int getAccountsLength() {
return this.accounts.size();
}
public boolean isAccountsNull() {
if (accounts == null) {
return true;
} else {
return false;
}
}
public List<BankAccount> getAccounts() {
return accounts;
}
public void setAccounts(List<BankAccount> accounts) {
this.accounts = accounts;
}
public String getInfoId() {
return this.infoId;
}
public void setInfoId(String infoId) {
this.infoId = infoId;
}
public String getPin() {
return this.pin;
}
public void setPin(String pin) {
this.pin = pin;
}
}
这是我的 CheckingAccount 实体...
package com.gmail.gmjord;
import com.gmail.gmjord.BankAccount;
import java.io.Serializable;
import javax.persistence.*;
/**
* Entity implementation class for Entity: CheckingAccount
*
*/
@Entity
public class CheckingAccount extends BankAccount implements Serializable {
private static final long serialVersionUID = 1L;
public CheckingAccount() {
super();
}
}
这是我的 CheckingAccount 的 BankAccount 超类...
package com.gmail.gmjord;
import java.io.Serializable;
import java.lang.String;
import javax.persistence.*;
/**
* Entity implementation class for Entity: BankAccount
*
*/
@MappedSuperclass
public abstract class BankAccount implements Serializable {
@Id
@Column(nullable=false)
private String accountNum;
@Column(nullable=false)
private String balance;
@Column(nullable=false)
private String accountType;
private static final long serialVersionUID = 1L;
public BankAccount() {
super();
}
public String getAccountNum() {
return this.accountNum;
}
public void setAccountNum(String accountNum) {
this.accountNum = accountNum;
}
public String getBalance() {
return this.balance;
}
public void setBalance(String balance) {
this.balance = balance;
}
public String getAccountType() {
return this.accountType;
}
public void setAccountType(String accountType) {
this.accountType = accountType;
}
}
【问题讨论】:
-
两张支票和一位顾客看起来很一对多。
-
声明 OneToMany 的注解是 ... OneToMany。不是列。如果你想要一个 CheckingAccount 列表,你应该有一个 List
类型的字段,而不是 List 类型的字段。