In EJB3.0, entity beans can achieve inheritance relationships.
For example, there is a Person's entity bean, which has both name and gender attribute.
When God and the female are generated, two people have been made: man and woman. Man and Woman are entity beans, and they all inherit Person.
Single table strategy is that the data of the sub-body and sub-entities are stored in a table, and the specified list is used to distinguish these entities.
Such as:
@Entity
@Inheritance (strategy = inheritancetype.single_table, discriminatortype = discriminatortype.string)
@DiscriminatorColumn (name = "p_type", nullable = true)
@ Inheritance annotation declarations as follows:
@ @Target ({TYPE}) @Retention (RUNTIME) public @interface Inheritance {InheritanceType strategy () default SINGLE_TABLE; DiscriminatorType discriminatorType () default STRING; String discriminatorValue () default "";}
This annotation is used to specify the policy of inheritance, and the type and value of the columns used to distinguish these entities.
@DiscriminatorColumn Note is used on a single table policy and a joint table policy. It is used to specify the columns needed to distinguish the entities.
@Target ({TYPE}) @Retention (RUNTIME) public @interface DiscriminatorColumn {String name () default ""; boolean nullable () default false; String columnDefinition () default ""; int length () default 10;}
This example mainly has the following documents, this example mainly implements the inheritance relationship between Person and Man, Woman, and the examples introduced below and this example is the same. MAN and Woman inherit the Person entity bean. As before, we still use the Client test.
Person.java: entity bean.
Man.java: Class depends on entity bean.
Woman.java: Class depends on entity bean.
Persontest.java: Business Interface for Session Bean
PersontestBean.java: Realization of Session Bean
Client.java: Test EJB client class.
JNDI.Properties :jndi property file provides basic configuration properties for accessing JDNI.
Build.xml: Ant profile, to compile, release, test, and clear EJB.
Here is a description of the content of each file.
Person.java
package com.kuaff.ejb3.singleinheritance; import javax.ejb.DiscriminatorColumn; import javax.ejb.DiscriminatorType; import javax.ejb.Entity; import javax.ejb.GeneratorType; import javax.ejb.Id; import javax.ejb.Inheritance ; import javax.ejb.InheritanceType; @Entity @Inheritance (strategy = InheritanceType.SINGLE_TABLE, discriminatorType = DiscriminatorType.STRING) @DiscriminatorColumn (name = "P_TYPE", nullable = true) public class Person implements java.io.Serializable {private int ID; private string name; private string gender; @ID (generate = generatortype.auto) public int getId () {return id;} public void setid (int id) {this.id = id;} public string getName () { Return name;} public void setName (String name) {this.name = name;} public void setgender (String gender) {this.gender = gender;} public string getgen () {return gender;} } Specifies that the P_TYPE column is used to distinguish the entity bean.
Man.java
package com.kuaff.ejb3.singleinheritance; import javax.ejb.DiscriminatorType; import javax.ejb.Entity; import javax.ejb.Inheritance; import javax.ejb.InheritanceType; @Entity @Inheritance (strategy = InheritanceType.SINGLE_TABLE, discriminatorType = DiscriminatorType.STRING, discriminatorValue = "Man") public class Man extends Person {private boolean isGood; public void setGood (boolean isGood) {this.isGood = isGood;} public boolean isGood () {return isGood;}}
This entity bean adds a good man's property.
Woman.java
package com.kuaff.ejb3.singleinheritance; import javax.ejb.DiscriminatorType; import javax.ejb.Entity; import javax.ejb.Inheritance; import javax.ejb.InheritanceType; @Entity @Inheritance (strategy = InheritanceType.SINGLE_TABLE, discriminatorType = DiscriminatorType.STRING, discriminatorValue = "Woman") public class Woman extends Person {private boolean isbeautiful; public void setIsbeautiful (boolean isbeautiful) {this.isbeautiful = isbeautiful;} public boolean isIsbeautiful () {return isbeautiful;}} EntityTest.java
package com.kuaff.ejb3.singleinheritance; import javax.ejb.Remote; import java.util.List; @Remote public interface PersonDAO {public int createMan (String name, String gender, boolean b); public int createWoman (String name, String gender, Boolean B); Public Person Find (INT I); public list FindByname; public list findbyinfo (String gender);
PersontestBean.java
package com.kuaff.ejb3.singleinheritance; import javax.ejb.EntityManager; import javax.ejb.Inject; import javax.ejb.Stateless; import java.util.List; @Stateless public class PersonDAOBean implements PersonDAO {@Inject private EntityManager manager Public int Createman (String Name, String Gender, Boolean B) {man man = new man (); man.setname (name); man.setGooder (GENDER); Manager.create (Man) Return man.getid (); public int CREATEWOMAN (String Name, String Gender, Boolean B) {Woman Woman = New Woman (); Woman.setName (Name); Woman.SetGender (GENDER); Woman.setisbeautiful (B ); Manager.create (Woman); returnous.GetId ();} public person Find (Person Manager.find (Person.class, i);} public list FindbyName (String name) {Return Manager.createQuery ("from person p.name =: name"). setpa Rameter ("name", name) .listresults ();} public list findbyInfo (String Gender) {Return Manager.createQuery ("from person p where p.gender =: gender"). SetParameter ("gender", gender). ListResults ();}} The method of creating a Man, Woman entity bean is provided in this session bean, and provides a lookup method.
Client.java
package com.kuaff.ejb3.singleinheritance; import javax.naming.InitialContext; import javax.naming.NamingException; import java.util.List; public class Client {public static void main (String [] args) throws NamingException {InitialContext ctx = NEW INITIALCONTEXT (); Persondao Dao = (Persondao) CTX.lookup (Persondao.Class.GetName ()); Int i = Dao.Createman ("晁 晁 攀", "male", true); Dao.Createwoman ("Zhu Lihuan", "Female", true); Person P = DAO.FIND (I); System.out.Printf ("% s gender:% s% n", p.GetName (), p.getGender ()); list List = DAO.FindByname ("Zhu Li Huan"); for (Object o: list) {Woman W = (Woman) O; system.out.printf ("% s beautiful? Conclusion:% b% N", w.Getname ), w.isisbeautiful ());}}} This client is tested.
Run {$ jboss_home} / bin's run.bat: run -c all, start JBoss.
HTTP: // localhost: 8080 / jmx-console / htmladaptor? action = inspectMben & name = jboss% 3AService% 3DHYPERSONIC% 2CDATABASE% 3DLOCALDB, then call the StartDatabaseManager () method to open the HSQL Management Tools Management Database.
Execute the EJBJAR TARGET in the ANT view of Eclipse. Or in the command line, enter this project directory, perform Ant Ejbjar, publish this EJB.
Execute Run Target in the ANT view of Eclipse. Or on the command line, enter this project directory, perform Ant Run, test this EJB.