Magician Is Castor (CaStor JDO, Castor XML, Magic)

zhaozj2021-02-17  98

.p9 {font-family: "Song"; font-size: 10pt; line-height: 130%} ->

MAGICIAN IS CASTOR (Castor JDO, Castor XML, Magic) Author: Le dawn airwalker@263.net (2002/3/18) INDEX1.INTRO2.MAGICAL TOUCHING OF

3.magical Touching of

4.MApping5.conclusion 1. Intro 1.1 Before INTRO Based on the business system of the middleware system, the database management system (related database, object-oriented database, etc.), data transaction system (Enterprise Javabean), and customer performance is responsible ( JSP, servlet- web) This three aspects; during business, XML will be the format of remote data communication. Its specific operation process is: Database management system, responsible for storing all permanent data of the entire business logic, which may be the most popular relational database, or object-oriented databases or others. Data transaction processing system (such as EJB) is responsible for reading data from the database management system based on its customer's business requirements, and converting it into the work format of the management system (such as converting into an OO object) according to certain transaction rules. Business processing, the result may be to re-deposit the converted object into the database, or passed some objects or data to its customers; note that this customer may be the customer's customer performance responsibility layer (JSP, servlet- web), also It may be a remote application, of course, may also be another data transaction processing system. Customer Performance Layer (JSP, Servlet- WEB) You may pass the XML data directly to the customer, and the customer is responsible for converting it into other formats (HTML, objects) to pay attention to the following processes: 1. Data transaction system (such as EJB) converts data from the database management system System-specific class objects for business processing. The system needs to define a class based on a set of mapping rules according to a set of mapping rules, and generate an object to the data fills obtained by the database query. Most of the number, the relational database is not very complex, similar to the structure in the programming language (including several data properties). 2. Data Transaction System (such as EJB) Convert the class object to the database data inverted into the database management system through query statements. The processing method is similar to the upper, which can be considered as a reverse process) 3. Customer performance responsibility layer (JSP, servlet-based web) or data transaction processing system converts Object into XML data as a transmission medium during business. The system is to define a mapping rule for this conversion, and specially handle the XML mapping description of object-oriented inheritance and combined rules. 4. The client application re-converts the acquired XML data into objects and works. The processing method is similar, and it is an inverse process) These processes are summarized by the following figure: This process is almost in any system, and the data carrier mapping rules may be redefined in a new business system. We can reflect this mapping rule in the code, of course, can also be placed in a script file. The latter acquisition will have greater reuse and ease of use. This is a multiplexed module that will be back. Can I establish a common mapping rule between the three today's application of the database data, OO object, and XML, and the most widely used data carrier format, form a reuse framework, even standard, will greatly improve the business. Development efficiency. 1.2 Preface of Castorcastor is an item of an open source under Exolab Group, which is mainly implemented by O / R mapping.

Its main API and data interface are: JDO-LIKE, SQL, OQL, JDBC, LDAP, XML, DSML. It supports distributed directory transaction processing and time; providing processing XML, Directory, XADirectory class libraries, providing a conversion mechanism from XML to Java classes. Cast portred the mapping conversion rules of the data carrier format mentioned above: CaStor JDO is responsible for database data and OO Object mapping conversion. The Castor XML is responsible for the mapping conversion of XML data to OO Object. Marshaller and UNMARSHALLER are responsible for the conversion between objects and XML data. The CASTOR JDO (Java Data Objcet) implemented an O / R mapping, and its most important functionality can directly read or permanently access the object-oriented class object from the relational database. JDO users can fully query the relational database and SQL query, which will be mapped by JDO's O / R. The working mode is described below: CaStor XML implementation is a map of XML and OO Class / Object, we can think this is a separate Framework, dedicated to dealing with XML. We need to save the defined information of the class (using XML), according to this idea, in XML processing in Castor, complete two independent function blocks: a piece of saving and loading of XML, A piece of saving and loading of an object to XML. Specific constructs We can see the figure below: As described above, Marshaller and Unmarshaller are responsible for the transition between objects and XML files. This conversion process is mainly completed at the server side. In the operation, the client's request is sent to the server side, and the objects in the memory are operated accordingly, and then the processing of object Marshall is the XML file, or further XML files formatted to the client to the client. If you send it to the client in an XML file format, then restore the XML file into memory in the client, which is completed in the memory. The Source Generator tool comes with the CASTOR can automatically generate the corresponding Java code according to the XML description file. It automatically generated code and the style of Bean are very similar, which is easy to understand. The overall running frame is as follows: Application method of CaStor JDO and CASTOR XML will be described in detail below. 2. Magical Touching of

2.1 Open A JDO Database (Open a JDO Database) Catorjdo supports two types of environments, client applications, and J2EE servers. The client application is responsible for setting up database connections and managing transactions. The J2EE application uses JNDI to get a pre-configured database connection and use the useertransaction or CMT (Container Management Transaction) to manage transactions. 2.1.1 Client Applications Client Applications are responsible for setting up database connections and management transactions. The setting of the database is implemented by a separate XML file connected to the mapping file. (Generally Database.xml, but we can use any name) org.exolab.castor.jdo.jdo defines the name and property of the database and is used to open a database connection. Specify the configuration file URL by using the setConfiguration command, we can load the configuration of the database. Use the same configuration to create multiple JDO objects only load the configuration file. Org.exolab.castor.jdo.database objects implements an open connection to the database. In the definition, the Database object is not a process secure, so it should not be used in concurrent processes. When a plurality of Database objects are open, there will be a little overhead, and the JDBC connection can only be obtained in one open transaction (background). The following codeD describes how to open a JDO database in the Client App: JDO JDO; Database DB; // Defines JDO object JDO = New JDO (); jdo.setdatabaseename ("MYDB"); JDo.setConfiguration ("Database. XML "); jdo.setclassloader (GetClassLoader ()); // Get a new database DB = jdo.getdatabase (); // Start transaction processing db.begin (); // transaction process. // Submit a transaction, turn off the database db.commit (); db.close (); 2.1.2 J2EE program We will use CASTOR to map the data carrier in the J2EE container, the J2EE program depends on the J2EE container (including servlet, JSP, EJB, ETC, to set up a database connection, and use JNDI to find it. This work model allows container configurations to set database properties in a work center and allow J2EE containers to manage part-style DB transactions across multiple data (library) sources. The J2EE program found this data source via JNDI namespace. Specification recommends setting the JDO object in the Java: Comp / Env / JDO namespace to name the JDBC data source.

The following cleaner uses JNDI to find a database, and use userTransAction to perform transaction management: InitialContext CTX; usertransaction Ut; Database DB; // Find database CTX = new initialContext () in JNDI (); db = (data) CTX.lookup ("Java: Comp / Env / JDO / MYDB"); // Start transaction processing UT = (UsertransAction) CTX.lookup ("Java: Comp / UsertransAction"); ut.begin (); // transaction process. // Submit the transaction, turn off the database ut.commit (); db.close (); When the transaction is managed by the container, such as EJB (especially entity bean [entity bean]), this is not necessary to clearly do business BEGIN / Commit. Application Server will be responsible for data updates in real time. The following codes describe the transaction process managed by the container: InitialContext CTX; userTransaction Ut; Database DB; // Lookup Database In JNDI CTX = New InitialContext (); DB = (Database) CTX.lookup ("Java: Comp / ENV / JDO / MYDB "); //, the database db.close (); Summary: Opening a database is different from the client and the server side, mainly reflecting the process of transaction processing, configuration Question, etc., you need to pay attention. 2.2 JDO Transaction 2.2.1 Temporary objects and persistent objects All JDO operations occur in transactions. When the transaction is submitted, JDO loads data from the database into the memory, allows the application to operate the object, and then store the new status of the object into the database. All objects can be two states: temporary or lasting. Temporary: Deficit the object that is not saved when the status is submitted. The modification of the object does not appear in the database. Sluit: Refers to the object that will be saved when the transaction is submitted. Modifications to objects will be reflected in the database. In the case of the case, the object is lasting: 1, the result of a query. 2, use create (java.lang.object) or update (java.lang.object) to add it to the database. All objects that are not persistent are temporary objects that all persistent objects become temporary objects when transaction is submitted or rolled. (Because the object has been unanimously dependent on the database), use BeGin (), Commit () and Rollback () to manage the transaction. In J2EE programs, JDO relies on Container to manage transactions using Javax.Transaction.Usertransaction interface using Javax.Transaction.UsertransAction interfaces. If a persistent object is changed in transactions, modify it when submitting it. If the transaction rolls back, the database will not be changed. If you want to use the same object in two different transactions, you must re-query. The temporary and persistence of objects are viewed from the perspective of the database to which the transaction belongs. Generally, persistent objects are only for a database, and if ISPERSISTENT (Java.lang.Object) is called in another database, false will be returned.

Of course, a persistent object can also be corresponding to two databases, such as running queries in a database, and create it in another database. 1.2.2 OQLQuery (Object Query Language) OQL query is used to query and find objects from the database. OQL query and SQL query are similar, but use object names rather than the SQL name, and no JOIN clause is required. For example, if the object type TestObject is loaded, the OQL query will use "from testObject", regardless of the end name. If the load associated object requires Join, Castor will automatically connect. The following cleaner uses an OQL query to load all objects in the specified group. Note that the product and group are associated objects, JDBC queries use 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); // Traversing results and prints resultS = oql.execute (); regards.hasmore ()) {system.next ());} The following code The tablets use the previous query to get the product, reduce their prices 25%, and share them in the database (using the client program transaction here): While (results.hasmore ()) {Product product; prod = (Product Results.next (); prod.markdown (0.25); prod.setonsale (true);} // Submit transaction and turn off the end db.commit (); db.close (); as described above, query division three Steps Implementation: First, create a Query object from the database using the OQL syntax; Second, if there is a parameter, bind the parameters to the query. The order of parameter binding and the order in which they appear in the OQL syntax is consistent; three, execute queries and get a series of objects for org.exolab.castor.jdo.QueryResults. After the query is created, you can use multiple times, but you have to rebound each time. After the query is executed again, the last result set can continue to use. Refer to the following code to call the stored procedure: oql = db.getoquery ("Call sp_something ($) as myapp.product"); sp_something is a stored procedure that returns one or more result sets, field order and the same front. For objects that are not associated, the order is: identifier, and next is the same as the order defined in mapping.xml.). Create / Remove / Update Create (java.lang.object) method creates a new object in the database, or a temporary object is allowed to last in JDO Terminology. If the transaction is submitted, this object created using the CREATE method will remain in the database, otherwise it will be deleted. If you use the identity already existing in the database, an exception will be generated.

The following code creates a new product for a group previously queried: Product Prod; // Create a Product object prod = new product (); prod.setsku (5678); prod.setname ("plastic chair); PROD.SETPRICE (55.0); prod.setgroup (furnitures); // Create a new object (persistence) db.create (Prod) in the database; // Create a Product object prod = new product (); prod. Setsku (5678); Prod.setName ("Plastic Chair"); prod.setprice (55.0); prod.setgroup (furnitures); // Create a new object (persistence) DB.CREATE (PROD) in the database The REMOVE (Java.lang.Object) method produces the opposite effect, which will delete a persistent object. Once deleted, any transaction cannot be used. If the transaction is submitted, this object will be deleted from the database, otherwise the object will still exist in the database. If you try to delete a non-persistent object, an exception will be generated. 2.3 JDO MAPPING CASTOR JDO Users need to define a mapping description file to define an O / R mapping, Castor uses it as an XML file and defines mapping DTD / Schema. Limited to the space, the user can download detailed inspection. CASTOR MAPPING DTD: http://castor.exolab.org/mapping.dtdcastor mapping xml schema: http://castor.exolab.org/mapping.xsd contains the CaStor JDO O / R mapping rule and mapping rules for Castor XML . 3. Magical Touching of

The OO object can be mapped to XML / Schema data by using CaStor XML. Users must define a mapping mapping rule according to CASTOR MAPPING Schema. (Implemented in mapping.xml). The conversion process is implemented by the org.exolab.castor.xml.Marshaller class. 3.1 Marshaller User Defines Mapping.xml of the Identifier Mapping Rule, uses org.exolab.castor.mapping.mapping to load it, then use the org.exolab.castor.xml.Marshaller to load mapping. Next, the user can use the Marshaller.Marshal (..) method to make Object to XML data mapping conversion. The following code indicates the configuration of Marshaller and several mapping processes: Mashaller Marshaller; mapping _mapping; printwriter write; 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 flow, DOM tree node, or SAX traversal handle) mapping conversion java.io.writer out; Marshaller.Marshal (MyObject, Out); org. W3c.dom.Node NodeMarshaller.Marshal (MyObject, Node); org.xml.sax.documenthandler Handlermarshaller.Marshal (MyObject, handler); ................ Castor XML Run Frame as shown below: 3.2 xml mapping (XML Mapping Definition) CASTOR XML Users need to define a mapping description file to define an OO Object to XML mapping, and Cast as an XML file and defines mapping DTD / Schema. Limited to the space, the user can download detailed inspection. CASTOR MAPPING DTD: http://castor.exolab.org/mapping.dtdcastor mapping xml schema: http://castor.exolab.org/mapping.xsd contains the CaStor JDO O / R mapping rule and mapping rules for Castor XML . 4. Mapping4.1 Castor mappingcastor maping dtd: http://castor.exolab.org/mapping.dtdcastor mapping XML schema: http://castor.exolab.org/mapping.xsd contains CaStor JDO O / R mapping rules And mapping rules for Castor XML.

The following is a parameter code - Product Group < SQL Name = "ID" type = "integer" dirty = "check" /> ............. ...... Note: Each Class's object set is mapped to a table of the relational database, the field in the table is the class of the class, the mapping table needs to be identified the Class name, SQL field of each property. Name, XML element or field name, and its own category. Castor mapping supports the inheritance of category and the combination of objects, detailed syntax refers to Castor maping DTD / CASTOR MAPPING XML Schema.

4.2 JDO Configuration The following is a Database.xml of JDO Configuration: Note: Database's engine property identifies database vendors supported by Castor JDO, marking as follows: Generic Generic JDBC Engine Oracle Oracle 7 and Oracle 8sybase Sybase 11 and SQL Anywhere SQL-Server Microsoft SQL ServerPostgreSQL PostgreSQL 6.5 and 7Data-Source Class-Name Properties identifies the implementation class of JDBC, then some elements and their properties identify the access portal of the database. Mapping HREF attribute identifies the mapping of Castor Mapping XML files (generally named mapping.xml) 5. The Conclusion Castor framework proposes a near-standard O / R mapping rule and implementation, which makes the framework customers more convenient Implement the transformation of system data carrier format, in relational database, object-oriented language, and XML Daxing industry, Castor framework allows customers to do not need to do code-style data fill, and transparency of JDBC's SQL, users in the Castor framework Familiar with the process, it can take advantage of its exceptions to improve development efficiency. CASTOR is a very strong framework, but its original JDO mapping mechanism needs to generate a map editor for a graphical interface. 1, and Sun JDO's relationship CASTOR JDO and Sun JDO are different. CaStor JDO is developed independently of Sun JDO, but the work they have finished is very similar. Although CaStor JDO can be redefined into Sun JDO, it is not cost-effective due to the fundamental differences between the two in lock strategies. Internally, CaStor JDO is only a backup of a locked (and cache) for each activity of all transactions. In the Sun, each of the persistence objects in each transaction is maintained, and the Sun JDO requires a zodical code editor, and CaStorjdo does not need. The Cast has other features, such as Key Generators, Long Transaction Support, and OQL Query, these are not seen in the JSR of Sun. So our evaluation of CASTOR is JDO-LIKE. 2, and EJB's relationship A entity bean can manage its own persistence. It either calls BMP (Bean Managed Persistence) from management persistence, or on its container's CMP (Container Managed Persistence) for persistence.

转载请注明原文地址:https://www.9cbs.com/read-31473.html

New Post(0)