Third, the existing O/R mapping product introduction

Source: Internet
Author: User
Tags abstract define bind commit integer mysql object model xmlns
Connected)



The specific process is as follows:

(1) First set up the database configuration file, we are designated here as Database.xml, of course, can also be changed to other names.

<?xml version= "1.0" encoding= "gb2312"?>

<database name= "Customerdemo" engine= "MySQL" >

<driver url= "Jdbc:mysql://cwb:3306/quickstart" class-name= "Org.gjt.mm.mysql.Driver" >

<param name= "user" value= "dbusername"/>

<param name= "Password" value= "Dbpassword"/>

</driver>

<mapping href= "Customer.xml"/>

</database>




Create a mapping file Customer.xml

<?xml version= "1.0" encoding= "gb2312"?>

<class name= "Demo.customer" access= "shared" identity= "CustomerID" >

<map-to table= "Users"/>

<field name= "CustomerID" type= "integer" >

<sql name= "CustomerID" type= "integer"/>

</field>

<field name= "Name" type= "string" >

<sql name= "name" type= "varchar"/>

</field>

</class>




Create a persistent class that is similar to the Hibernate JavaBean class

Package Demo;

public class Customer {

private String name;

private int CustomerID;

Public Customer () {

}



public int Getcustomerid () {

return CustomerID;

}



public void Setcustomerid (int customerID) {

This.customerid = CustomerID;

}



Public String GetName () {

return name;

}



public void SetName (String name) {

THIS.name = name;

}

}




After the basic implementation, we can see how this demo works.

Import java.util.*;

Import org.exolab.castor.jdo.*;

Import java.net.*;



public class Customermanager {

JDO JDO;

Database DB;

Public Customermanager () throws Databasenotfoundexception,

persistenceexception {



Define a Jdo Object

JDO = new Jdo ();

Jdo.setdatabasename ("Customerdemo");

Jdo.setconfiguration ("Database.xml");

Jdo.setclassloader (GetClass (). getClassLoader ());



Get Connection Database

db = Jdo.getdatabase ();

}

/**

* Used to read users

* @param ID The primary key of the Customer object

*/

Public Customer Loadcustomer (Integer id) throws Databasenotfoundexception,

persistenceexception {



Customer result = null;



Start a transaction

Db.begin ();

result = (Customer) db.load (Customer.class, id);



Complete transaction, close database

Db.commit ();

Db.close ();

return result;

}



/**

* Used to create user

* @param Customer Newcustomer New Object

*/

public void CreateCustomer (Customer newcustomer) throws

Databasenotfoundexception,

persistenceexception {


Customer result = null;

Db.begin ();



New Customer

Db.create (Newcustomer);



Db.commit ();

Db.close ();

}



/**

* Update Old objects

*/

Public Customer UpdateCustomer (customer UpdateCustomer) throws

Databasenotfoundexception,

persistenceexception {



Db.begin ();



Update Customer

Db.update (UpdateCustomer);



Db.commit ();

Db.close ();

return null;

}



public void Removecustomer (Customer removecustomer) throws

Databasenotfoundexception,

persistenceexception {



Db.begin ();



Delete Customer

Db.remove (Removecustomer);



Db.commit ();

Db.close ();

}

}




Executing queries on the Castor JDO object model

Castor implements a subset of the ODMG 3.0 specification of the Object Query Language (OQL). OQL syntax is similar to SQL syntax, but it allows you to query the object model rather than querying the database directly. This can be a powerful feature when multiple databases are supported. The OQL implementation of Castor transforms the OQL query internally into the appropriate SQL for the database. Use the bind () method to bind the parameters to the query. Here are some simple examples of OQL queries.

Castor's OQL implementation does not continue to use fully qualified object names throughout the query, instead it supports the use of object aliases. In the following queries, C is an alias like this.

If you want to query to find all of the Customer, you can execute the following query:

SELECT C from Demo.customer C

If you want to query to find the customer with the identity equal to 1234, you can:

SELECT c from Demo.customer C WHERE c.customerid= $

Start with, followed by:

Query.bind (1234)



To query for a Customer with a name similar to a special string, you can execute the following query:

SELECT c from Demo.customer C WHERE c.name like $

Followed by:

Query.bind ("%abcd%")






3, ObjectSpaces

ObjectSpaces is the R/O Mapping below Microsoft. Net, so far it is a beta version, I believe it will appear in Vs.net 2004 official version. NET under the R/O mapping is not as prosperous as Java, open source is not much, OJB. Net, Atomsframework, OPF. NET, and so on, are quite well-known, but still in the mature. Ado. NET function is powerful, and JDBC has a lot of different places, so. NET under the O ' mapping has a lot of its own characteristics.



Now a simple introduction to the use of objectspaces, you can compare with Hibernate and JDO.



ObjectSpaces also has a configuration Source.xml file:

<sources xmlns= "Http://www.microsoft.com/ObjectSpaces-v1" >



<!-the configuration of the data connection-->

<source name= "Demo" adapter= "SQL" connection= "Data source=localhost;" Integrated SECURITY=SSPI; Database=customerdemo "/>

</sources>




Each persisted class also has a corresponding map.xml:

<map xmlns= "Http://www.microsoft.com/ObjectSpaces-v1" >

<type name= "Customer" datasource= "Customer" >

<property name= "CustomerID" datasource= "CustomerID"/>

<property name= "name" datasource= "CustomerName"/>

</type>
</map>




We have hibernate the above example, I believe it is easy to understand this section of XML, many of which are similar. Similarly, a persistent class is required:

Public abstract class Customer

{

To define a primary key

[UniqueId] public abstract int CustomerID {get; set;}

Also define attributes

Public abstract string Name {get; set;}

public void OnCreate (int newId)

{

CustomerID = newId;

}

}




Examples of Use:

Mount Source.xml, build ObjectSpace factory

Iobjectspace OS = objectspacefactory.createobjectspace ("Source.xml");



Create a new customer

Customer Thecustomer = (customer) OS. CreateObject (typeof (Customer), "1");

Thecustomer.name = "Karl";



Save the new customer

Os. UpdateAll ();




If you need to save a persisted class with a database, the wording is somewhat different:

Establish connection

String ConnectionString = "Data source=localhost;integrated security=sspi;initial catalog=customerdemo;";

SqlConnection Connection = new SqlConnection (ConnectionString);

SqlDataAdapter thesqldataadapter = new SqlDataAdapter ("select * from Customer")

Connection);

DataSet ds = new DataSet ("Customer");

Thesqldataadapter.fill (ds, "Customer");

Create a DataSpace instance

DataSpace thedataspace = new DataSpace ("Map.xml", DS);

The name from DataSpace is the customer of "Karl".

Customer Thecustomer = (customer) Thedataspace.getobject (typeof (Customer), "Name= ' Karl '");



Modify Name

Thecustomer.name = "Little Karl";




The above simple introduction of Hibernate, JDO and the use of objectspaces, to more in-depth attention, it should be a good study of their own.





(Next chapter "IV, my first edition of O ' Mapping Introduction")

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.