EJB3.0 Development Guide: Multi-Table Mapping

xiaoxiao2021-03-06  48

In the previous example, each of us is only mapped to a table in the database. In fact, an entity bean can be mapped to multiple tables. In some items that require a dictionary table, it will be used often, and the items I have done before, use the data sheet specified in many national standards. In our example, gender exists as a dictionary table, and the student's entity will map the two tables of the student information table, the gender table.

You can use @secondarytable from the table to comment:

@Target ({TYPE}) @Retention (RUNTIME) public @interface SecondaryTable {String name (); String catalog () default ""; String schema () default ""; JoinColumn [] join () default {}; UniqueConstraint [ ] UNIQUECONSTRAINTS () default {};

This comment can specify a table name, classification, schema, union column, constraint, etc.

If you use multiple tables, you can use the following comments to declare multiple tables:

@SecondaryTable

@Target ({type}) @Retention (runtime) public @Interface secondarytables {secondarytable [] value () default {};

This example has the following documents, this example mainly implements the function of managing students. Student is an entity bean. This bean's Name property is a class, that is, the Name class, this Name class is a dependent value object. The gender of students is mapped to the second table. StudentDaobean is a stateless session bean for calling entity beans. As with the previous example, we still use the Client test.

This example is basically the same as the previous example, just Student.java and Client vary.

Student.java: entity bean.

Name.java: Class depends on entity bean.

StudentDao.java: Business Interface for Session Bean

StudentDaobean.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.secondary; import javax.ejb.Dependent; import javax.ejb.DependentAttribute; import javax.ejb.Column; import javax.ejb.Entity; import javax.ejb.GeneratorType; import javax.ejb.Id ; import javax.ejb.Table; import javax.ejb.SecondaryTables; import javax.ejb.SecondaryTable; import javax.ejb.JoinColumn; @Entity @Table (name = "STUDENT") @SecondaryTables ({@SecondaryTable (name = " GENDER ", join = {@JoinColumn (name =" GENDER_ID ")})}) public class Student implements java.io.Serializable {private int id; private name name; private String grade; private String email; private String gender; @ ID (generate = generatorType.auto) public int getId;} public void setId (int id) {this.id = ID;} public void setname (name name) {this.name = name;} @dependent ({@DependentAttribute (name = "first", column = {@column (name = "first")}), @DependentAttribute (Name = "last", column = {@column (name = ")})}) PUBLIC NAME GETNAME () {Return Name;} public void setgrade (String grade) {this.grade = grade; @column (name = "GRADE") public String getGrade () {return grade;} public void setEmail (String email) {this.email = email;} @Column (name = "EMAIL") public String getEmail () {return email; } Public void setgender (string gender) {this.gen = gender;

@Column (name = "gender", secondarytable = "gender") public string getgen () {return gender;}} student entity bean implemented Student entity Bean, which provides the basic situation of students. In the class, the annotation of the second table is declared:

@Secondarytables ({

@SecondaryTable (name = "gender", join = {@joincolumn (name = "gender_id")})

})

In the Gnder property, add a note of the second sheet:

@Column (name = "gender", secondarytable = "gender")

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 adds the student's score and tests the Email of this student.

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.

Execute Run Target in the ANT view of Eclipse. Or on the command line, enter this project directory, perform Ant Run, test this EJB.

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

New Post(0)