我正在学习 JPA,我有 2 个实体(Customer 和 CheckingAccount),其中第一个实体与第二个实体具有一对多关系。 CheckingAccount 是 BankAccount 类的子类。我正在使用 MySQL。
我在测试程序中创建了 2 个支票帐户,并将它们都分配给我创建的一名客户。
在我坚持所有内容并检查数据库后,我希望在 CheckingAccount 表中看到 2 行(我这样做),在 Customer 表中看到 2 行,表明该客户有 2 个支票帐户,但我只看到一行和帐户的值列是“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 父类(super class)...
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;
}
}
最佳答案
当您对 2 个支票帐户和一个客户使用“一对多”时,您的数据库中具有以下结构(只是一个任意示例 - 我使用 json 格式只是为了更好地解释,但将其属性想象为列):
客户 = [{id: 1, name: 安德森先生}]
CheckingAccount = [{id:1, value: $100, id_customer: 1}, {id:2, value: $250, id_customer: 1}]
客户表中不需要有两行。当您具有一对多关系时,您在多关系侧表中拥有外键。
明白了..这有意义吗?
关于java - JPA - 为什么我的一对多关系没有出现在数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27000486/