EJB3.0 Development Guide: Multi-to-many and one-on-one

xiaoxiao2021-03-06  49

In the previous example, we demonstrated a pair of and more-to-one examples, which will demonstrate multiple and one-on-one relationship.

Students and teachers are many-to-many relationships. A student has a number of teachers, a teacher teaches multiple students.

Students and files are one-on-one relationship (I don't know if there is any files in foreign students?).

In order to achieve a multi-to-many relationship, a related table is required to be associated between two entities. JBoss can automatically generate a related table, you can also specify the information of the associated table @AssociationTable.

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 annotation declarations are as follows:

@Target ({METHOD, FIELD}) public @interface AssociationTable {Table table () default @Table (specified = false); JoinColumn [] joinColumns () default {}; JoinColumn [] inverseJoinColumns () default {};}

Associated Table Note Specifies the name of the associated table, the column of the primary table and the column from the table.

In order to achieve a one-on-one relationship, you need to use @Ontoone to comment.

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 Dossier also defines related associations, it is two-way. The two-way meant is to find a dossier through a Student entity, and you can find a student through a dossier.

@ Ontoone's annotation declaration is 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 has the following documents, which mainly achieves the relationship between students and teachers, students, and archives. Student, Teacher, Dossier are entity beans. Student and Dossier are the relationship between two-way ANDONE, Student and Teacher are the relationship between ManyTomany, and two-way. As with the previous example, we still use the Client test.

Student.java: entity bean.

Dossier.java: Class depends on entity bean.

TEacher.java: Class depends on entity bean. EntityTest.java: Business Interface for Session Bean

EntityTest Bean.java: Realization of Session Bean

Client.java: Test EJB client class.

JNDI.Properties :jndi property file provides basic configuration properties for accessing JDNI.

Build.xml: Ant profile, to compile, release, test, and clear EJB.

Here is a description of 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) @ssociationTable (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 = generatortortype.auto) public long getId () {returnid;} public void setid (long id) {this.id = ID;} public void setResume (String resume) {this.Resume = resume; 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 o;} 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 () {returnis;}} 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 ("Teacher Hu Xi, Ph.D."); Manager.create (Teacher1); Teacher2.Setid (New Long (2)); Teacher2.setname ("Liyongchi"); Teacher2.setInfo ("Professor Li Yongchi, Ph.D."); Manager.create (Teacher2); Student1.SetFirst ("晁") Student1.setlast ("Yue Clip"); Dossier1.setresume ("This is the file of Yue Yue"); Student1.Setdossier (dossier1); student1.add (student1); student2.setfirst ("Zhao"); student2.setlast ("Zhi Wei"); Dossier2.setresume ("This is Zhao Zhiwei's file"); Student2.Setdossier (dossier2); students1.add (student2); student3.setfirst ("field");

Student 3.SetLast ("Ming"); Dossier3.SetResume ("This is Omage's Archive"); Student3.Setdossier (dossier3); StudentS2.Add (student3); Teacher1.setstudents; Teacher2.SetStudents (students2) Public List FindbyName (String Name) {Return Manager.createQuery ("from Teacher T WHERE T.NAME =: Name"). SetParameter ("name", name) .listresults ();}} is available in this session bean The method of creating each entity bean and provides a way to find a teacher.

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 ("晁", "Yue Clip", "8", "Smallnest@kuaff.com "," Men "); Dao.Create (" Zhu "," 立 焕 "," 6 "," zhuzhu@kuaff.com "," female "); list list = DAO.FINDALL (); for (Object O : list) {student s = (student) O; system.out.printf ("% s% s gender:% s% n", s.getname (). getFirst (), s.getname (). getLast ( ), S.GetGender ()); DAO.EVICT (S);}}}

This client is tested.

Run {$ jboss_home} / bin's run.bat: run -c all, start JBoss.

HTTP: // localhost: 8080 / jmx-console / htmladaptor? action = inspectMben & name = jboss% 3AService% 3DHYPERSONIC% 2CDATABASE% 3DLOCALDB, then call the StartDatabaseManager () method to open the HSQL Management Tools Management Database.

Execute the EJBJAR TARGET in the ANT view of Eclipse. Or in the command line, enter this project directory, perform Ant Ejbjar, publish this EJB.

转载请注明原文地址:https://www.9cbs.com/read-81456.html

New Post(0)