EJB 3.0 Development Guide Entity Bean (1)
Developing entity beans in EJB3.0 is very simple, you can program like developing a general Java Bean, just do a small amount of comments. An entity bean does not need home interface or Remote, Local interface. The entity bean is generated by EntityManager, and is a combination of persistence and persistent layers, from a persistent layer recovery. JBoss's EJB3.0 architecture is above Hibernate. Note: @ENTINTY: If you want to create a class of entity bean, you must add this comment on the class to tell the container This class is an entity bean. This bean's primary key is specified by @ID. The declaration of this comment is as follows:
@Target (TYPE) @Retention (RUNTIME) public @interface Entity {String name () default ""; EntityType entityType () default CMP; AccessType access () default PROPERTY; int version () default 3;}
Name is used to specify the name, default, and class name of the entity bean. EntityType is used to specify that this bean is a persistent entity bean for container management or a persistent entity bean for bean management. Can be CMP and BMP. AcceptType is used to specify the way the container accesses the persistence data of this EJB. Property is used to tell the container to use GET / SET access to persistence data (which is not a TRANSIENT annotation), and the file tells the container to access the field directly, and the field should declare the protected type. In order to provide clients such as other session beans, this bean should implement the serializable interface. The entity bean must constructor with a parameter. Sustainable properties include: Java's basic type (int, long, etc.), String, Biginteger, BigDecimal, Java.util.date, Calendar, Java.sql.date, Java.sql.Time, Java.sql.TimeStamp, Byte [], char [], other entity bean types, other entity beans collection (list, not supported). @Table is used to specify the primary table used by this entity bean, sometimes other tables, see the introduction of the following chapters. UNIQUECONSTRAINT comments are used to add constraints. @ID is used to specify the primary key of this entity bean. It can have a variety of ways: • Table: Container specifies that the underlying data table ensures unique. · SEQUENCE: Using the database's sequence column to ensure the only Identity: use the database's Indentit column to ensure that the only · Auto: Pick a suitable way by the container to ensure that the only · None: Container is not responsible for the generation of primary key, completed by the calling program . @OneTomany may have a pair, multi-to-one, one-on-one, multi-to-many relationships, and later two relationships in the examples. For example, students and class scores are a pair of relationships. In EJB3.0, a pair of associations must be two-way, that is, there must be a multi-to-one association and it corresponds to it. OneTomany annotation declaration is as follows:
@Target ({Method, Field}) @Retime (runtime) public @ITERface OneTomany {string targentity () default ""; cascadetype [] cascade () default {}; fetchtype fetch () default lazy;} When we use this note When you use the generic programming of JDK5.0, return the collection Collection
package com.kuaff.ejb3.entity; import javax.ejb.CascadeType; import javax.ejb.Entity; import javax.ejb.FetchType; import javax.ejb.GeneratorType; import javax.ejb.Id; import javax.ejb.JoinColumn ; import javax.ejb.OneToMany; import javax.ejb.Table; import java.util.ArrayList; import java.util.Collection; import java.io.Serializable; @ Entity @ Table (name = "STUDENT") public class Student Implements serializable {// Private key private int id; // Student Name Private String Name; // Student Score Private Collection
package com.kuaff.ejb3.entity; import java.io.Serializable; import javax.ejb.Entity; import javax.ejb.GeneratorType; import javax.ejb.Id; import javax.ejb.JoinColumn; import javax.ejb.ManyToOne ; import javax.ejb.Table; @ Entity @ Table (name = "Score") public class Score implements Serializable {private int id; private String name; private int number; private Student student; // primary key automatically generated @Id (generate = GeneratorType.Auto) Public int getId () {return;} public void setId;} public string getname () {return name;} public void setname (String name) {this. name = name;} public int getNumber () {return number;} public void setNumber (int number) {this.number = number;} @ManyToOne @JoinColumn (name = "student_id") public Student getStudent () {return student; } Public void setstudent (student student) {this.student = student;}} This entity bean stores students' scores. Teacher.java
package com.kuaff.ejb3.entity; import javax.ejb.Remote; import javax.ejb.Remove; import java.util.Map; @Remotepublic interface Teacher {public void addScore (String studentName, Map
This session bean interface provides an increased score and a method of obtaining the user. Teacherbean.java
package com.kuaff.ejb3.entity; import javax.ejb.EntityManager; import javax.ejb.Inject; import javax.ejb.Remove; import javax.ejb.Stateful; import java.util.Map; import java.util.Set ; @Statefulpublic class TeacherBean implements Teacher {@Inject private EntityManager manager; private Student student; public Student getStudent () {return student;} public void addScore (String studentName, Map
package com.kuaff.ejb3.entity; import java.util.Map; import java.util.HashMap; import java.util.Collection; import javax.naming.InitialContext; import javax.naming.NamingException; public class Client {public static void main (String [] args) throws NamingException {InitialContext ctx = new InitialContext (); Teacher teacher = (Teacher) ctx.lookup (Teacher.class.getName ()); Map