EJB3.0 many developers and one-to-one MySQL

Source: Internet
Author: User
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 implement multi-to-many relationships, you need to disable EJB in the database.

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 in the {$ JBOSS_HOME}/bin directory.

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.