In the example of all entity beans in front, the primary keys we use are ID and are automatically generated by the container.
Such as:
@ID (generate = generatortype.auto)
Public int getID ()
{
Return ID;
}
In fact, the primary key can be an arbitrary Java basic type, or a basic type of packaging class, such as an Integer, String type, or a primary key class with a field or property. Note that if you use the primary key, you need to change the generated rules in the comment from the primary key to NONE. Just generate the main key value by the program.
The primary key must implement the HashCode and Equals methods.
The first example is an entity bean that stores student information. In that example, use the Name class as an attribute, and in this example, we will use Name as the primary key, pay attention to Name Type of Java. class. As with the previous example, we still use the Client test.
Name.java: Primary key.
Student.java: Entity Bean class.
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.
Name.java
package com.kuaff.ejb3.composite; 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) {this.first = first;} public string getlast () {return last;} public void setlast (String last) {this.last = Last;} public int 6code () {return (first last) .hashcode ();} Public boolean equals (object object) {if (this == Object) Return true; if (object == null) Return False; if (! (! (!) Return False; Name Name = (NAME) Object; if ((Name.first.equals First)) && (name.last.equals (last))) Return True; Else Return False;}} student.java
package com.kuaff.ejb3.composite; 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 name name; private String grade; private String email; public void setName (name name) {this .name = name;} @ID (generate = generatorType.none) @Dependent ({@DependentAtttribute (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.e Mail = email;} @Column (name = "email") public string getemail () {return email;}} studentdao.java
package com.kuaff.ejb3.composite; import javax.ejb.Remote; import java.util.List; @Remote public interface StudentDAO {void create (String first, String last, String grade, String email); Student find (Name name (String name); list findbylastname (String name); list findbyemail; void merge (student s);}
StudentDaobean.java
package com.kuaff.ejb3.composite; 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 void 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);} public Student find (name name) {return manager.find (Student.class, name);} 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 =: "). SetParameter (" 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);}} client.java
package com.kuaff.ejb3.composite; 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 ()); Dao.Create ("晁", "Yue Pan", "8", "Smallnest@kuaff.com"); DAO.CREATE ("Zhu", "立 焕", "6", "zhuzhu@kuaff.com"); name name = new name ("Zhu", "立 焕"); // List list = DAO.FindByemail ("zhuzhu@kuaff.com"); Student S = DAO.FIND (NAME); System.out.Printf ("% s% s Email:% S% N", s.getname (). getFirst (), S.GetName (). getlast (), s.getemail ()); / * 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 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.
in
Eclipse
of
Ant
Executation in the view
Run Target
. Or in the command line, enter this project directory, execute
Ant Run,
Test this
EJB
.