Hibernate mapping (i)-Basic mapping

Source: Internet
Author: User


Object Relational Mappings ( ORM ) has been introduced in the previous blog post: ORM Introduction

This blog post is mainly to introduce Hibernate The basic mapping


Concept

Relationship: The state of interacting and connecting things.

Association: Associating Objects (database tables) in some way

Mapping: Converting one form to another


Basic mapping: There is no connection between table and table (dictionary table in System)  

Hibernate provides several ways of mapping


Hibernate basic Components

Mapping Class (User. Java): It is the structure that describes a database table, and the fields in the table are described as attributes. In the future, you can map the records in a table into objects of that class.

Mapping file (User. Hbm.xml): It is the relationship between the specified database table and the mapping class, including the mapping class and database table correspondence, the Correspondence of table fields and class property categories, and the correspondence between table fields and class property names.

Database configuration file (user.properties/user.cfg.xml): It is the connection information that is required to specify the connection to the database, for example: which database to connect to, the user name to log in to the database, the login password, and the connection string, and the address mapping information for the mapping class

Example

1 , mapping entity classes: User.java

<span style= "FONT-SIZE:18PX;" >package Com.bjpowernode.hibernate;import java.util.date;//The importance of the default empty constructor public class User {<span style= "color:# ff0000; " >//empty constructor Public user () {}//constructor with parameters public user (String id,string name,string password) {this.id=id;this.name=name; This.password=password;} </span>private string Id;private string name;private string Password;public string GetId () {return ID;} public void SetId (String id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public String GetPassword () {return password;} public void SetPassword (String password) {this.password = password;}}} </span>

entity class design principles:

( 1 ) provides an identity

( 2 ) provides a default constructor with no parameters

Hibernate The framework invokes the default constructed instance object. If no construction method is provided, the virtual machine automatically provides the default constructor method, and if other constructive methods are involved , the virtual machines will no longer provide you with a default constructor method . In this case, you must manually write the non-parametric construction method in the code , Otherwise the error will be instantiated .

( 3 ) It is recommended not to use Final Modifying entity classes

The entity class generation proxy object is inherited when loading data with load delay

( 4 It is recommended that the entity class be generated Getter and the Setter Method

If not used, all attributes need to be added with field ID

2 , mapping Files ( User . Hbm.xml )

<pre name= "code" class= "HTML" ><span style= "font-size:18px;" >

3 , database configuration file: Hibernate.cfg.xml

<span style= "Font-family:simsun;" ><span style= "FONT-SIZE:18PX;" ><!--Database Mapping file-->

4, the Client

public class Client {public static void main (string[] args) {//Read Hibernate.cfg.xml file configuration cfg = new configuration ( ). Configure ();//establish sessionfactorysessionfactory factory = Cfg.buildsessionfactory ();//Get sessionsession session = NULL ; try {session = Factory.opensession ();//Open transaction session.begintransaction (); User user = new user (), User.setname ("Gao Xiaoqing"), User.setpassword ("123");//Save Object Session.save (user);// Commit Transaction Session.gettransaction (). commit ();} catch (Exception e) {e.printstacktrace ();//ROLLBACK TRANSACTION session.gettransaction (). rollback ();} finally {if (session! = NULL) {if (Session.isopen ()) {//close sessionsession.close ();}}}}

Results:


Note:

Create a table separately, what should I do?

the program writes a common main method, which is used to generate tables in batches:exportdb; Run directly is not possible, because there is no database Hibernate_first, you need to build the library before you execute ExportDB

public class Exportdb {public   static void Main (string[] args) {//default read hibernate.cfg.xml file configuration cfg = new Confi Guration (). Configure (); Schemaexport export = new Schemaexport (cfg);//Automatically build database table Export.create (true, true) according to Hibernate.cfg.xml;}}


This blog post is primarily A small instance of Hibernate, which will continue to summarize Other mappings for hibernate.




Hibernate mapping (i)-Basic mapping

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.