EJB 3.0 Development Guide Entity Bean (2)

xiaoxiao2021-03-06  14

In one example above, the attributes of the entity bean correspond to the columns in the data table, all of which use the default setting. With Column, you can specify the column name in the data table for the property. The declaration of Column is as follows:

@Target ({TYPE, METHOD, FIELD}) @Retention (RUNTIME) public @interface Column {String name () default ""; boolean primaryKey () default false; boolean unique () default false; boolean nullable () default true; boolean insertable () default true; boolean updatable () default true; String columnDefinition () default ""; String secondaryTable () default ""; int length () default 255; int precision () default 0; int scale () default 0 Boolean Specified () default true; // for internal use only} EntityManager is a secondary class for processing entity beans. It can be used to generate / delete persistent entity beans, find entity beans through the primary key, query entity beans by querying language. Below is the declaration of the EntityManager interface:

Package javax.ejb; import java.sql.connection; / *** Interface for interacting and persistence context * / public interface EntityManager {/ ** * Make entity beans from persistent management * @Param entity * / public void Create (Object Entity); / ** * Combine the status and persistence context of a given entity bean. A updated operation similar to the database. * @Param Entity * @return is a combined entity instance * / public t merge (t enttem); / ** * Delete instance * @Param entity * / public void remove (Object entity); / ** * primary key lookup * @param entityName * @param primaryKey * @return query instance * / public Object find (String entityName, Object primaryKey);. / ** * * @param primaryKey * @return lookup query example of a primary key * / public T Find (Class EntityClass, Object PrimaryKey); / ** * Persistence Context with the underlying Database * / Public Void Flush (); / ** * Perform an EJBQL Query * @Param EJBQLSTRING EJBQL Query Statement * @Return the new query instance * / public query create Query (String ejbqlstring); / ** * Perform a named query * @Param Name predefined query name * @return query instance * / public query createenamedQuery (String name); / * * * Perform a local SQL query statement * @Param SQLSTRING Local query statement * @return Returns instance * / public query createnative Query (String Sqlstring); / ** * Updated to the database * @Param entity * / public void refresh (Object) Entity); / ** * Persistence context Remove entity * @Param entity * / public void evic (Object Entity); / ** * Check if this entity is included in the current context * @ @Param Entity * @return * / Public Boolean Contains (Object @} @JoinColumns is used to indicate that the primary key is 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.javapackage 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 {// Motor 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;} public string getname () {return name;} public void setname (String name) {this.name = name;} public void addresscores String name, int number) {if (score == null) {score = new arraylist (); score score = new score (); score.setname (name); score.setNumber (Number); score. SetStudent (this); score.add (score);} @Onetomany (cascade = cascadetype.all, fetc h = FetchType.EAGER) @JoinColumn (name = "student_id") public Collection getScores () {return scores;} public void setScores (Collection scores) {this.scores = scores;}} Student.java A STUDENT entity bean provides the basic situation of students and the student score, 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.javapackage 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 (); @ removepublic void leave ();} This session bean interface provides an increase 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

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

New Post(0)