In the previous example, we demonstrated one-to-many and one-to-one. In this chapter, we will demonstrate the relationship between multiple-to-one and one-to-one.
Students and teachers are many-to-many relationships. One student has multiple teachers and one teacher teaches multiple students.
There is a one-to-one relationship between students and archives (Do you know if there are any foreign students ?).
To achieve multi-to-many relationships, the database needs to associate tables to establish associations between two entities. JBoss can automatically generate an association table. You can also @ AssociationTable to specify the association table information.
For example:
@ ManyToMany (cascade = {CascadeType. CREATE, CascadeType. MERGE}, fetch = FetchType. EAGER, isInverse = true)
@ AssociationTable (table = @ Table (name = "STUDENT_TEACHER "),
JoinColumns = {@ JoinColumn (name = "TEACHER_ID")}, inverseJoinColumns = {@ JoinColumn (name = "STUDENT_ID ")})
@ AssociationTable:
@ Target ({METHOD, FIELD })
Public @ interface AssociationTable {
Table table () default @ Table (specified = false );
JoinColumn [] joinColumns () default {};
JoinColumn [] inverseJoinColumns () default {};
}
The joined table comment specifies the name of the joined table, the columns of the primary table, and the columns of the slave table.
To implement a one-to-one relationship, @ OneToOne must be used for annotation.
For example:
@ OneToOne (cascade = {CascadeType. ALL })
@ JoinColumn (name = "DOSSIER_ID ")
Public Dossier getDossier ()
{
Return dossier;
}
This defines a one-to-one relationship. If Dossier also defines related associations, it is bidirectional. A two-way query means that you can find a Dossier through a Student object, and you can find a Student through a Dossier.
@ OneToOne:
@ Target ({METHOD, FIELD}) @ Retention (RUNTIME)
Public @ interface OneToOne {
String targetEntity () default "";
CascadeType [] cascade () default {};
FetchType fetch () default EAGER;
Boolean optional () default true;
}
This example mainly contains the following documents. This example mainly realizes the relationship between students and teachers, students and archives. Student, Teacher, and Dossier are entity beans. Student and Dossier are two-way relationships between OneToOne. Student and Teacher are manytoone relations and also two-way relationships. As in the previous example, we still use the Client for testing.
Student. java: Entity Bean.
Dossier. java: class on which the object Bean depends.
Teacher. java: class on which the entity Bean depends.
EntityTest. java: the service interface of the Session Bean
EntityTest Bean. 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.relationships;
Import javax. ejb. CascadeType;
Import javax. ejb. Entity;
Import javax. ejb. FetchType;
Import javax. ejb. GeneratorType;
Import javax. ejb. Id;
Import javax. ejb. JoinColumn;
Import javax. ejb. OneToOne;
Import javax. ejb. ManyToMany;
Import javax. ejb. Table;
Import javax. ejb. AssociationTable;
Import java. util. ArrayList;
Import java. util. Set;
Import java. util. Collection;
Import java. io. Serializable;
@ Entity
@ Table (name = "STUDENT ")
Public class Student implements Serializable
{
Private int id;
Private String first;
Private String last;
Private Dossier dossier;
Private Set Teachers;
@ Id (generate = GeneratorType. AUTO)
Public int getId ()
{
Return id;
}
Public void setId (int id)
{
This. id = id;
}
Public void setFirst (String first)
{
This. first = first;
}
Public String getFirst ()
{
Return first;
}
Public void setLast (String last)
{
This. last = last;
}
Public String getLast ()
{
Return last;
}
Public void setDossier (Dossier dossier)
{
This. dossier = dossier;
}
@ OneToOne (cascade = {CascadeType. ALL })
@ JoinColumn (name = "DOSSIER_ID ")
Public Dossier getDossier ()
{
Return dossier;
}
Public void setTeacher (Set Teachers)
{
This. teachers = teachers;
}
@ ManyToMany (cascade = {CascadeType. CREATE, CascadeType. MERGE}, fetch = FetchType. EAGER, isInverse = true)
@ AssociationTable (table = @ Table (name = "STUDENT_TEACHER "),
JoinColumns = {@ JoinColumn (name = "TEACHER_ID")}, inverseJoinColumns = {@ JoinColumn (name = "STUDENT_ID ")})
Public Set GetTeacher ()
{
Return teachers;
}
}
Dossier. java
Package com. kuaff. ejb3.relationships;
Import javax. ejb. Entity;
Import javax. ejb. GeneratorType;
Import javax. ejb. Id;
@ Entity
Public class Dossier implements java. io. Serializable
{
Private Long id;
Private String resume;
@ Id (generate = GeneratorType. AUTO)
Public Long getId ()
{
Return id;
}
Public void setId (Long id)
{
This. id = id;
}
Public void setResume (String resume)
{
This. resume = resume;
}
Public String getResume ()
{
Return resume;
}
}
Teacher. java
Package com. kuaff. ejb3.relationships;
Import javax. ejb. AssociationTable;
Import javax. ejb. Basic;
Import javax. ejb. CascadeType;
Import javax. ejb. Column;
Import javax. ejb. Entity;
Import javax. ejb. FetchType;
Import javax. ejb. Id;
Import javax. ejb. JoinColumn;
Import javax. ejb. ManyToMany;
Import javax. ejb. Table;
Import javax. ejb. Transient;
Import javax. ejb. Version;
Import java. util. Set;
Import javax. ejb. GeneratorType;
@ Entity
Public class Teacher implements java. io. Serializable
{
Private Long id;
Private String resume;
Private String name;
Private String info;
Private Set Students;
@ Id (generate = GeneratorType. IDENTITY)
Public Long getId ()
{
Return id;
}
Public void setId (Long id)
{
This. id = id;
}
Public void setName (String name)
{
This. name = name;
}
Public String getName ()
{
Return name;
}
Public void setInfo (String info)
{
This.info = info;
}
Public String getInfo ()
{
Return info;
}
Public void setStudents (Set Students)
{
This. students = students;
}
@ ManyToMany (cascade = {CascadeType. CREATE, CascadeType. MERGE}, fetch = FetchType. EAGER)
@ AssociationTable (table = @ Table (name = "STUDENT_TEACHER "),
JoinColumns = {@ JoinColumn (name = "TEACHER_ID", referencedColumnName = "ID ")},
InverseJoinColumns = {@ JoinColumn (name = "STUDENT_ID", referencedColumnName = "ID ")})
Public Set GetStudents ()
{
Return students;
}
}
EntityTest. java
Package com. kuaff. ejb3.relationships;
Import javax. ejb. Remote;
Import java. util. List;
@ Remote
Public interface EntityTest
{
Public void createData ();
Public List findByName (String name );
}
EntityTestBean. java
Package com. kuaff. ejb3.relationships;
Import javax. ejb. EntityManager;
Import javax. ejb. Inject;
Import javax. ejb. Stateless;
Import java. util. HashSet;
Import java. util. Set;
Import java. util. List;
@ Stateless
Public class EntityTestBean implements EntityTest
{
Private @ Inject EntityManager manager;
Public void createData ()
{
Teacher teacher1 = new Teacher ();
Teacher = new Teacher ();
Set Students1 = new HashSet ();
Set Students2 = new HashSet ();
Student student1 = new Student ();
Student student2 = new Student ();
Student student3 = new Student ();
Dossier dossier1 = new Dossier ();
Dossier dossier2 = new Dossier ();
Dossier dossier3 = new Dossier ();
Teacher1.setId (new Long (1 ));
Teacher1.setName ("hushisheng ");
Teacher1.setInfo ("Professor Hu Shisheng, doctoral advisor ");
Manager. create (teacher1 );
Teacher2.setId (new Long (2 ));
Teacher2.setName ("liyongchi ");
Teacher2.setInfo ("Professor Li yongchi, doctoral advisor ");
Manager. create (teacher2 );
Student1.setFirst ("success ");
Student1.setLast ("Yue pan ");
Dossier1.setResume ("this is the file of Yi yuepan ");
Student1.setDossier (dossier1 );
Students1.add (student1 );
Student2.setFirst ("Zhao ");
Student2.setLast ("Zhiwei ");
Dossier2.setResume ("this is Zhao Zhiwei's Archive ");
Student2.setDossier (dossier2 );
Students1.add (student2 );
Student3.setFirst ("Tian ");
Student3.setLast ("Ming ");
Dossier3.setResume ("this is Tian Ming's file ");
Student3.setDossier (dossier3 );
Students2.add (student3 );
Teacher1.setStudents (students1 );
Teacher2.setStudents (students2 );
}
Public List findByName (String name)
{
Return manager. createQuery ("from Teacher t where t. name =: name"). setParameter ("name", name). listResults ();
}
}
This session Bean provides the method for creating various entity beans and the method for finding instructors.
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 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.