Third, the existing O/R mapping product introduction
O/R mapping has been a long time, a lot of products: Java has hibernate, JDO and so on. Net has objectspaces, grove.net, Ojb.net, Atomsframework, Objectz.net, OPF. NET and so on.
1, Hibernate
Hibernate is a Java open source O/R Mapping, which has a lightweight object encapsulation for JDBC, and is very flexible to manipulate the database with object-programmed thinking. Now take a simple demo to see how the Hibernate is used:
First hibernate need a hibernate.cfg.xml configuration file
<?xml version= ' 1.0 ' encoding= ' utf-8 '?>
<! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration dtd//en" "http:// Hibernate.sourceforge.net/hibernate-configuration-2.0.dtd ">
<!-The primary key generation rule, "increment" means that the primary key is incremented, and there are more than 10 primary key generation methods Hibernate-->
<generator class= "Increment" >
</generator>
</id>
<!-defines the properties of a class-->
<property name= "Name" >
<!-The fields that are mapped, you can see that the field names of the tables can be completely different from the class property names-->
Customer.xml defines many of the class's Properties and table fields, and if anything changes, just change the XML file. Hibernate definition mapping more flexible, property can also not define the contents of the property, with the default form.
In addition to this XML file, there is also a persistence class: Customer
Package ormappingdemo.hibernate;
public class Customer {
private int CustomerID;
private String name;
Public Customer () {
}
All properties are accessed through the GET, set method
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;
}
}
The customer is a "thin" class, and the persistence class does not need to implement any special interfaces or inherit from a particular persistent parent class. Up to now, a customer-mapped persistence class work is done, and this part of the work can be done automatically through the tool of a hibernate schema builder. Now, we can use this class:
Set up a session factory
Sessionfactory sessionfactory =
New Configuration (). Configure (). Buildsessionfactory ();
Open a session
Session session = Sessionfactory.opensession ();
Start a transaction
Transaction tx = Session.begintransaction ();
Initializes a persistent class
Customer Thecustomer = new Customer ();
assigning values
Thecustomer.setname ("Karl");
Save the new persisted class
Session.save (Thecustomer);
Commit a transaction
Tx.commit ();
Close session
Session. Close ();
This code, you do not see any traditional and database dealing with the code, only need to use the persistence of classes and a few of the factory class, you can achieve full functionality. Hibernate also has an extremely powerful query language hql that looks like SQL. But the hql is completely object-oriented.
Transaction tx = Session.begintransaction ();
Query through the HQL language. Note that the HQL here are objects and attributes, not tables and fields. If name is a property of the persisted class, the name of the table field that is actually mapped is CustomerName
Query query = Session.createquery ("Select Name from Customer as customer where Customer.customerid>=:id");
Query.setinteger ("ID", 1);
Iterating through the customer with iterations
for (Iterator it = query.iterate (); It.hasnext ();) {
The Hibernate function is extremely powerful, the structure is reasonable, and completely develops the source code, does not need license. Whether you are using Java or not, a careful study of hibernate is of great benefit to learning and developing O/R mapping.
2. Castor JDO
Castor JDO (Java data Objects) is also an open source, 100% Java Data binding framework.
Castor JDO was first published in December 1999 as one of the most open source data binding frameworks available. Since then, the technology has developed considerably. Castor JDO is now often used in conjunction with many other technologies, both open source and commercial, to bind the Java object model to relational databases, XML documents, and LDAP directories.
The same section is also a demo to talk about Castor JDO,
Castor JDO, called a Java data object, also uses a class similar to JavaBean to store the writings and represent the relationship between the data.
The application is primarily responsible for setting up database connections and managing transactions. The database settings are implemented through a separate XML file that is connected to the mapping file.
Look below, like Hibernate's Sessionfactory, Castor JDO also encapsulates a class that handles database resources. is Org.exolab.castor.jdo.JDO, which defines the name and attributes of the database and is used to open a database connection.
We can specify the configuration file URL by using the Setconfiguration command, and we can mount the configuration of the database. Using the same configuration to create multiple JDO objects only mounts the configuration file once.
The following code shows the process of opening a database, doing a transaction, and then shutting down the database.
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.