EJB 3.0 Development Guide Entity Bean (1)

xiaoxiao2021-03-06  19

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 , then you do not need to specify the type of targeTentity, otherwise the return type declares that the TargeTentity is declared. CascadeType specifies what processing when this entity bean is new or Merge, what is related to the entity that is related to the entity: Merge: When the main entity bean is being used by Merge, the associated entity bean is also Merge · Create: When the main entity bean is used by Create At the time, the associated entity bean is also Create · Remove: When the primary entity bean is evic, the associated entity bean is also Evict · All: Includes the above case FetchType specifies the way to read from the data: Lazy or Eager. LAZY is only active from the database from the database when the first visit is first accessed. @MANYTOONE We ​​know a pair of associations two-way. Methods from the ManyToone annotation will be declared in the associated entity bean. @JoinColumn We know that two entities can be associated, but correspond to Table, you need to specify a column as an foreign key. If Name does not specify Name, it is considered that the primary key in the primary table and the primary key in the table have the same name as the foreign key. If you do not specify ReferenceColumnName, you will think that the foreign key corresponds to the primary key of the side table. @JOINCOLUMNS is used to indicate that the primary key is described in the later chapter. This example has the following documents, which mainly implements the function of managing student scores. Student is an entity bean that manages students 'basic information (name and scores of each class), where students' scores are an entity bean. Tacherbean is a stateless session bean for calling entity beans. As with the previous example, we still use the Client test. · Student.java: entity bean. · Score.java: entity bean. · Teacher.java: Business Interface for Session Bean · Teacherbean.java: Realization Class for 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.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 score; // Primary key automatically generate @ID (generate = generatorType.Auto) public int getId () { Return ID;} public void setid (int id) {this.id = ID;} public string getname () {return name;} public void setname (String name) {this.name = name;} public void addresscores , int number) {if (score == null) {score = new arraylist (); score score = new score (); score.setname (name); score.setnumber (Number); score.setstudent This); scores.add (score);} @ONETOMANY (cascade = cascadetype.all, fetch = fetchtype. EAGER) @JoinColumn (name = "student_id") public Collection getScores () {return scores;} public void setScores (Collection scores) {this.scores = scores;}} Student.java achieved Student entity BEAN, it provides the basic situation of students and the score of students, and the score is another entity bean. The Student entity bean and score entity beans are a couple of relationships, and the point of view standing is to look at the relationship. Entity Bean needs to use @entity to make a comment, and it specifies this entity bean corresponding to Table Student (by comment @table (name = ")), you can see this table in JBoss's database. Score.java

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 map) Public student getStudent (); @remove public void leave ();

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 map) {if (student == null ) {Student = new student (); student.setname; set set = map.keyset (); for (String Sname: set) {student.addscores (SNAME, Map.get (sname). INTVALUE ());}} @Remove Public void Leave () {manager.create (student);}} This is the implementation class of session beans. Client.java

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 map = new HashMap (); Map.PUT ("Language", New Integer (98)); Map.Put ("Chemistry", New Integer (149)); Map.PUT ("Physics", New Integer (143)) Teacher.addscore ("Smallnest", MAP); Student Student = Teacher.getStudent (); string name = student.getname (); system.out.printf ("Display% s score:% n", name); Collection c = student.getscores (); for (score score: c) {system.out.printf ("% s:% s% n", score.getname (), score.getnumber () "" }}}

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

New Post(0)