Inheritance _jsp programming for EJB3.0 entity beans

Source: Internet
Author: User
Tags one table jboss
In EJB3.0, entity beans can implement inheritance relationships. Like an entity bean with a person, it has a name and sex two attributes.

When God Hoji the creation of man, he made two kinds of men: Man and woman. Both man and woman are entity beans, and they all inherit person.

A single table strategy is that the data for the secondary and child entities are stored in one table, and a column is specified to distinguish them.

Such as:

@Entity
@Inheritance (strategy = inheritancetype.single_table, Discriminatortype = discriminatortype.string)
@DiscriminatorColumn (name = "P_type", nullable = True)
@ Inheritance's comments are 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 policies that are used for inheritance and to distinguish between the types and values of the columns used by these entities.

@DiscriminatorColumn annotations are used on a single table policy and a federated table strategy. Used to specify the columns that are required to differentiate 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 several files, this example mainly realizes the person and the man, the woman inheritance relations, the following two chapters introduces the example and this example is identical. Man and woman inherit the person entity bean. As in the previous example, we still use the client test.

Person.java: Entity Bean.

Man.java: The class on which the entity bean depends.

Woman.java: The class on which the entity bean depends.

Persontest.java: Business Interface for session Bean

Persontestbean.java: Implementation class for Session Bean

Client.java: The client class that tests the EJB.

The Jndi.properties:jndi property file provides access to the basic configuration properties of the Jdni.

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

Here is an introduction to the contents 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;
}

}

Specifies that the P_type column is used to distinguish between entity beans.

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 Mans extends person
{
Private Boolean Isgood;
public void Setgood (Boolean isgood)
{
This.isgood = Isgood;
}

public boolean Isgood ()
{
return isgood;
}
}

This entity Bean adds a property 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 mans = New Man ();
Man.setname (name);
Man.setgender (gender);
Man.setgood (b);
Manager.create (Mans);
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 ();
}

}

The method for creating a man, woman entity Bean is provided in this session bean, and a lookup method is provided.

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 ("Shi", "male", true);
Dao.createwoman ("Zhuli", "female", true);
Person p = dao.find (i);
System.out.printf ("Sex of%s:%s%n", P.getname (), P.getgender ());
List List = Dao.findbyname ("Zhuli");

for (Object o:list)
{
Woman w = (Woman) o;
System.out.printf ("%s) pretty?" Conclusion:%b%n ", W.getname (), w.isisbeautiful ());
}
}

}

This client is used to test.

Please run the {$JBOSS _home}/bin directory under Run.bat:run–c all and start JBOSS.

http://localhost:8080/jmx-console/HtmlAdaptor?action=inspectMBean&name=jboss%3Aservice%3DHypersonic% 2CDATABASE%3DLOCALDB, and then call the Startdatabasemanager () method to open the Hsql management tool Management database.

Executes Ejbjar target in Eclipse's ant view. Or, under the command line, go into the engineering directory, execute Ant Ejbjar, and publish the EJB in the compilation package.

Execute run target in Eclipse's ant view. Or, under the command line, go into the engineering directory, execute Ant run, and test the EJB.

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.