如果以前使用过
EJB1.x
或
EJB2.x
的实体
Bean
,会发现无法通过继承实体
Bean
将单个表分成多表。而在
EJB3
中,我们很容易实现这个功能。先看看图
1
所示的表结构和记录。
图1 t_accounts表的结构和记录
在t_accounts
表中,有一个account_type
字段。这个字段是一个长度为1
的String
类型字段。只能取两个值:C
和S
。如果该字段值为C
,表示活期帐户(CheckingAccount
),如果该字段值为S
,表示储蓄存款帐户(SavingsAccount
)。t_accounts
表的前三个字段(account_id
、balance
和account_type
)是活期帐户和储蓄存款帐户都需要的,而interestrate
只对储蓄存款帐户有意义,overdraftlimit
只对活期帐户有意义。因此,我们可以将t_accounts
表分成两个表,当account_type
的值为C
时和S
时各为一个表。
如果使用EJB3
的实体Bean
,可以先编写一个Account
类来封装t_accounts
的前三个字段,代码如下:
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_accounts " )
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = " account_type " )
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 " ,insertable = false , updatable = false )
public String getType()
{
return type;
}
public void setType(String type)
{
this .type = type;
}
}
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_accounts " )
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = " account_type " )
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 " ,insertable = false , updatable = false )
public String getType()
{
return type;
}
public void setType(String type)
{
this .type = type;
}
}
对于Account
类的代码,要注意如下两个注释:
1. @Inheritance
2. @DiscriminatorColumn
@Inheritance注释用于设置实体Bean的继承类型,默认值是InheritanceType.SINGLE_TABLE,也就是单表策略类型。如果使用该继承类型,每一个从该实体Bean继承的表都会被映射成一个子表。而这个子表需要根据一个鉴别字段的值来映射,在本例中该字段是account_type,这个字段由@DiscriminatorColumn注释来指定。还要注意一点的是,由于account_type字段现在被设置成了鉴别字段,因此,该字段值不能由开发人员通过代码动态指定,而必须在Account类的子类中通过注释来指定(在后面会详细介绍),因此,需要使用@Column注释将该字段对应的实体Bean属性设为不可插件和编辑的(insertable=false, updatable=false)。否则在运行程序时会抛出下面的异常:
org.hibernate.MappingException: Repeated column in mapping for entity: entity.SavingsAccount column: account_type (should be mapped with insert="false" update="false")
活期帐户的实体Bean
的代码如下:
package
entity;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
@Entity
@DiscriminatorValue( " C " )
public class CheckingAccount extends Account
{
private double overdraftLimit;
public double getOverdraftLimit()
{
return overdraftLimit;
}
public void setOverdraftLimit( double overdraftLimit)
{
this .overdraftLimit = overdraftLimit;
}
}
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
@Entity
@DiscriminatorValue( " C " )
public class CheckingAccount extends Account
{
private double overdraftLimit;
public double getOverdraftLimit()
{
return overdraftLimit;
}
public void setOverdraftLimit( double overdraftLimit)
{
this .overdraftLimit = overdraftLimit;
}
}
在CheckingAccount
类中通过@DiscriminatorValue
注释将account_type
字段的值设为了C
。如果使用CheckingAccount
类来映射t_accounts
表时,EJB
容器会自动将t_accounts
表的account_type
字段值设为C
(并不需要开发人员干预)。
储蓄存款帐户对应的实体Bean
的代码如下:
package
entity;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
@Entity
@DiscriminatorValue( " S " )
public class SavingsAccount extends Account
{
private double interestRate;
public double getInterestRate()
{
return interestRate;
}
public void setInterestRate( double interestRate)
{
this .interestRate = interestRate;
}
}
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
@Entity
@DiscriminatorValue( " S " )
public class SavingsAccount extends Account
{
private double interestRate;
public double getInterestRate()
{
return interestRate;
}
public void setInterestRate( double interestRate)
{
this .interestRate = interestRate;
}
}
可以下面的代码进行测试:
CheckingAccount ca
=
new
CheckingAccount();
ca.setBalance( 342 );
ca.setOverdraftLimit( 120 );
em.persist(ca); // 自动将account_type字段的值设为C
SavingsAccount sa = new SavingsAccount();
sa.setBalance( 200 );
sa.setInterestRate( 321 );
em.persist(sa); // 自动将account_type字段的值设为S
ca.setBalance( 342 );
ca.setOverdraftLimit( 120 );
em.persist(ca); // 自动将account_type字段的值设为C
SavingsAccount sa = new SavingsAccount();
sa.setBalance( 200 );
sa.setInterestRate( 321 );
em.persist(sa); // 自动将account_type字段的值设为S
本文转自 androidguy 51CTO博客,原文链接:
http://blog.51cto.com/androidguy/214430
,如需转载请自行联系原作者