In the previous example, each entity Bean is mapped to only one table in the database. In fact, one entity Bean can be mapped to multiple tables. It is often used in some projects that require dictionary tables. For example, I have used many data tables defined by the national standard for my previous projects. In the following example, gender exists as a dictionary table. The Student Entity maps to the student information table and Gender table.
You can use @ SecondaryTable to comment on the slave 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 the table name, category, schema, Union column, and constraint. If you use multiple tables, you can use the following annotations to declare multiple tables:
@ SecondaryTable
@ Target ({TYPE}) @ Retention (RUNTIME)
Public @ interface SecondaryTables {
SecondaryTable [] value () default {};
}
This example mainly contains the following files. This example mainly implements the function of managing students. Student is an entity Bean. The name attribute of this Bean is a class, that is, the Name class. This Name class is a dependent value object. The student's gender is mapped to the second table. StudentDAOBean is a stateless Session Bean used to call the Entity Bean. As in the previous example, we still use the Client for testing.
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 object Bean depends.
StudentDAO. java: the service interface of the Session Bean
StudentDAOBean. 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.
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 = "first", column ={@ Column (name = "FIRST ")}),
@ 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 students. Add the comments of the second table to the class declaration:
@ SecondaryTables ({
@ SecondaryTable (name = "GENDER", join ={@ JoinColumn (name = "GENDER_ID ")})
})
The second annotation mapped to the gender property is added:
@ 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 ("pan", "Yue pan", "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 Gender: % s % n", s. getName (). getFirst (), s. getName (). getLast (), s. getGender ());
Dao. evict (s );
}
This client increases the score of the student and the test shows the student's email.
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,
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.