Java framework --- hibernate (one-to-one) ing, java --- hibernate

Source: Internet
Author: User

Java framework --- hibernate (one-to-one) ing, java --- hibernate

Object/Relation Mapping (ORM) is generated with the development of object-oriented software development methods, it is a technology that aims to solve the mismatch between objects and relational databases. In essence, it is to convert data from one form to another.

Object-Oriented development is the mainstream development method in today's enterprise-level application development environment. Relational databases are the mainstream data storage systems that permanently store data in enterprise-level application environments. Objects and relational data are two forms of business entities. business entities are represented as objects in the memory and relational data in the database. Objects in the memory have associations and inheritance relationships. In the database, relational data cannot directly express many-to-many associations and inheritance relationships. Therefore, the object-relational ing (ORM) system generally exists in the form of middleware, mainly to map program objects to relational database data.

In short, ORM automatically persists the objects in java programs to relational databases by using metadata describing the ing between objects and databases.

When Hibernate implements the ORM function, it mainly uses the following files: ing Class (*. java), ing file (*. hbm. xml) and database configuration file (*. properties /*. cfg. xml). Their Respective functions are as follows.

Ing Class(*. Java): it describes the structure of the database table. Fields in the table are described as attributes in the class. In the future, records in the table can be mapped to objects of this class.

Ing file (*. hbm. xml): It specifies the relationship between database tables and ing classes, includes mappings between ing classes and database tables, between table fields and class attribute types, and between table fields and Class Attribute names.

Database configuration file (*. properties/*. cfg. xml): It specifies the connection information required for database connection, such as the database to be connected, the username to log on to the database, the logon password, and the connection string. Of course, you can also put the address ing information of the ing class here.

A one-to-one relationship between two objects, for example: Person-IdCard instance

There are two policies for one-to-one association ing:

* Primary Key Association: let two objects have the same primary key value to indicate a one-to-one correspondence between them. The database table does not have any additional fields to maintain the relationship between them, it is associated only by the primary key of the table.

Example join of one-to-one unique foreign key Association

Package cn. itcast. I _hbm_oneToOne; public class Person {private Integer id; private String name; private IdCard idCard; public Integer getId () {return id;} public void setId (Integer id) {this. id = id;} public String getName () {return name;} public void setName (String name) {this. name = name;} public IdCard getIdCard () {return idCard;} public void setIdCard (IdCard idCard) {this. idCard = idCard ;}@ Override public String toString () {return "[Person ��id =" + id + ", name =" + name + "]" ;}}Person

 

Package cn. itcast. I _hbm_oneToOne; public class IdCard {private Integer id; private String number; private Person person Person; public Integer getId () {return id;} public void setId (Integer id) {this. id = id;} public String getNumber () {return number;} public void setNumber (String number) {this. number = number;} public Person getPerson () {return person;} public void setPerson (Person person Person) {this. person = person ;}@ Override public String toString () {return "[IdCard ��id =" + id + ", number =" + number + "]" ;}}IdCard

 

<? Xml version = "1.0"?> <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  

<? Xml version = "1.0"?> <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  

Package cn. itcast. I _hbm_oneToOne; import org. hibernate. session; import org. hibernate. sessionFactory; import org. hibernate. cfg. configuration; import org. junit. test; import com. java1234.util. hibernateSessionFactory; public class App {private static SessionFactory sessionFactory = HibernateSessionFactory. getSessionFactory (); // save, associated @ Test public void testSave () throws Exception {Session session = sessi OnFactory. openSession (); session. beginTransaction (); // ---------------------------------------------- // create object Person = new person (); Person. setName ("Zhang San"); IdCard idCard = new IdCard (); idCard. setNumber ("00000011x"); // associate // person. setIdCard (idCard); idCard. setPerson (person); // Save the session. save (person); session. save (idCard); // ------------------------------------------------ session. getTrans Action (). commit (); session. close ();} // obtain the associated peer @ Test public void testGet () throws Exception {Session session = sessionFactory. openSession (); session. beginTransaction (); // -------------------------------------------- // obtain one party and display the other party's information // Person person = (Person) session. get (Person. class, 1); // System. out. println (person); // System. out. println (person. getIdCard (); IdCard idCard = (IdCard) Session. get (IdCard. class, 1); System. out. println (idCard); System. out. println (idCard. getPerson (); // ------------------------------------------------ session. getTransaction (). commit (); session. close () ;}// unassociate: only foreign key providers can maintain the association. @ Test public void testRemoveRelation () throws Exception {Session session = sessionFactory. openSession (); session. beginTransaction (); // revoke // unbind from a foreign key. // IdCard idCard = (IdCard) session. get (IdCard. class, 1); // idCard. setPerson (null); // unbind from the non-foreign key. No. Person person = (Person) session. get (Person. class, 1); person. setIdCard (null); // ------------------------------------------------ session. getTransaction (). commit (); session. close ();} // Delete the object, impact on the associated object @ Test public void testDelete () throws Exception {Session session = sessionFactory. openSession (); session. beginTransaction (); // ---------------------------------------------- // a. If there is no associated peer, delete it. // B. If the other party has an association and can maintain the association relationship (with a foreign key), it will first Delete the association and then delete itself. // C. If there is an associated peer and the association cannot be maintained (there is no foreign key), the user will be deleted directly, and an exception will occur. IdCard idCard = (IdCard) session. get (IdCard. class, 1); session. delete (idCard); // Person person Person = (Person) session. get (Person. class, 1); // session. delete (person); // ------------------------------------------------ session. getTransaction (). commit (); session. close ();}}APP

Configuration File hibernate. cfg. xml

<? Xml version = '1. 0' encoding = 'utf-8'?> <! DOCTYPE hibernate-configuration PUBLIC "-// Hibernate/Hibernate Configuration DTD 3.0 // EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> Example join of one-to-one primary key Association

For the remaining code, you only need to modify the following files.

<? Xml version = "1.0"?> <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  

Related Article

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.