Multi-table Mapping _jsp programming for EJB 3.0 Development Guide

Source: Internet
Author: User
In the previous example, each of our entity beans is mapped to only one table in the database. In fact, an entity bean can be mapped to more than one table. In some of the items that need a dictionary table, it is often used, as I have done before, to use a lot of GB data tables. In our example, the gender as a dictionary table exists, the student entity will be mapped to the Student information table, the gender table of the two tables.

You can use @secondarytable to annotate from a table:

@Target ({TYPE}) @Retention (RUNTIME)

Public @interface Secondarytable {
String name ();
String catalog () default "";
String schema () default "";
Joincolumn[] Join () default {};
Uniqueconstraint[] Uniqueconstraints () default {};
}

This annotation can specify table names, categories, schemas, Union columns, constraints, and so on. If you use more than one table, you can use the following comments to declare multiple tables:

@SecondaryTable
@Target ({TYPE}) @Retention (RUNTIME)

Public @interface Secondarytables {
Secondarytable[] Value () default {};
}

This example mainly has the following several files, this example mainly realizes manages the student the function. Student is an entity bean, the Name property of the Bean is a class, the name class, and the name class is a dependent value object. The student's gender is mapped to the second table. Studentdaobean is a stateless session bean that is used to invoke entity beans. As in the previous example, we still use the client test.

This example is basically the same as the previous example, except that Student.java and client are different.

Student.java: Entity Bean.

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

Studentdao.java: Business Interface for session Bean

Studentdaobean.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.

Student.java

Package com.kuaff.ejb3.secondary;

Import javax.ejb.Dependent;
Import Javax.ejb.DependentAttribute;
Import Javax.ejb.Column;
Import javax.ejb.Entity;
Import Javax.ejb.GeneratorType;
Import Javax.ejb.Id;
Import javax.ejb.Table;
Import Javax.ejb.SecondaryTables;
Import javax.ejb.SecondaryTable;
Import Javax.ejb.JoinColumn;

@Entity
@Table (name = "STUDENT")
@SecondaryTables ({
@SecondaryTable (name = "GENDER", join = {@JoinColumn (name = "gender_id")})
})

public class Student implements Java.io.Serializable
{
private int id;
private name name;
private String grade;
Private String Email;
Private String gender;

@Id (generate = Generatortype.auto)

public int getId ()
{
return ID;
}

public void setId (int id)
{
This.id = ID;
}

public void SetName (name name)
{
THIS.name = name;
}

@Dependent ({@DependentAttribute (name = "A", column ={@Column (name = "A")}),

@DependentAttribute (name = "Last", Column ={@Column (name = "Last")})}

Public Name GetName ()
{
return name;
}

public void Setgrade (String grade)
{
This.grade = grade;
}

@Column (name = "GRADE")

Public String Getgrade ()
{
return grade;
}

public void Setemail (String email)
{
This.email = email;
}

@Column (name = "EMAIL")

Public String Getemail ()
{
return email;
}

public void Setgender (String gender)
{
This.gender = gender;
}

@Column (name = "Gender", secondarytable = "gender")

Public String Getgender ()
{
return gender;
}

}


Student.java implements the Student entity Bean, which provides basic information about the students. Add a second table annotation on the class declaration:

@SecondaryTables ({
@SecondaryTable (name = "GENDER", join = {@JoinColumn (name = "gender_id")})
})

Add a second note to the gender property:

@Column (name = "Gender", secondarytable = "gender")

Client.java

Package com.kuaff.ejb3.secondary;

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 ();

Studentdao DAO = (Studentdao) ctx.lookup (StudentDAO.class.getName ());

int id = dao.create ("Chao Buzhi", "Yue Climb", "8", "smallnest@kuaff.com", "male");

Dao.create ("Zhu", "Li Huan", "6", "zhuzhu@kuaff.com", "female");



List List = Dao.findall ();

for (Object o:list)

{

Student s = (Student) o;

System.out.printf ("%s%s Sex:%s%n", S.getname (). GetFirst (), S.getname (), GetLast (), S.getgender ());

Dao.evict (s);

}

The client increased the student's score and tested the student's email.

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,

Then call the Startdatabasemanager () method and 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.