Multi-and one-to-one _jsp programming for EJB3.0 development

Source: Internet
Author: User
Tags ming jboss
In the previous example, we demonstrated a one-to-many and Many-to-many example, which will demonstrate many-to-many and one-to-one relationships in this chapter.

The student and the teacher are many to many relations. One student has many teachers and one teacher teaches many students.

Student and file is a one-to-one relationship (do not know the foreign students have no records?) )。

In order to achieve many-to-many relationships, relational tables are required in the database to establish associations between two entities. JBoss can automatically generate association tables, and you can also @associationtable to specify information about the associated tables.

Such as:

@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's comments are as follows:
@Target ({method, FIELD})

Public @interface Associationtable {
Table table () default @Table (Specified=false);
Joincolumn[] Joincolumns () default {};
Joincolumn[] Inversejoincolumns () default {};
}

The associated table annotation specifies the name of the associated table, the columns of the primary table, and the columns from the table.

In order to achieve a one-to-one relationship, you need to annotate with @onetoone.

Such as:

@OneToOne (cascade = {Cascadetype.all})
@JoinColumn (name = "dossier_id")

Public Dossier Getdossier ()
{
return dossier;
}

This defines a one-way one-to-one relationship. If the associated association is also defined in dossier, then it is bidirectional. Bi-directional means that through a student entity can find a dossier, through a dossier can find a student.

@ Onetoone's comments are as follows:
@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 has the following documents, this example mainly realizes the student and the teacher, the student and the file relations. Student, Teacher, and dossier are entity beans. Student and dossier are a two-way onetoone relationship, student and teacher are manytomany relationships, and are two-way. As in the previous example, we still use the client test.

Student.java: Entity Bean.

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

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

Entitytest.java: Business Interface for session Bean

Entitytest Bean.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.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 A;
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 a)
{
This.first = A;
}

Public String GetFirst ()
{
return A;
}

public void Setlast (String last)
{
This.last = Last;
}

Public String GetLast ()
{
return to 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 teacher2 = 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 tutor");
Manager.create (Teacher1);
Teacher2.setid (New Long (2));
Teacher2.setname ("Liyongchi");
Teacher2.setinfo ("Professor Li Yongchi, Doctoral tutor");
Manager.create (TEACHER2);

Student1.setfirst ("Chao Buzhi");
Student1.setlast ("Yue Climbing");
Dossier1.setresume ("This is the Shi file");
Student1.setdossier (Dossier1);
Students1.add (STUDENT1);

Student2.setfirst ("Zhao");
Student2.setlast ("Zhi Wei");
Dossier2.setresume ("This is the Zhiwei Zhao speaking file");
Student2.setdossier (DOSSIER2);
Students1.add (Student2);

Student3.setfirst ("Tian");
Student3.setlast ("Ming");

Dossier3.setresume ("This is the Tian Ming 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 a way to create individual entity beans and provides a way to find teachers.

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

This client is used to test.

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