Tutorials to .com

Tutorials to .com » Jsp » Application » MAGICIAN IS CASTOR (Castor JDO, Castor XML, magic)

MAGICIAN IS CASTOR (Castor JDO, Castor XML, magic)

Print View , by: iSee ,Total views: 20 ,Word Count: 5502 ,Date: Mon, 24 Aug 2009 Time: 4:08 PM



























MAGICIAN
IS CASTOR
(Castor
JDO, Castor XML, magic)




Author: Le Dawn airwalker@263.net
(2002/3/18)






INDEX



1.INTRO

2.MAGICAL TOUCHING OF <CASTOR-JDO>

3.MAGICAL TOUCHING OF <CASTOR-XML>


4.MAPPING

5.CONCLUSION




1.
INTRO




1.1
Before Intro



Middleware-based system, operational systems are generally managed by database management systems (relational databases, object-oriented databases, etc.), data transaction processing systems (Enterprise
JavaBean), as well as the customer is responsible for the performance of layers (JSP, Servlet-based WEB) these three composition; business processes, xml will be the long-range data communications format.



The actual process of operation are:



Database management system, is responsible for storing all of the business logic of permanent data, its type is probably the most popular relational databases or object-oriented database, or other. Data transaction processing systems (such as EJB) in accordance with its customer's business requirements call for read data from the database management system, and convert it into a working format of the management system (for example, converted to OO objects) according to certain rules in the affairs of business process, the result may be to convert the object back into the database, or some object or data transmission to its customers; attention to this client may be responsible for the performance of the system's customers tier (JSP, Servlet-based WEB), also application may be remote, of course, may also be other data transaction processing system. Customer is responsible for the performance of layers (JSP, Servlet-based WEB), is responsible for customer's WEB page delivery, it can obtain from the data transaction processing system, the object is converted to xml data, and then further use of XSL generate HTML pages passed WEB customers; it also xml data may be transmitted directly to customers, by the customer is responsible to convert into other formats (HTML, object)










Attention to the following process:



1. Data transaction processing systems (such as EJB) from the database management system query data into system-specific class object to carry out business processes. System needs to database logic (such as relational databases) In accordance with a set of mapping rules defined classes,
And put on the database query to obtain data that populate the classes generated objects. Most of the number of terms, the relational database tables as well as the description logic is not very complicated, similar to the structure of programming languages (including a number of data attributes).




2. Data transaction processing systems (such as EJB) class object will be converted into the database through the query against the data back into database management system. With the previous approach is similar to reverse the course of his or her)



3. Customer is responsible for the performance of layers (JSP, Servlet-based on the WEB) or a data transaction processing system in the business process Object will be converted into XML data as a transmission medium. System must define a mapping rule for the conversion, in particular, had to deal with object-oriented inheritance and combination rules of the XML mapping description.




4. Client application to access the XML data back into objects, to make business processes. Is similar with the previous approach, is a reverse process)



These processes are summarized by the following diagram:






This process exists in almost any system, data carrier mapping rules in a new business systems are likely to be re-defined. We can use this mapping rule is reflected in the code, of course, can also be placed in a script file. The latter will have more access to reuse and ease of use. This is the back is a reusable module.
Can database data, OO class objects, XML that three of today's most widely used form of data carrier exchange between the maps to establish a common mapping rules to form a reusable framework, or even to reach the standard will greatly improve business development more efficient.




1.2
Preface Of Castor



Castor is a ExoLab Group following an open-source project, its major achievement is the O / R mapping capabilities. It is the main API and data interface: JDO-like,
SQL, OQL, JDBC, LDAP, XML, DSML. It supports distributed transaction processing and time-directory; to provide processing XML, Directory, XADirectory class library, providing classes from XML to java conversion mechanism.




Castor the above mentioned mapping data carrier format conversion rules Dacheng an application framework:

Castor JDO responsible for the database mapping of data and OO Object conversion.

Castor XML for XML data and OO Object mapping transformation. marshaller, and is responsible unmarshaller between objects and XML data conversion.




Castor
JDO (Java Data Objcet)



Realize that the O / R mapping, it is the most important functions can directly object-oriented class object to read from a relational database or permanent access. JDO users can can query SQL relational database, as well as transparent, this will be JDO of the O / R mapping do it. Mode of operation described as follows:





Castor
XML



Realize that the XML and OO Class / Object mapping, we can think this is a separate Framework, specifically responsible for dealing with XML. We need to define the class of information permanently saved (using XML), according to this idea, in the Castor in the XML processing, the completion of two independent functional blocks: a charge of class to the XML, save and load, The other one is responsible for object to XML, save and load.


Concrete construction that we can see following diagram:






Described above diagram, marshaller, and unmarshaller objects and XML documents for inter-conversion. This conversion process is mainly completed at the server side. In operation, the client sends the request to the server side, the pairs of corresponding objects in memory operations, and then disposed of the object marshall into XML file or XML file format for further HTML files are sent to the client. If the XML file format to send to the client, then the client-side implementation of unmarshall operation will restore the XML document object in memory, thus completed the process of interaction.
Castor's own source generator tool can automatically generate the corresponding XML description file of the java code, and it automatically generates the code and BEAN style is very similar, it is easy to understand.



The overall operating framework described in the following figure:






The following detailed description of the Castor JDO and Castor XML application method.











2.
MAGICAL TOUCHING OF <CASTOR-JDO>



2.1
Open A JDO Database (Open a JDO database)




CatorJDO supports two types of environments, client applications and j2ee servers. Client applications responsible for setting up database connection and manage transaction processing. J2EE application uses JNDI to obtain pre-configured database connection, and the use of UserTransaction or CMT (container
managed transaction) to manage the transaction.



2.1.1 Client Application



Client applications responsible for setting up database connection and manage transaction processing. Set up the database through a mapping file connected with a separate XML file to achieve. (Usually database.xml, but we can use any name)
org.exolab.castor.jdo.JDO defines the database name and attributes, and is used to open a database connection. Through the use of setConfiguration command to specify the configuration file URL, we can load the database configuration. Use the same JDO object configured to create multiple configuration files will be loaded once.
org.exolab.castor.jdo.Database object implements an open connection to the database. In the definition, database object is not the process safe, and therefore should not be used for concurrent processes. When you open multiple Database objects there will be a little overhead, and the JDBC connection is only open only in a transaction to get (back).



The following code describes how the film opens in the Client App off a JDO database:



JDO jdo;

Database db;




/ / Define JDO Objects

jdo = new JDO ();

jdo.setDatabaseName ( "mydb");

jdo.setConfiguration ( "database.xml");

jdo.setClassLoader (getClass (). getClassLoader ());




/ / Get a new database

db = jdo.getDatabase ();




/ / Start the transaction

db.begin ();




/ / Business Process

...

/ / Commit the transaction, close database

db.commit ();

db.close ();






2.1.2 J2EE program



We will use the J2EE container data carrier Castor mapping work, J2EE programs rely on the J2EE container (including the Servlet, JSP, EJB, etc) to set up a database connection, and use JNDI to find it. This working model allows the container to configure a work center personnel to set the database properties, and let the J2EE container can span multiple data (library) source to manage the division-style DB transaction. J2EE procedure JNDI name space to find the data source. JDO object specification proposed to set the java: comp / env / jdo name space, so similar to JDBC data source named.






The following piece of code to use JNDI to find a database, and use the UserTransaction to carry out transaction management:




InitialContext ctx;

UserTransaction ut;

Database db;




/ / Find the database in the JNDI

ctx = new InitialContext ();

db = (Database) ctx.lookup ( "java: comp / env / jdo / mydb");




/ / Start the transaction

ut = (UserTransaction) ctx.lookup ( "java: comp / UserTransaction"
);

ut.begin ();




/ / Business Process

...




/ / Commit the transaction, close database

ut.commit ();

db.close ();




When the transaction is managed by the container when, for example, EJB (especially the entity Bean [entity bean]), then there is no need for a clear start doing transaction with the commit operation (begin / commite). Application
Server is responsible for real-time data update.




The following code piece describes the process by the container-managed transactions:




InitialContext ctx;

UserTransaction ut;

Database db;




/ / Lookup databse in JNDI




ctx = new InitialContext ();

db = (Database) ctx.lookup ( "java: comp / env / jdo / mydb");




/ / Do something

...




/ / Close the database

db.close ();




Summary: Open a database client and the server side there is a difference, mainly reflected in the transaction process, and configuration issues such as the above, need attention.



2.2
JDO Transaction (JDO transaction processing)




2.2.1 temporary objects and persistent objects



All the JDO operations occur in the transaction. When a transaction is committed, JDO data from the database is loaded into memory in the object, so that the application to operate on the object, and then the object's new state is stored into the database. All the objects can be two kinds of states: temporary or permanent.



Provisional: The submission refers to when the transaction will not be saved when the state of the object. Modifications to the object will not be reflected in the database.


Persistent: The submission refers to when the transaction will be saved when the state of the object. The object changes will be reflected in the database.




In the following cases, the object is persistent:

1, a query result.

2, use the create (java.lang.Object) or update (java.lang.Object) to add it to the database.



All is not a persistent object is a temporary object, when the transaction commits or rolls back, all persistent objects into a temporary object. (Because the object and the database has been out of the same correlation of) the client program, use the begin (), commit () and rollback () to manage the transaction. In the J2EE program, JDO rely on container to implicitly or explicitly (based on the bean's transaction attribute values) the use of javax.transaction. UserTransaction interface to manage the affairs.
If a persistent object in the transaction was changed to modify the submission will be stored in the database. If the transaction rollback, the database will not be changed. If you want two different transactions using the same object, you must re-query.
Temporary and persistent objects are from the firm's point of view of the database. Generally speaking, only for a persistent object database, if in another database called isPersistent (java.lang.Object) will return false. Of course, you can make a persistent object that corresponds to two databases, for example, run a query in a database, but in another database, create it.





1.2.2 OQLQuery (Object Query Language)




OQL queries are used to from the database queries and search for the object. OQL queries and SQL queries is similar, but using the object name rather than the SQL name, and do not need to join clause. For example, if the type of object is TestObject loaded, OQL query will use the "FROM
TestObject ", regardless of why the table name in the end. If you load an object associated with the need join, Castor will automatically connect.




The following piece of code using the OQL query to load all of the objects in the specified group. Note that products and groups are associated with an object, JDBC query using the connection:




OQLQuery oql;

QueryResults results;




/ / Construct a new query query and bind parameters

oql = db.getOQLQuery ( "SELECT p FROM Product p WHERE Group = $"
);

oql.bind (groupId);




/ / Loop through results and print it

results = oql.execute ();

while (results.hasMore ()) (

System.out.println (results.next ());

)




The following piece of code using the previous query to get the product, their prices will be reduced by 25%, and deposit them in the database will

(In this case using the client-side procedural matters):




while (results.hasMore ()) (

Product prod;

prod = (Product) results.next ();

prod.markDown (0.25);

prod.setOnSale (true);

)




/ / Commit the transaction and close the end of the

db.commit ();

db.close ();




As described above, the query in three steps to achieve:

First, the use of OQL syntax to create a query from the database object;

Second, if there are parameters, it will be bound to the query parameter. Parameter binding order and that they appear in the order of OQL syntax consistent;

Third, the implementation of the query and access to a range of org.exolab.castor.jdo.QueryResults objects.




Inquiries can be created once and used multiple times, but each time had to re-binding parameters. Query is executed again after the last result set can continue to use.




Refer to the following code to call the stored procedure:

oql = db. getOQLQuery ( "CALL sp_something ($) AS myapp.Product"
);

Here sp_something is a return of one or more of the stored procedure result set, field order, and in front of the same (for there is no object associated with the order as follows: identifier, followed by all other fields, in order and mapping.xml the definition of the order of the same).


Create / remove / update

create (java.lang.Object) method in the database to create a new object, or JDO terminology in making a temporary object persistence. If the transaction is committed, then use the create method to create this object will be maintained in a database, or they will be deleted. In a database that already exists if you are using to create the identity object, will generate an exception.




The following piece of code for the previous query out of a group creates a new product:




Product prod;



/ / Create a Product object

prod = new Product ();

prod.setSku (5678);

prod.setName ( "Plastic Chair");

prod.setPrice (55.0);

prod.setGroup (furnitures);




/ / In the database to create a new object (persistent)

db.create (prod);




/ / Create a Product object

prod = new Product ();

prod.setSku (5678);

prod.setName ( "Plastic Chair");

prod.setPrice (55.0);

prod.setGroup (furnitures);




/ / In the database to create a new object (persistent)

db.create (prod);




remove (java.lang.Object) method have the opposite effect, it will delete a persistent object. Once deleted, any business can use the object. If the transaction is committed, this object from the database, delete, otherwise the object will still exist in the database. If you try to delete a non-persistent object, will generate an exception.








2.3
JDO Mapping (JDO mapping)



Castor JDO user must define a mapping description file to define the O / R mapping, Castor as an XML file, and defines the mapping of the dtd / schema. Due to space limitations, the user can download the details from the next inspection.




Castor Mapping DTD: http://castor.exolab.org/mapping.dtd

Castor Mapping XML Schema: http://castor.exolab.org/mapping.xsd




Which contains Castor JDO O / R mapping rules, and Castor XML mapping rules.














3.
MAGICAL TOUCHING OF <CASTOR-XML>




OO allows the use of Castor XML objects and XML / Schema mapping data interchange. Users must be in accordance Castor Mapping Schema mapping rules define a mapping. (Achieve mapping.xml middle). The conversion process by org.exolab.castor.xml.Marshaller class.




3.1
Marshaller

Identity mapping user-defined rules mapping.xml, then org.exolab.castor.mapping. Mapping of its load,

He then org.exolab.castor.xml. Marshaller will Mapping load. Goes on, the user can use the Marshaller. Marshal (
..) Method to do the mapping Object to XML data transformation.




The following code to indicate the configuration of the Marshaller and several mapping process:




Marshaller marshaller;

Mapping _mapping;

PrintWriter writer;

MyClass myObject;

... ... ... ... ... ...

/ / Create a marshaller object and load Mapping Mapping Rules

marshaller = new Marshaller (writer);

marshaller.setMapping (_mapping);




/ / OO object to XML data (can be a stream, DOM node tree, or SAX traversal handles)
Mapping transformation

java.io.Writer out;

marshaller.marshal (myObject, out);




org.w3c.dom.Node node

marshaller.marshal (myObject, node);




org.xml.sax.DocumentHandler handler

marshaller.marshal (myObject, handler);

... ... ... ... ... ...



Castor XML operating framework as shown below:








3.2
XML Mapping (XML Mapping Definition)



Castor XML user must define a mapping description file to define the OO Object to XML mapping, Castor as an XML file, and defines the mapping of the dtd / schema. Due to space limitations, the user can download the details from the next inspection.




Castor Mapping DTD: http://castor.exolab.org/mapping.dtd

Castor Mapping XML Schema: http://castor.exolab.org/mapping.xsd




Which contains Castor JDO O / R mapping rules, and Castor XML mapping rules.













4.
MAPPING



4.1
Castor Mapping



Castor Mapping DTD: http://castor.exolab.org/mapping.dtd

Castor Mapping XML Schema: http://castor.exolab.org/mapping.xsd



Which contains Castor JDO O / R mapping rules, and Castor XML mapping rules.




The following is a section of Castor Mapping of the mapping.xml indicate the code




<mapping>

- "! --

Mapping for ProductGroup

- "

<class name = "myapp.ProductGroup" identity = "id"
access = "shared">

<description> Product group </ description>

<map-to table="prod_group" xml="group" />

<field name = "id" type = "integer" required = "false"
direct = "false" lazy = "false">

<sql name = "id" type = "integer" dirty = "check"
/ "

<xml node="attribute" />

</ field>

<field name = "name" type = "string" required = "false"
direct = "false" lazy = "false">

<sql name = "name" type = "char" dirty = "check"
/ "

<xml node="text" />

</ field>

</ class>

... ... ... ... ... ... ... ..

</ mapping>




Note: Each set of a Class object relational database mapping for a table, the table in the field for the Class of attribute sets, the mapping table, need to identify each attribute of the Class name, SQL field name, XML elements or words segment name, and their respective categories. Castor
Mapping support the types of inheritance, as well as a combination of object-relations, detailed syntax see Castor Mapping DTD / Castor Mapping
XML Schema.




4.2
JDO Configuration




The following is a JDO Configuration section of the database.xml hint code:




<! DOCTYPE databases (View Source for full doctype ...)>


<database name="test" engine="sybase">

<data-source class-name="com.sybase.jdbc2.jdbc.SybDataSource">

<params user = "myname" password = "secret" port-number = "4100"
server-name = "airwalker" />

</ data-source>

<mapping href="mapping.xml" />

</ database>




Note: database of the engine attribute identifies the Castor JDO to support a database maker, marked as follows:



generic Generic JDBC engine

oracle Oracle 7 and Oracle 8

sybase Sybase 11 and SQL Anywhere

sql-server Microsoft SQL Server

postgresql PostgreSQL 6.5 and 7



data-source for class-name attribute identifies the JDBC implementation class, and then identifies some of the elements and their attributes to access the database entry. mapping of the href attribute identifies the Castor
Mapping mapping XML file (usually named mapping.xml)












5. CONCLUSION




Castor framework sets out a close with the standard O / R mapping rules and implementation, it allows customers to more convenient framework for the realization of system data carrier format conversion, in a relational database, object-oriented language, and XML Road Daxing its industry, Castor framework allows customers to do the code type of data without padding, as well as JDBC, SQL queries transparent, the user Castor
The familiar framework of the process to take full advantage of their superiority in all aspects to get the development efficiency.




Castor is a very strong framework for reuse, but its original JDO mapping mechanism to generate a graphical interface to be the mapping editor.




1, and the relationship between the SUN JDO




Castor JDO and the SUN JDO is not the same, Castor JDO is independent of the SUN JDO developed, but they are very similar to the work done. Although it can be Castor
JDO back into the SUN JDO that custom look, but due to the locking strategy of the two fundamental differences exist, and is not worth doing. Internally, Castor JDO for all affairs of the durability of each activity to only one target to maintain a lock (and cache) backup. In the SUN in the right of each transaction for each persistent object of maintaining a backup, and SUN
JDO also need to byte-code editor, but CastorJDO do not need to. Castor also provides some other features, such as key generators, long transaction
support as well as the OQL query, these are in the SUN's JSR is invisible. Therefore, we evaluated the Castor's JDO-LIKE.




2, and the relationship between the EJB




An entity Bean can manage its own persistence. It is either called BMP (Bean Managed Persistence) to manage its own persistence, or to rely on its container CMP (Container
Managed Persistence) to be persistent.




For the BMP, the one entity BEAN can use Castor JDO as its persistence mechanism, you can also use other mechanisms, such as direct calls to JDBC.




For the CMP, the EJB Container, they can be one of the CMP to achieve based on Castor JDO, then Castor
JDO will be used to physical BEAN persistence. If developers need to EJB's life-cycle management,, "write once deploy anywhere"
And other advantages of distributed applications, it is recommended that direct use of EJB. Otherwise, the use of Castor will be more simple, but also open source, cost less and provide more design freely shaped, as well as provide an integrated XML support. We should choose according to the actual situation, the mechanisms used.













#
Reference

Castor's official description document.






Le Morning (2002/3/18)











Java Application / Other Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.