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 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 unique
NONE: The container is not responsible for the generation of primary key, and 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}) @Retrion (Runtime)
Public @INTERFACE 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 {
/ / Primary key
Private int ID;
// Student name
PRIVATE STRING NAME;
// Student score
PRIVATE COLLECTION
// Primary key automatically
@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 addscores (String name, int number)
{
IF (score == null)
{
Scorers = 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
{
Return scores;
}
Public void setscores (Collection
{
THIS.SCORES = Score;
}
}
Student entity bean implemented Student entity beans, providing students' basic situation and student score, and 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.manytoad;
Import javax.ejb.table;
@Entity
@Table (Name = "score")
Public Class Score IMPLEments Serializable
{
Private int ID;
PRIVATE STRING NAME;
PRIVATE INT NUMBER;
PRIVATE STUDENT;
// Primary key automatically
@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 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;
@Remote
Public Interface Teacher
{
Public Void AddScore (String StudentName, 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;
@Stateful
Public Class Teacherbean Implements Teacher
{
@Inject
Private EntityManager Manager;
PRIVATE STUDENT;
Public student getstudent ()
{
Return student;
}
Public void addscore (String StudentName, Map
{
IF (student == null)
{
STUDENT = new student ();
}
Student.setName (studentname);
Set
For (String Sname: SET)
{
Student.addscores (sname, map.get (sname) .intValue ());
}
}
@Remove
Public void leave ()
{
Manager.create (student);
}
}
This is the implementation class of the session bean.
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.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
For (Score SCORE: C)
{
System.out.Printf ("% s:% s% n", score.getname (), score.getnumber () "");
}
}
}
This client adds students' scores and tests the information about this student.
Please run the run.bat: Run? C all under the directory of {$ jboss_home} / bin, 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.