日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

eclipse + JBoss 5 + EJB3開發指南(11):實體

系統 1849 0
本文為原創,如需轉載,請注明作者和出處,謝謝!

上一篇: eclipse + JBoss 5 + EJB3開發指南(10):通過繼承實體Bean,將單個表映射成多個表(單表策略,SINGLE_TABLE)

在上一篇文章中,使用單表策略將一個表從邏輯上分成了多個表。但這樣可能會造成空巢字段,也就是說,一個邏輯表只由部分字段組成,而物理的表的很多字段的值就會為 null 。為了解決這個問題,可以將 t_accounts 表物理地分成多個表。為了與 t_accounts 表進行對比,新建一個 t_myaccounts 表,結構如圖 1 所示。

圖1 t_myaccounts表

從t_myaccounts的結構可以看出,在該表中只包含了t_accounts表的前三個字段,而后兩個在邏輯上分到了不同的表,因此,首先要建立兩個物理表:t_checkingaccount和t_savingsaccount。這兩個表的結構如下:



圖2 t_checkingaccount表



圖3 t_savingsaccount表

在t_checkingaccount和t_savingsaccount表中都有一個account_id,這個account_id的值依賴于t_myaccounts表中的account_id。
下面先來編寫與t_myaccounts對應的實體Bean,代碼如下:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> 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 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 類的代碼:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> 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類共享的主鍵名
@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類的代碼:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> 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字段賦默認值
setType( " S " );
}
@Column(name
= " interest_rate " )
public double getInterestRate()
{
return interestRate;
}
public void setInterestRate( double interestRate)
{
this .interestRate = interestRate;
}
}

在上面的代碼中使用構造方法來初始化了 t_myaccounts 表的 account_type 字段的值。
可以使用下面的代碼進行測試:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> System.out.println(((MyCheckingAccount)em.createQuery( " fromMyCheckingAccountwhereid=12 " )
.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開發指南(12):使用命名查詢執行JPQL

國內最棒的Google Android技術社區(eoeandroid),歡迎訪問!

《銀河系列原創教程》 發布

《Java Web開發速學寶典》 出版,歡迎定購

eclipse + JBoss 5 + EJB3開發指南(11):實體Bean的連接策略(JOINED Strategy)


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。?!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 衡山县| 姚安县| 始兴县| 武定县| 洱源县| 邮箱| 栾川县| 四川省| 盖州市| 威远县| 西宁市| 阿尔山市| 新巴尔虎左旗| 黑河市| 铜山县| 临猗县| 合作市| 东乡族自治县| 商丘市| 皋兰县| 杭州市| 保定市| 迁安市| 北流市| 丰县| 乐业县| 盐源县| 曲周县| 上高县| 张掖市| 靖安县| 白水县| 荣成市| 资中县| 汶川县| 吴旗县| 略阳县| 渑池县| 文山县| 南华县| 萍乡市|