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
STUDENT_TEACHER "), joinColumns = {@JoinColumn (name =" TEACHER_ID ")}, inverseJoinColumns = {@JoinColumn (name =" STUDENT_ID ")}) public Set
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
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
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.