In the preceding example, the attributes of the object bean correspond to the columns in the data table. The default settings are used. With column, you can specify the column name in the data table for the attribute.
The column statement 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 helper class used to process entity beans. It can be used to generate/delete Persistent Object beans, search for object beans through the primary key, and query object beans through the query language.
The entitymanager interface declaration is as follows:
Package javax. EJB; Import java. SQL. connection; /** * Interfaces used to interact with persistent Context */ Public interface entitymanager { /** * Persistent Management of entity beans * @ Param entity */ Public void create (Object entity ); /** * Combine the state of the given entity bean with the persistent context. Similar to database update operations. * @ Param entity * @ Return refers to the entity instance to be combined. */ Public <t> T Merge (T entity ); /** * Deleting an instance * @ Param entity */ Public void remove (Object entity ); /** * Search by primary key. * @ Param entityname * @ Param primarykey * @ Return query an instance */ Public object find (string entityname, object primarykey ); /** * Search by primary key * @ Param primarykey * @ Return query an instance */ Public <t> t find (class <t> entityclass, object primarykey ); /** * Synchronization between 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 the name query. * @ Param name: predefined query name * @ Return query an instance */ Public query createnamedquery (string name ); /** * Execute a local SQL query statement * @ Param sqlstring local query statement * @ Return returns the query instance */ Public query createnativequery (string sqlstring ); /** * Update to database * @ Param entity */ Public void refresh (Object entity ); /** * Delete objects in persistent Context * @ Param entity */ Public void evict (Object entity ); /** * Check whether the current context contains this object * Context. * @ Param entity * @ Return */ Public Boolean contains (Object entity ); }
|
@ Joincolumns
Used to indicate that the primary key complies with the primary key, which is described in later sections.
This example mainly contains the following files. This example mainly implements the function of managing student scores. Student is an entity bean that manages the basic information of students (names and scores of each course). The score of students is also an Entity Bean. Tacherbean is a stateless Session Bean used to call the Entity Bean. As in the previous example, we still use the client for testing.
· Student. Java: Entity Bean.
· Score. Java: Entity Bean.
· Teacher. Java: Service Interface of Session Bean
· Teacherbean. Java: Implementation class of Session Bean
· Client. Java: client class for testing EJB.
· JNDI. properties: The JNDI property file, which provides basic configuration properties for accessing jdni.
· Build. xml: ant configuration file for compiling, publishing, testing, and clearing ejbs.
The following describes 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. onetoworkflow; 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; // Score of the student Private collection <score> scores; // Primary key automatically generated @ 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 (scores = NULL) { Scores = new arraylist <score> (); } 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 <score> getscores () { Return scores; } Public void setscores (collection <score> scores) { This. Scores = scores; } } |
Student. Java implements the Student Entity Bean, which provides the basic information of the student and the score of the student. The score is another entity bean. Student object bean and score object bean are one-to-many relationships. From the perspective of score, they are many-to-one relationships.
The Entity Bean needs to be annotated using @ entity. In addition, it specifies that the entity bean corresponds to the table student (by commenting @ table (name = "student ")), you can see this table in the JBoss 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; // Primary key automatically generated @ 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) { This. Student = student; } } |
This entity bean stores student 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 <string, integer> map ); Public student getstudent (); @ Remove Public void leave (); } |
This session bean interface provides methods to increase scores and obtain users.
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 <string, integer> map) { If (student = NULL) { Student = new student (); } Student. setname (studentname ); Set <string> 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 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) CTX. Lookup (teacher. Class. getname ()); Map <string, integer> map = new hashmap <string, integer> (); Map. Put ("", new INTEGER (98 )); Map. Put ("chemistry", new INTEGER (149 )); Map. Put ("physical", new INTEGER (143 )); Teacher. addscore ("smallnest", MAP ); Student = teacher. getstudent (); String name = student. getname (); System. Out. printf ("display % S score: % N", name ); Collection <score> C = student. getscores (); For (score: C) { System. Out. printf ("% s: % S % N", score. getname (), score. getnumber () + ""); } } }
|
This client increases the score of the student and the test shows the student's information.
Run run. BAT: Run-c all in the {$ jboss_home}/bin directory to start JBoss.
Http: // localhost: 8080/JMX-console/htmladaptor? Action = inspectmbean & name = JBoss % 3 aservice % 3 dhypersonic % 2 cdatabase % 3 dlocaldb, then call the startdatabasemanager () method to open the hsql management tool to manage the database.
Execute ejbjar target in the ant view of Eclipse. Alternatively, go to the project directory under the command line and execute ant ejbjar to package the compilation and release the EJB.
Run target in the ant view of Eclipse. Or enter the project directory under the command line and run ant run to test the EJB.