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