In the chapter above, we mention that the property bean's attribute can be a basic object of Java, DATE, in fact, the properties of entity beans can also be other Java objects. These Java objects cannot be read directly from the persistence context, depending on the main entity bean. Not like the associated entity bean, the collection of relying on the dependency object is not supported in EJB3.0.
The dependency object does not support inheritance, which will be discussed in EJB3.1.
Dependent class needs to use ◎ DependentObject to comment:
@Target ({type}) @Retention (runtime) public @ITERFACE DEPENDENTOBJECT {Accessty Access () default property;
This comment can specify how the container accesses this class is to access by the property or directly through the field.
One property in the entity bean requires a dependency value object, then this attribute's GET method needs to use @Dependent comments:
@Target ({Method, Field}) @Retention (Runtime) public @Interface dependent {dependentattribute [] value () default {};
This comment can specify dependency dependentAttribute:
@Target ({}) @Retention (Runtime) public @Interface dependentattribute {string name (); column [] column () default {};
You can specify the name of the attribute name and mapping. Here, the attribute of a main entity bean can map multiple columns in the data table.
If you still don't quite understand, look at the example below.
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. StudentDaobean is a stateless session bean for calling entity beans. As with the previous example, we still use the Client test.
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.dependent; 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; @Entity @Table (name = "STUDENT") public class Student implements java.io.Serializable {private int id; private name name; private String grade; private String email; @Id (generate = Public int getId () {return id;} public void setid;} public void setname (name name) {this.name = name;} @dependent ({@Dependentatattribute) (Name = "first", column = {@column (name = "first")}), @DependentAttribute (name = "last", column = {@column (name = "last")}}) PUBLIC NAME GETNAME () {RETURN Name;} public void setgrade (string grade) {this.grade = grade;} @column (name = "GR ADE ") public String getGrade () {return grade;} public void setEmail (String email) {this.email = email;} @Column (name =" EMAIL ") public String getEmail () {return email;}} Student. Java implements a Student entity bean that provides the basic situation of students. The student's name is Name class, via @Dependent
{@DependentAtttribute (name = "first", column = {@column (name = "first")}),
@DependentAtttribute (name = "last", column = {@column (name = "last")}})
To declare, and specify this dependencies of the two properties first and last, and map to the first and last columns of the data table. Name.java
package com.kuaff.ejb3.dependent; import java.io.Serializable; import javax.ejb.AccessType; import javax.ejb.DependentObject; @DependentObject (access = AccessType.PROPERTY) public class Name implements java.io.Serializable {private String first; private String last; public Name () {} public Name (String first, String last) {this.first = first; this.last = last;} public String getFirst () {return first;} public void setFirst ( String first;} public string getlast ()} public string getlast () {return last;} {this.last = last;}}
This value object is also very simple, and the general javabean is almost, but there are two places to pay attention:
1. This class implements the java.io.serializable interface
2. This class uses @DependentObject.
StudentDao.java
package com.kuaff.ejb3.dependent; import javax.ejb.Remote; import java.util.List; @Remote public interface StudentDAO {int create (String first, String last, String grade, String email); Student find (int id (String name); list findbylastname (String name); list findbyemail (String email); void merge (student s);}
This session bean interface provides a way to find the user.
StudentDaobean.java
package com.kuaff.ejb3.dependent; import java.util.List; import javax.ejb.EntityManager; import javax.ejb.Inject; import javax.ejb.Stateless; @Stateless public class StudentDAOBean implements StudentDAO {@Inject private EntityManager manager ; public int create (String first, String last, String grade, String email) {Student student = new Student (); student.setName (new Name (first, last)); student.setGrade (grade); student.setEmail ( email); manager.create (student); return student.getId ();} public Student find (int id) {return manager.find (Student.class, id);} public List findByFirstName (String name) {return manager. . createQuery ( "from Student s where s.name.last =: name") setParameter ( "name", name) .listResults ();} public List findByLastName (String name) {return manager.createQuery ( "from Student s where S.Name.first =: Name "). setParamet ER ("Name", name) .ListResults ();} public list Findbyemail (String email) {Return Manager.createQuery ("from student s where s.email =: email). SetParameter (" email ", email). ListResults (); public void merge (student s) {manager.merge (s);} This is the implementation class of session beans. You can see the main entity bean.
Client.java
package com.kuaff.ejb3.dependent; 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 "); DAO.CREATE (" Zhu "," 立 焕 "," 6 "," zhuzhu@kuaff.com "); list list = dao.Findbyemail (" zhuzhu@kuaff.com "); for (Object O: List) {student s = (student) O; system.out.printf ("% s% s Email:% s% n", s.getname (). getFirst (), s.getname (). getLast () , s.getemail ());}}} 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.