SoFunction
Updated on 2025-03-09

EJB3.0 development is many to many and one to one

In the previous example, we demonstrate one-to-many and many-to-one examples, and in this chapter, we will demonstrate the relationship between many-to-many and one-to-one.

Students and teachers are a many-to-many relationship. One student has multiple teachers, and one teacher teaches multiple students.

Students and archives are a one-to-one relationship (I wonder if students abroad have archives?).

In order to achieve a many-to-many relationship, an association table is needed in the database to establish associations between two entities. JBoss can automatically generate association tables, and you can also @AssociationTable to specify the information of the association table.

like:

@ManyToMany(cascade = {, }, fetch = , isInverse = true)
@AssociationTable(table = @Table(name = "STUDENT_TEACHER"),

joinColumns = {@JoinColumn(name = "TEACHER_ID")},inverseJoinColumns = {@JoinColumn(name = "STUDENT_ID")})

@AssociationTable's annotation declaration is as follows:
@Target({METHOD, FIELD})

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

The association table annotation specifies the name of the association table, the columns of the primary table, and the columns of the slave table.

In order to achieve a one-to-one relationship, @OneToOne needs to be annotated.

like:

@OneToOne(cascade = {})
@JoinColumn(name = "DOSSIER_ID")

public Dossier getDossier()
{
return dossier;
}

This defines a one-way one-to-one relationship. If the correlation is also defined in Dossier, then it is bidirectional. Two-way means that a Dossier can be found through a Student entity, and a Dossier can be found through a Student.

@OneToOne's annotation statement 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 mainly contains the following documents. This example mainly realizes the relationship between students and teachers, students and archives. Student, Teacher, and Dossier are all physical beans. Student and Dossier are a two-way relationship between OneToOne, and Student and Teacher are ManyToMany's relationship, which is also two-way. Like the previous example, we still use Client tests.

: Entity Bean.

: The class on which entity beans depend.

: The class on which entity beans depend.

: The service interface of the session bean

EntityTest: The implementation class of session bean

: Test EJB's client class.

: jndi attribute file, providing access to the basic configuration properties of jdni.

:ant configuration file to compile, publish, test, and clear EJB.

The following is an introduction to the content of each file.

  

package .;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

@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 = )

public int getId()
{
return id;
}

public void setId(int id)
{
= id;
}

public void setFirst(String first)
{
= first;
}

public String getFirst()
{
return first;
}

public void setLast(String last)
{
= last;
}

public String getLast()
{
return last;
}

public void setDossier(Dossier dossier)
{
= dossier;
}

@OneToOne(cascade = {})
@JoinColumn(name = "DOSSIER_ID")

public Dossier getDossier()
{
return dossier;
}

public void setTeacher(Set teachers)
{
= teachers;
}

@ManyToMany(cascade = {, }, fetch = , isInverse = true)
@AssociationTable(table = @Table(name = "STUDENT_TEACHER"),

joinColumns = {@JoinColumn(name = "TEACHER_ID")},inverseJoinColumns = {@JoinColumn(name = "STUDENT_ID")})

public Set getTeacher()
{
return teachers;
}
}

  

package .;

import ;
import ;
import ;

@Entity

public class Dossier implements
{
private Long id;
private String resume;

@Id(generate = )
public Long getId()
{
return id;
}

public void setId(Long id)
{
= id;
}

public void setResume(String resume)
{
= resume;
}

public String getResume()
{
return resume;
}
}



package .;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

@Entity

public class Teacher implements
{
private Long id;
private String resume;
private String name;
private String info;
private Set students;

@Id(generate = )

public Long getId()
{
return id;
}

public void setId(Long id)
{
= id;
}

public void setName(String name)
{
= name;
}

public String getName()
{
return name;
}

public void setInfo(String info)
{
= info;
}

public String getInfo()
{
return info;
}

public void setStudents(Set students)
{
= students;
}

@ManyToMany(cascade = {, }, fetch = )
@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;
}
}

  

package .;

import ;
import ;

@Remote

public interface EntityTest
{
public void createData();
public List findByName(String name);
}

  

package .;

import ;
import ;
import ;
import ;
import ;
import ;

@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();
(new Long(1));
("hushisheng");
("Professor Hu Shisheng, PhD supervisor");
(teacher1);
(new Long(2));
("liyongchi");
("Professor Li Yongchi, PhD supervisor");
(teacher2);

("Chao");
("Yue Pan");
("This is Chao Yuepan's archive");
(dossier1);
(student1);

("Zhao");
("Zhiwei");
("This is Zhao Zhiwei's file");
(dossier2);
(student2);

("field");
("bright");

("This is Tian Ming's file");
(dossier3);
(student3);

(students1);
(students2);

}

public List findByName(String name)
{
return ("from Teacher t where = :name").setParameter("name", name).listResults();
}

}

This session bean provides a method to create individual entity beans and provides a method to find teachers.



package .;

import ;
import ;
import ;

public class Client
{
public static void main(String[] args) throws NamingException
{
InitialContext ctx = new InitialContext();
StudentDAO dao = (StudentDAO) (());
int id = ("Chao","Yue Pan","8","smallnest@","Male");
("Zhu","Lihuan","6","zhuzhu@","female");
List list = ();
for(Object o:list)
{
 Student s = (Student)o;
("%s%s' gender:%s%n",().getFirst(),().getLast(),());
 (s);
}
}
}

This client is used for testing.

Please run: run –c all in the bin directory and start JBOSS.

http://localhost:8080/jmx-console/HtmlAdaptor?action=inspectMBean&name=jboss%3Aservice%3DHypersonic%2Cdatabase%3DlocalDB, then call the startDatabaseManager() method to open the HSQL management tool to manage the database.

Execute ejbjar target in the Ant view of Eclipse. Or on the command line, enter this project directory, execute ant ejbjar, and publish this EJB with a package.

Execute a run target in the Ant view of Eclipse. Or on the command line, enter this project directory, execute ant run, and test this EJB.