Inheritance of EJB3.0 object Bean

Source: Internet
Author: User

In EJB3.0, entity beans can implement inheritance relationships. For example, a Person entity bean has two attributes: Name and gender.

When God and nvwa are created, Man and Woman are created. Both Man and Woman are entity beans, and both inherit from Person.

A single table policy means that data of sub-entities and sub-entities are stored in one table, and a column is specified to differentiate these entities.

For example:

@ Entity
@ Inheritance (strategy = InheritanceType. SINGLE_TABLE, discriminatorType = DiscriminatorType. STRING)
@ DiscriminatorColumn (name = "P_TYPE", nullable = true)
@ Inheritance:
@ 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 inheritance and use policies, and to distinguish the types and values of columns used by these entities.

@ DiscriminatorColumn comment is used in a single table policy and a Union table policy. Used to specify columns required to differentiate objects.
@ Target ({TYPE}) @ Retention (RUNTIME)

Public @ interface DiscriminatorColumn {
String name () default "";
Boolean nullable () default false;
String columnDefinition () default "";
Int length () default 10;
}

This example mainly contains the following files. This example mainly implements the inheritance relationship between Person, Man and Woman. The examples described in the following two chapters are the same as those in this example. Man and Woman inherit the Person entity Bean. As in the previous example, we still use the Client for testing.

Person. java: Entity Bean.

Man. java: class on which the object Bean depends.

Woman. java: the class on which the object Bean depends.

PersonTest. java: the service interface of the Session Bean

PersonTestBean. java: Implementation class of Session Bean

Client. java: Client class for testing EJB.

Jndi. properties: The jndi property file that provides basic configuration properties for accessing jdni.

Build. xml: ant configuration file for compiling, publishing, testing, and clearing ejbs.

The following describes 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 getGender ()
{
Return gender;
}

}

Specify the P_TYPE column to differentiate the beans of each object.

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 the attribute of whether it is a good man.

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 (String name );
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. setGender (gender );
Man. setGood (B );
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 );
Return woman. getId ();
}

Public Person find (int I)
{
Return manager. find (Person. class, I );
}

Public List findByName (String name)
{
Return manager. createQuery ("from Person p where p. name =: name"). setParameter ("name", name). listResults ();
}

Public List findByInfo (String gender)
{
Return manager. createQuery ("from Person p where p. gender =: gender"). setParameter ("gender", gender). listResults ();
}

}

In this session Bean, you can create Man and Woman entity beans and search for them.

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 ("Yan Yue pan", "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 lihuan ");

For (Object o: list)
{
Woman w = (Woman) o;
System. out. printf ("% s pretty? Conclusion: % B % n ", w. getName (), w. isIsbeautiful ());
}
}

}

This client is used for testing.

Run. bat: run-c all in the {$ JBOSS_HOME}/bin directory to start JBOSS.

Http: // localhost: 8080/jmx-console/HtmlAdaptor? Action = inspectMBean & name = jboss % 3 Aservice % 3 DHypersonic % 2 Cdatabase % 3 DlocalDB, then call the startDatabaseManager () method to open the HSQL management tool to manage the database.

Execute ejbjar target in the Ant view of Eclipse. Alternatively, go to the project directory under the command line and execute ant ejbjar to package the compilation and release the EJB.

Run target in the Ant view of Eclipse. Or enter the project directory under the command line and run ant run to test the EJB.

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.