Eclipse + JBoss 5 + EJB3 Development Guide (10)

Source: Internet
Author: User
Tags inheritance jboss

Map individual tables to multiple tables by inheriting entity beans (single table policy, single_table)

If you have previously used an entity bean with ejb1.x or ejb2.x, you will find that you cannot divide a single table into multiple tables by inheriting entity beans. And in EJB3, we can easily implement this function. Let's take a look at the table structure and records shown in Figure 1.

Fig. 1 structure and record of t_accounts table

In the T_accounts table, there is a account_type field. This field is a string type with length 1. Can only take two values: C and S. If the field value is C, the current account (Checkingaccount) is represented, and if the field value is S, the savings Account (SavingsAccount) is represented. The first three fields of the T_accounts table (account_id, balance, and Account_type) are required for both a current account and a savings account, and interestrate is only meaningful for a savings account, Overdraftlimit only has meaning for a current account. Therefore, we can divide the t_accounts table into two tables, when the value of Account_type is C and S is a table.

If you are using EJB3 entity beans, you can first write an account class to encapsulate the first three fields of T_accounts, as follows:

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; }

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.