在t_checkingaccount和t_savingsaccount表中都有一個(gè)account_id,這個(gè)account_id的值依賴(lài)于t_myaccounts表中的account_id。
下面先來(lái)編寫(xiě)與t_myaccounts對(duì)應(yīng)的實(shí)體Bean,代碼如下:
package entity;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;
@Entity
@Table(name="t_myaccounts")
@Inheritance(strategy=InheritanceType.JOINED)
public class Account
{
protected String id;
protected float balance;
protected String type;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="account_id")
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public float getBalance()
{
return balance;
}
public void setBalance(float balance)
{
this.balance = balance;
}
@Column(name="account_type")
public String getType()
{
return type;
}
public void setType(String type)
{
this.type = type;
}
}
從上面的代碼可以看出,只使用了@Inheritance對(duì)實(shí)體Bean進(jìn)行注釋。
下面編寫(xiě)MyCheckingAccount和MySavingsAccount類(lèi)的代碼:
MyCheckingAccount類(lèi)的代碼:
package entity;
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
@Entity
@Table(name="t_checkingaccount")
// 指定與Account類(lèi)共享的主鍵名
@PrimaryKeyJoinColumn(name="account_id")
public class MyCheckingAccount extends Account
{
private double overdraftLimit;
public MyCheckingAccount()
{
// 為account_type字段賦默認(rèn)值
setType("C");
}
@Column(name="overdraft_limit")
public double getOverdraftLimit()
{
return overdraftLimit;
}
public void setOverdraftLimit(double overdraftLimit)
{
this.overdraftLimit = overdraftLimit;
}
}
MySavingsAccount類(lèi)的代碼:
package entity;
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
@Entity
@Table(name="t_savingsaccount")
@PrimaryKeyJoinColumn(name="account_id")
public class MySavingsAccount extends Account
{
private double interestRate;
public MySavingsAccount()
{
// 為account_type字段賦默認(rèn)值
setType("S");
}
@Column(name="interest_rate")
public double getInterestRate()
{
return interestRate;
}
public void setInterestRate(double interestRate)
{
this.interestRate = interestRate;
}
}
在上面的代碼中使用構(gòu)造方法來(lái)初始化了t_myaccounts表的account_type字段的值。
可以使用下面的代碼進(jìn)行測(cè)試:
System.out.println(((MyCheckingAccount)em.createQuery("from MyCheckingAccount where id=12")
.getSingleResult()).getBalance());
MyCheckingAccount ca = new MyCheckingAccount();
ca.setBalance(342);
ca.setOverdraftLimit(120);
em.persist(ca);
MySavingsAccount sa = new MySavingsAccount();
sa.setBalance(200);
sa.setInterestRate(321);
em.persist(sa);