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}) @Retrion (Runtime)
Public @Interface column {
String name () Default ""
Boolean primarykey () Default false DEFAULT FALSE
Boolean Unique () Default False;
Boolean nullable () Default true;
Boolean insertable () Default true;
Boolean Updatable () Default True 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 an auxiliary 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 used to interact with persistence context
* /
Public interface EntityManager {
/ **
* Make entity beans managed by persistence
* @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's unique instance
* /
PUBLIC
/ **
* Delete instance
* @Param Entity
* /
Public Void Remove (Object Entity);
/ **
* Find in accordance with the primary key.
* @Param EntityName
* @Param PrimaryKey
* @Return query instance
* /
Public Object Find (String EntityName, Object PrimaryKey);
/ **
* Find according to the primary key
* @Param PrimaryKey
* @Return query instance
* /
Public
/ **
* Synchronization of persistence context and underlying database
* /
Public void flush ();
/ **
* Execute an EJBQL query
* @Param EJBQLSTRING EJBQL query statement
* @Return the new query instance
* /
Public Query CreateQuery (String Ejbqlstring);
/ **
* Execute a named query
* @Param Name predefined query name
* @Return query instance
* /
Public Query CreateNamedQuery (String Name); / **
* Enter a local SQL query statement
* @Param sqlstring local query statement
* @Return returns the query instance
* /
Public Query CreatenativeQuery (String Sqlstring);
/ **
* Updated to the database
* @Param Entity
* /
Public void refresh (Object Entity);
/ **
* Persistence context Remove entities
* @Param Entity
* /
Public void evic (Object Entity);
/ **
* Check if this entity is included in the current context
* Context.
* @Param Entity
* @Return
* /
Public Boolean Contains (Object Entity);
}
@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.