Tuesday, September 14, 2010

Eclipse + JBoss + EJB3 Entity Bean's connection strategy



In the previous article, the use of single-table strategy will be a table logically divided into multiple tables. However, this field may result in empty nest, that is, a logical table composed only of part of the field, while the physical form of the many field value will to null. To solve this problem, you can t_accounts table physically divided into multiple tables. In order to compare with the t_accounts table, create a new t_myaccounts table, the structure shown in Figure 1.






Figure 1 t_myaccounts table

From t_myaccounts structure can be seen in the table only contains the first three fields t_accounts table, then logically assigned to two different tables, so, we must first establish two physical forms: t_checkingaccount and t_savingsaccount. The two tables is structured as follows:






Figure 2 t_checkingaccount table






Figure 3 t_savingsaccount table

In t_checkingaccount and t_savingsaccount table has a account_id, the account_id value depends on the t_myaccounts table account_id.

Following the first to write and t_myaccounts corresponding entity Bean, 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_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;))
As can be seen from the above code, only use the @ Inheritance annotation on the entity Bean to.

Following the preparation of MyCheckingAccount and MySavingsAccount class code:

MyCheckingAccount class code:
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") / / specify the Account class to share the primary key name @ PrimaryKeyJoinColumn (name = "account_id") public class MyCheckingAccount extends Account (private double overdraftLimit; public MyCheckingAccount () (/ / default to account_type field Fu value setType ("C");) @ Column (name = "overdraft_limit") public double getOverdraftLimit () (return overdraftLimit;) public void setOverdraftLimit (double overdraftLimit) (this.overdraftLimit = overdraftLimit;))
MySavingsAccount class code:
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 () (/ / default value assigned to account_type field setType ("S");) @Column(name="interest_rate") public double getInterestRate() { return interestRate; } public void setInterestRate(double interestRate) { this.interestRate = interestRate; } }
鍦ㄤ笂闈㈢殑浠g爜涓娇鐢ㄦ瀯閫犳柟娉曟潵鍒濆鍖栦簡t_myaccounts琛ㄧ殑account_type瀛楁鐨勫?銆?br />
鍙互浣跨敤涓嬮潰鐨勪唬鐮佽繘琛屾祴璇曪細
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);






相关链接:



realplayer flv



WINDOWS media player psp



ts FORMAT



Rmvb Quicktime



No comments:

Post a Comment