I am learning JPA and I have 2 entities (Customer and CheckingAccount) where the first entity has a one to many relationship with the 2nd entity. CheckingAccount subclasses a BankAccount class. I am using MySQL.
I create 2 checking accounts in a test program and assign them both to one customer that I have created.
After I persist everything and check my database I expected to see 2 rows in the CheckingAccount table which I do and 2 rows in the Customer table indicating this customer has 2 checking accounts but I only see one row and the value of the Accounts column is "blob" where I was expecting to see an account number.
Shouldn't I see 2 rows in my Customer table to indicate that the customer has 2 checking accounts?
Here is my Customer entity...
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;
}
}
Here is my CheckingAccount entity...
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();
}
}
Here is my BankAccount superclass of CheckingAccount...
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;
}
}
When you use "one to many" with 2 checking accounts and one customer, you have the following structure in your database (only an arbitrary example - I am using json format just to explain better but imagine its attributes as columns):
Customer = [{id: 1, name: Mr. Anderson}]
CheckingAccount = [{id:1, value: $100, id_customer: 1}, {id: 2, value: $250, id_customer: 1}]
You don't need to have two rows in customer table. When you have one-to-many relationship, you have the foreign key in the many-relationship-side table.
Got it.. does it make sense?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.