I have been learning EJB and has been more than a year. I have received a big project before receiving a big project. WebLogic 7.0, the database is used by SQL Server 2000, so I can only be on WebLogic SQL Server. The 2000 environment is described.
First, configure the data source in WegLogic, there is a JDBC of SQL Server 2000 in WebLogic, so you can use it directly, pay attention, you must use TX-DataSource in EJB, in addition, you have to play a JTA for SQL Server 2000 database. Patch, so that it can better support distributed transactions.
Below, I will make a simple introduction to a certified subsystem in our system. (Reference: http://www-900.ibm.com/developerworks/cn/cnedu.nsf/java-onlinecourse-Bytitle/991A4E2DCA2D6CE148256BCE00135FAE? OpenDocument) I am basically made according to this example.
In this sub-system, LocalUserbean (entity) is used to verify user identity, userinfobean user details, UserGroupBean user group, localrolebean user role, UserManageBean's unique remote interface of the subsystem.
Relationship: LocalUserbean ---- UserInfobean is the single-phase relationship of One2one, UserGroupBean ---- LocalUserBean is the two-phase relationship of One2MANY, LocalUserbean ---- LocalRolebean is the bipolar relationship of Many2MANY.
LocalUserbean ---- UserInfobean: One-on-one relationship is relatively simple
LocalUserbean Field (CMP)
Public Abstract Void SeteMail (java.lang.string email);
Public Abstract void setpw (java.lang.string pw);
Public abstract java.lang.string getemail ();
Public abstract java.lang.string getpw ();
Establish relationship with UserInfo (CMR)
Public Abstract Void SetUserInfo (UserManageSample.userInfo UserInfo);
Public AbstractUserInfo getUserInfo ();
UserInfobean Field (CMP)
Public Abstract Void SeteMail (java.lang.string email);
Public Abstract void setDept (java.lang.string dept);
Public Abstract void setName (java.lang.string name);
......
Public abstract java.lang.string getemail ();
Public abstract java.lang.string getDept ();
Public abstract java.lang.string getname ();
......
Since the relationship is single, the userinfobean does not have a CMR field.
WebLogic-cmp-rdbms-jar.xml
field-map>
field-map>
TABLE-MAP>
WebLogic-Rdbms-Bean>
field-map>
field-map>
field-map>
TABLE-MAP>
WebLogic-Rdbms-Bean>
colorn-map>
rateship-role-map>
WebLogic-Relationship-Role>
WebLogic-Rdbms-Rethlation> LocalUserBean Method:
EJBCREATE (String email, string pw, userinfovalueObject user) {
STEMAIL (email);
SetPW (PW)
}
EjbPostcreate (String email, String Pw, userinfovalueObject user) {
Context ctx = new initialContext ();
LocalroleHome Home = (localrolehome) ctx.lookup ("localrole");
Home.create (user.getemail, .........);
}
With EJBPOSTCREATE, call LocalRoleBean's local interface, and create user information, userinfovalueObject is a user information class, remember, this class must implement Java.IO. Serializable, because the requirements in EJB can serialize.
Method in UserManage
Advance users. While adding localuser, add UserInfo
Public Void AddUser (String Email, String Pw, UserInfowValueObject User) {
Try {
LocalUser User = UserHome.create (Email, PW, USER);
} catch (createexception ce) {
Throw new EJBEXCEPTION ("Create User" Email "Error!", CE);
}
}
delete users. Deleting the LocalUser while also deleting UserInfo
Public void removeuser (String email) {
Try {
UserHome.Remove (email);
} catch (createexception ce) {
Throw new ejbexception ("Delete User" Email "Error!", CE);
}
}
Check user password
Public Boolean VerifyUser (String Email, String PW) {
Try {
LocalUser User = UserHome.FindByPrimaryKey (email);
Return User.getPw (). Equals (PW);
} catch (FINDEREXCEPTION FE) {
Throw new ejbexception ("Verify User" Email "Error!", Fe);
}
}
Localuser ---- Localrole's relationship is the bipolar relationship of Many2MANY.
When I start writing EJB CMP, since I always failed to Many2Many's relationship map, I avoided the relationship between MANY2MANY, and later found that the relationship in EJB is actually realized in the database, so for the database If you need to understand, it is not complicated.
LocalroleBean: (CMP)
Public Abstract void setroleName (java.lang.string rolename);
Public Abstract void setdescription (java.lang.string description);
Public abstract java.lang.string getrolename ();
Public Abstract Java.lang.String getDescription (); (CMR)
Public abstract java.util.collection getlocaluser ();
Public Abstract Void SetLocalUser (Java.util.collection Localuser);
In LocalUserbean, because it is the relationship between Many2MANY, it is a Collection interface;
Public Abstract void setroletab (java.util.collection roletab);
Public abstract java.util.collection getroletab ();
WebLogic-cmp-rdbms-jar.xml
field-map>
field-map>
TABLE-MAP>
WebLogic-Rdbms-Bean>
colorn-map>
rateship-role-map>
WebLogic-Relationship-Role>
colorn-map>
rateship-role-map>
WebLogic-Relationship-Role>
WebLogic-Rdbms-RELATION>
Method (omitial) implemented in UserManage;
UserGroup ---- Localuser is the double phase relationship of One2MANY;
(CMP)
Public Abstract void setName (java.lang.string name);
Public Abstract void setdescription (java.lang.string description);
Public abstract java.lang.string getname ();
Public Abstract Java.lang.String getDescription ();
(CMR)
Public abstract java.util.collection getlocaluser ();
Public Abstract Void SetLocalUser (Java.util.collection Localuser);
WebLogic-cmp-rdbms-jar.xml
field-map>
field-map>
TABLE-MAP>
WebLogic-Rdbms-Bean>
colorn-map>
rateship-role-map>
WebLogic-Relationship-Role>
WebLogic-Rdbms-RELATION>
Summary: First, you must fully understand the principle of EJB CMP / CMR. Second, it is necessary to understand how the relationship in the database principle is defined. This is very good for the mapping of your realization, third, that is to see the code and examples of others. See the relevant introduction article, do more examples.
Because I have limited knowledge and level, please don't worry about teaching and criticize.
My contact information: zceast@hotmail.com (msn)
I hope to discuss and learn together with you.