上一篇: eclipse + JBoss 5 + EJB3開發(fā)指南(10):通過繼承實體Bean,將單個表映射成多個表(單表策略,SINGLE_TABLE)
在上一篇文章中,使用單表策略將一個表從邏輯上分成了多個表。但這樣可能會造成空巢字段,也就是說,一個邏輯表只由部分字段組成,而物理的表的很多字段的值就會為 null 。為了解決這個問題,可以將 t_accounts 表物理地分成多個表。為了與 t_accounts 表進行對比,新建一個 t_myaccounts 表,結(jié)構(gòu)如圖 1 所示。


圖2 t_checkingaccount表

圖3 t_savingsaccount表
下面先來編寫與t_myaccounts對應(yīng)的實體Bean,代碼如下:
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 Stringid;
protected float balance;
protected Stringtype;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = " account_id " )
public StringgetId()
{
return id;
}
public void setId(Stringid)
{
this .id = id;
}
public float getBalance()
{
return balance;
}
public void setBalance( float balance)
{
this .balance = balance;
}
@Column(name = " account_type " )
public StringgetType()
{
return type;
}
public void setType(Stringtype)
{
this .type = type;
}
}
從上面的代碼可以看出,只使用了
@Inheritance
對實體
Bean
進行注釋。
下面編寫
MyCheckingAccount
和
MySavingsAccount
類的代碼:
MyCheckingAccount 類的代碼:
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類共享的主鍵名
@PrimaryKeyJoinColumn(name = " account_id " )
public class MyCheckingAccount extends Account
{
private double overdraftLimit;
public MyCheckingAccount()
{
// 為account_type字段賦默認值
setType( " C " );
}
@Column(name = " overdraft_limit " )
public double getOverdraftLimit()
{
return overdraftLimit;
}
public void setOverdraftLimit( double overdraftLimit)
{
this .overdraftLimit = overdraftLimit;
}
}
MySavingsAccount類的代碼:
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字段賦默認值
setType( " S " );
}
@Column(name = " interest_rate " )
public double getInterestRate()
{
return interestRate;
}
public void setInterestRate( double interestRate)
{
this .interestRate = interestRate;
}
}
在上面的代碼中使用構(gòu)造方法來初始化了
t_myaccounts
表的
account_type
字段的值。
可以使用下面的代碼進行測試:
.getSingleResult()).getBalance());
MyCheckingAccountca = new MyCheckingAccount();
ca.setBalance( 342 );
ca.setOverdraftLimit( 120 );
em.persist(ca);
MySavingsAccountsa = new MySavingsAccount();
sa.setBalance( 200 );
sa.setInterestRate( 321 );
em.persist(sa);
下一篇: eclipse + JBoss 5 + EJB3開發(fā)指南(12):使用命名查詢執(zhí)行JPQL
國內(nèi)最棒的Google Android技術(shù)社區(qū)(eoeandroid),歡迎訪問!
《銀河系列原創(chuàng)教程》 發(fā)布
《Java Web開發(fā)速學(xué)寶典》 出版,歡迎定購
eclipse + JBoss 5 + EJB3開發(fā)指南(11):實體Bean的連接策略(JOINED Strategy)
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
