SoFunction
Updated on 2025-03-09

Multi-table mapping for EJB 3.0 Development Guide

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 multiple tables. It is often used in some projects that require dictionary tables. For example, the projects I have done before, many data tables specified by the national standard are used. In our example below, gender exists as a dictionary table, and the student entity will be mapped to the student information table and the gender table.

From the table you can use @SecondaryTable to comment:

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

public @interface SecondaryTable {
String name();
String catalog() default "";
String schema() default "";
JoinColumn[] join() default {};
UniqueConstraint[] uniqueConstraints() default {};
}

This comment can specify table names, classifications, schemas, union columns, constraints, etc. If you use multiple tables, you can use the following comments to declare multiple tables:

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

public @interface SecondaryTables {
SecondaryTable[] value() default {};
}

This example mainly contains the following documents, which mainly realize the function of managing students. Student is an entity bean, and the name attribute of this bean is a class, that is, the Name class, and this Name class is a dependency value object. The students' gender is mapped to the second table. StudentDAOBean is a stateless session bean that is used to call entity beans. Like the previous example, we still use Client tests.

This example is basically the same as the previous example, but it is different from the Client.

: Entity Bean.

: The class on which entity beans depend.

: The service interface of the session bean

: The implementation class of session bean

: Test EJB's client class.

: jndi attribute file, providing access to the basic configuration properties of jdni.

: ant configuration file, used to compile, publish, test and clear EJB.

The following is an introduction to the content of each file.



package .;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

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

public class Student implements
{
private int id;
private Name name;
private String grade;
private String email;
private String gender;

@Id(generate = )

public int getId()
{
return id;
}

public void setId(int id)
{
= id;
}

public void setName(Name 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)
{
= grade;
}

@Column(name = "GRADE")

public String getGrade()
{
return grade;
}

public void setEmail(String email)
{
= email;
}

@Column(name = "EMAIL")

public String getEmail()
{
return email;
}

public void setGender(String gender)
{
= gender;
}

@Column(name = "gender", secondaryTable = "GENDER")

public String getGender()
{
return gender;
}

}


Implemented Student entity bean, which provides the basic situation of students. Added a comment to the second table on the class declaration:

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

Added a comment to map the second picture to the gender attribute:

@Column(name = "gender", secondaryTable = "GENDER")



package .;

import ;

import ;

import ;


public class Client
{
public static void main(String[] args) throws NamingException

{

InitialContext ctx = new InitialContext();

StudentDAO dao = (StudentDAO) (());

int id = ("Chao","Yue Pan","8","smallnest@","Male");

("Zhu","Lihuan","6","zhuzhu@","female");



List list = ();

for(Object o:list)

{

Student s = (Student)o;

("%s%s' Gender:%s%n",().getFirst(),().getLast(),());

(s);

}

This client increases the student's score and the test shows the student's email.

Please run: run –c all in the bin directory and start JBOSS.

http://localhost:8080/jmx-console/HtmlAdaptor?action=inspectMBean&name=jboss%3Aservice%3DHypersonic%2Cdatabase%3DlocalDB,

Then call the startDatabaseManager() method to open the HSQL management tool to manage the database.

Execute ejbjar target in the Ant view of Eclipse. Or on the command line, enter this project directory, execute ant ejbjar, and publish this EJB with a package.

Execute a run target in the Ant view of Eclipse. Or on the command line, enter this project directory, execute ant run, and test this EJB.