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:
@Entity: If you want to create an entity bean class, 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
The primary table used to specify this entity bean, sometimes other tables, see the introduction of the following chapters. UNIQUECONSTRAINT comments are used to add constraints.
@ID
The primary key used to specify this entity bean. It can have a variety of ways:
Table: The container specifies the underlying data sheet to ensure unique.
SEQUENCE: Use the database of Sequence List to ensure unique
Identity: Use the Indentit column using the database to ensure unique
Auto: Pick a suitable way by the container to ensure that the only None: the container is not responsible for the generation of the primary key, is done by the calling program.
@Onetomany
There may be one-to-many, multi-one, one-on-one, multi-to-many relationship, and later two relationships in the following 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}) @Retection (runtime) public @ITERface OneTomany {string targengtity () default ""; cascadetype [] cascade () default {}; fetchtype fetch () default lazy;
When we use this comment as a GET method comment, if you use JDK5.0 universal programming, return a collection Collection
CascadeType specifies what processing does the entity associated with it when this entity bean is new or Merge requires how to do it:
MERGE: When the main entity bean is by Merge, the associated entity bean is also Merge
CREATE: When the main entity bean is used by Create, the associated entity bean is also Create
Remove: When the main entity bean is evict, the associated entity bean is also evvent
All: More than the above
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 the 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
It is used to indicate that the primary key is used in the following chapters.
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 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.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
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 Voi D 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; @Remote public 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 ; @Stateful public class TeacherBean implements Teacher {@Inject private EntityManager manager; private Student student; public Student getStudent () {return student;} public void addScore (String studentName, Map
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
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.