Eclipse+JBoss+EJB3实体Bean的连接策略

开发 后端
本文讲述Eclipse+JBoss+EJB3实体Bean的连接策略。一个逻辑表只由部分字段组成,而物理的表的很多字段的值就会为null。

在上一篇文章中,使用单表策略将一个表从逻辑上分成了多个表。但这样可能会造成空巢字段,也就是说,一个逻辑表只由部分字段组成,而物理的表的很多字段的值就会为null。为了解决这个问题,可以将t_accounts表物理地分成多个表。为了与t_accounts表进行对比,新建一个t_myaccounts表,结构如图1所示。

t_myaccounts表

图1  t_myaccounts表

从t_myaccounts的结构可以看出,在该表中只包含了t_accounts表的前三个字段,而后两个在逻辑上分到了不同的表,因此,首先要建立两个物理表:t_checkingaccount和t_savingsaccount。这两个表的结构如下:

t_checkingaccount表

图2  t_checkingaccount表

t_savingsaccount表

图3  t_savingsaccount表

在t_checkingaccount和t_savingsaccount表中都有一个account_id,这个account_id的值依赖于t_myaccounts表中的account_id。

下面先来编写与t_myaccounts对应的实体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;  
    }  
}  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.

从上面的代码可以看出,只使用了@Inheritance对实体Bean进行注释。

下面编写MyCheckingAccount和MySavingsAccount类的代码:

MyCheckingAccount类的代码:

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;  
    }  
}  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.

MySavingsAccount类的代码:

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;  
    }      
}  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.

在上面的代码中使用构造方法来初始化了t_myaccounts表的account_type字段的值。  

可以使用下面的代码进行测试:

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); 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

【编辑推荐】

  1. Eclipse+JBoss+EJB3通过继承实体Bean将单个表映射成多个表
  2. Eclipse+JBoss+EJB3实现Entity Bean的多对多映射
  3. Eclipse+JBoss+EJB3实现Entity Bean的一对多映射
  4. Eclipse+JBoss+EJB3实现Entity Bean的一对一映射
  5. Eclipse+JBoss+EJB3编写第一个实体Bean程序
责任编辑:book05 来源: BlogJava
相关推荐

2009-06-24 15:59:04

消息驱动Bean

2009-06-10 11:42:26

Session BeaEclipse+JBo

2009-06-24 15:47:13

实体Bean

2009-06-10 12:34:01

Session BeaEclipse+JBo

2009-06-10 11:09:40

配置文件SessionEclipse+JBo

2009-06-10 11:36:45

有状态的SessionEclipse+JBo

2009-06-24 15:53:08

Entity Bean多对多映射

2009-06-24 15:51:47

Entity Bean一对多映射

2009-06-24 15:55:09

EclipseJBossJ2ee

2009-06-24 15:49:54

Entity Bean一对一映射

2009-06-24 15:57:44

JPQL命名查询

2009-06-24 15:58:15

EntityManag

2009-06-24 16:00:00

2009-06-10 12:54:35

无状态的SessionEclipse+JBo

2009-06-15 16:06:25

JBoss IDE

2009-09-24 12:05:35

2009-06-17 08:51:26

Eclipse启动JB

2009-06-26 15:36:27

Session BeaEJB

2009-06-17 09:01:20

JBoss访问EJB

2009-06-16 15:15:18

WebLogic EJ
点赞
收藏

51CTO技术栈公众号