Ejb table sharding Query

Source: Internet
Author: User

If you have used EJB1.x or EJB2.x Entity Bean before, you will find that you cannot divide a single table into multiple tables by inheriting the Entity Bean. In EJB3, we can easily implement this function. First look at the table structure and records shown in Figure 1. Figure 1 Structure and record of the t_accounts table

In the t_accounts table, there is an account_type field. This field is a String type field with a length of 1. Only two values are allowed: C and S. If the field value is C, it indicates the current account (CheckingAccount). If the field value is S, it indicates the savings account (SavingsAccount ). The first three fields (account_id, balance, and account_type) in the t_accounts table are required by both the current account and the savings account, while interestrate is only meaningful to the savings account, overdraftlimit is valid only for current accounts. Therefore, we can divide the t_accounts table into two tables. When the value of account_type is C and S, each table is one.

If you use the EJB3 Entity Bean, you can write an Account class to encapsulate the first three fields of t_accounts. The Code is 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;
}
}

Note the following two notes for the Account class code:

1. @ inheritance

2. @ discriminatorcolumn

@ Inheritance annotation is used to set the inheritance type of the object bean. The default value is inheritancetype. single_table, that is, the single table policy type. If this inheritance type is used, every table inherited from this entity bean will be mapped into a sub-table. This sub-table needs to be mapped based on the value of a validation field. In this example, the field is account_type, which is specified by @ discriminatorcolumn annotation. Note that the account_type field is now set as an authentication field. Therefore, the value of this field cannot be dynamically specified by developers through code, the sub-classes of the account class must be specified through annotations (which will be described in detail later). Therefore, you need to use the @ column annotation to set the object bean attribute corresponding to this field to non-plug-ins and editable (insertable = false,
Updatable = false ). Otherwise, the following exception will be thrown during program running:

Org. hibernate. MappingException: Repeatedcolumn in mapping for entity: entity. SavingsAccount column: account_type (shocould be mapped with insert = "false" update = "false ")

The actual Bean code of the current account is as follows:

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

}

In the CheckingAccount class, use the @ DiscriminatorValue annotation to set the value of the account_type field to C. If the CheckingAccount class is used to map the t_accounts table, the EJB container automatically sets the account_type field value of the t_accounts table to C (no developer intervention is required ).

The code for the Entity Bean corresponding to the savings and deposit account is as follows:

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

The following code can be used for testing:

CheckingAccount ca = new CheckingAccount ();
Ca. setBalance (342 );
Ca. setOverdraftLimit (120 );
Em. persist (ca); // automatically sets the value of the account_type field to C
SavingsAccount sa = new SavingsAccount ();
Sa. setBalance (200 );
Sa. setInterestRate (321 );
Em. persist (sa); // automatically sets the value of the account_type field to S

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.