Toplink is the first Java object relationship sustainability architecture, the original service of WebGain's products, is now acquired by Oracle, and is re-encapsulated as Oracle As Toplink. Toplink provides highly flexible and efficient mechanisms for storing Java objects and enterprise Java components (EJBs) in relational database tables. Toplink provides excellent performance and options for developers, can work with any database, any application server, any development toolset, and any J2EE architecture. This article still uses the Toplink 3.5 of the industry that is widely used as a reference version, simply tells the use of Toplink's usage points. This article is not a STEP BY STEP entry instance tutorial, but it is eye-catching some basic concepts of Toplink, summarizing some of the best points, which can also spy the way Toplink's usage and its design principle.
First, connect the database
CREATE A login Object
Project.getlogin ()
New DatabaseLogin ()
Specifying Database and Driver Information
//project.getlogin (). useoracle ();
Project.getlogin (). setDriverclass (oracle.jdbc.driver.OracleDriver.class);
Project.getlogin (). setConnectionstring ("JDBC: Oracle: Thin @ DBSERVER: 1521: ORC1")
Setting login parameters
Project.getlogin (). setusername ("scott");
Project.getlogin (). setPassword ("tiger");
Instance Code 1
DatabaseElogin.setLicensePath ("c: // program files // common files // WebGain Shared //")
Project Project = new myproject ();
Project.getlogin (). setUsername ("UserID");
Project.getlogin (). setPassword ("passwd");
Databasesession session = project.createdTabasesSession ();
Session.login ();
Instance Code 2
DatabaseLogin.setlicensePath (...);
DatabaseElogin login = new databaseelogin ();
Login.set ...
...
Project Project = New Project (Login);
Databasesession session = project.createdTabasesSession ();
Session.login ();
Second, the use of DatabaseSession
An application must create a DatabaseSession, which stores the following:
1. A Project, DatabaseLogin instance (stored database login, configuration information)
2. A DatabaseAccessor instance (packaged JDBC connection, processing database access)
3. All lasting class of applications for Descriptors
4. A object MAP as a cache
Databasesession instances must be created by Project's instance, the Project object must be initialized by the correct database configuration parameters, such as JDBC Driver, DB URL.
REGISTORS (before login, login, but to register) DatabaseSession.AddDescriptors (Vector) or DatabaseSession.AddDescriptor (Descriptor)
Connect the database
DatabaseSession.login (), you can use this session to access the database, use "Query Framework"
Cache
You can use the initializeidentitymaps () method to refresh the cache. Note: It is not used before using the object in the cache before use.
logout database
DatabaseSession.logout (), no registration when logging in again, Descriptors
You can specify the exceptional processing of DatabaseSESSY:
Session.sexceptionHandler (New ExceptionHandler () {
Public HandleException (RuntimeException EX) {
IF ((ex.getMessage (). Equals ("Connection Reset By Peer") {
ex.GetAccessor (). ReestablishConnection (ex. maxssion ());
Return ex.getations (). EXECUTEQUERY (EX.GETQUERY ());
}
}
});
Third, a complete instance code using Toplink
import TOPLink.Public.Sessions *;. DatabaseLogin.setLicensePath ( ""); DatabaseLogin loginInfo = new DatabaseLogin (); loginInfo.useJDBCODBCBridge (); loginInfo.useSQLServer (); loginInfo.setDataSourceName ( "dbserver"); loginInfo.setUserName ( ""); loginInfo.setPassword ( ""); Project project = new Project (loginInfo); DatabaseSession session = project.createDatabaseSession (); session.addDescriptors (this.buildAllDescriptors ()); try {session.login ();} Catch (DatabaseException Exception) {Throw new runtimeException ("Database Error Occurred At Login:" ); System.out.Println ("Login Failed.");} ... session.logout ();
Fourth, query framework (Query Framework)
Directly call the method provided by the Databasesession object, complete the query, update, etc. Under the update operation, UnitOfWork is used to provide optimized performance.
Sample code
Employee employee = (Employee) session.readObject (Employee.class, new ExpressionBuilder () get ( "lastName") equals ( "Smith")..); Vector employees = session.readAllObjects (Employee.class, new ExpressionBuilder (). Get ("Salary"). GreaterThan ("1000")); 5. Using Expression
Toplink provides a mechanism called Expression that allows the object model based query mechanism, Toplink translates these Expressions for SQL statements, and then convert the query results to objects.
Create an Expression
Expression always gets through Expresson or ExpressionBuilder. ExpressionBuilder can be seen as a substitute for a query object. Generally named as the name of the corresponding object.
An instance of an ExpressionBuilder is associated with a specific query, do not try to use the same builder to generate multiple expression.
2. Subproof
ReportQuery is to get information about an object set, not the object itself. Common methods:
AddAttribute (String), Addplace (String), AddCount (String), etc.
1) Sample code 1
Query staff containing more than 5 administrative staff
ExpressionBuilder emp = new ExpressionBuilder (); ExpressionBuilder managedEmp = new ExpressionBuilder (); ReportQuery subQuery = new ReportQuery (Employee.class, managedEmp); subQuery.addCount ();. SubQuery.addSelectionCriteria (managedEmp.get ( "manager") equals ( EMP)); //? Expression Exp = Emp.SubQuery (Subquery) .greatertrthan (5);
2) Sample Code 2
Query the address of the address in Ottawa's highest salary
ExpressionBuilder emp = new ExpressionBuilder (); ExpressionBuilder ottawaEmp = new ExpressionBuilder (); ReportQuery subQuery = new ReportQuery (Employee.class, ottawaEmp); subQuery.addMax ( "salary"); subQuery.setSelectionCriteria (ottawaEmp.get ( "address") .get ("street"). Equals ("ottawa")); Expression Exp = Emp.get ("SALARY"). Equals (Subquery). And (Emp.Get ("Address"). Get ("street") .equals ("ottawa");
3. Parallel Expressions
Parallel Expression must have its own ExpressionBuilder, and this builder's constructor must be used as a parameter as a Class. There is only a unique primary expressionBuilder, which uses an empty parameter constructor, which will be obtained from Query.
1) instance code
Query all staff expressionBuilder EMP = New ExpressionBuilder (); // Main Builder
ExpressionBuilder spouse = new expressionbuilder (Employee.class); // Parallel Builder
Expression Exp = Emp.get ("Lastname"). Equal (spouse.get ("lastname") // Reflections based on object model
.and ("spouse"). Equal (spouse.get ("spouse"))
.and (Emp.Notequal (spouse);
4. Parametric Expressions, Finders
1) Parameterized expression example
/ / Conformally use the expression of query, pay attention to the parameter expressionBuilder EMP = New ExpressionBuilder EMP = New ExpressionBuilder (); Expression FirstNameExpression = EQUAL (Emp.GetParameter ("firstnameter)); // Generate a ReadObjectQuery object set reference query class, query parameters ReadObjectQuery query = new ReadObjectQuery (); query.setReferenceClass (Employee.class); query.setSelectionCriteria (firstNameExpression); query.setArgument ( "firstName"); // for Query Vector V = New Vector (); v.addeElement ("sarah"); Employee E = (Employee) session.excuteQuery (Query, V);
2) Nested parametric expression example
ExpressionBuilder EMP = New ExpressionBuilder (); Expression AddressExpression; // Generate Nested Parameterized Expression AddressExpression = Emp.Get ("Address"). Equal (Emp.getParameter ("Employee). GET ("Address"). get ("city")); // Nested expression readObjectQuery query = new readObjectQuery (Employee.class); Query.SetSelection; Query.Addargument ("EMPLOYEEE"); / /? // Set the parameters, execute the query vector v = new vector (); v.addelement (employee); Employee E = (Employee) session.excuteQuery (query, v);