Hibernate ing (1)-Basic ing

Source: Internet
Author: User

Hibernate ing (1)-Basic ing

 

Object relationship ing (ORM) is introduced in the previous blog: ORM Introduction

This blog post mainly introduces the basic mappings of Hibernate.

 

Concept

Relationship: Status of interaction and interconnectivity between things

Association: Associate objects (database tables) in a certain way

Ing: converts one form to another

 

Basic ing: There is no association between the table and the table (dictionary table in the system)

Hibernate provides several ing methods

 

Basic Hibernate Components

Ing Class (User. java): it describes the structure of the database table. Fields in the table are described as attributes. In the future, you can map the records in the table into objects of this class.

Ing file (User. hbm. xml ): it specifies the relationships between database tables and ing classes, including mappings between ing classes and database tables, table fields and class attribute classes, and table fields and Class Attribute names..

Database configuration file (User. properties/User. cfg. xml): it specifies the connection information required to connect to the database, such as the database to be connected, the user name to log on to the database, the login password, the connection string, and the address ing information of the ing Class.

 

Instance

1. ing object class: User. java

 

Package com. bjpowernode. hibernate; import java. util. date; // The Importance of the default null constructor public class User {// The empty constructor public User () {}// the Public User (String id, string name, String password) {this. id = id; this. name = name; this. password = password;} 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 ;}}}

 

Entity design principles:

(1) provide an identifier

(2) provide Default constructors without Parameters

The hibernate framework calls the Default Construction Instance Object. If no constructor is provided, the virtual machine automatically provides the default constructor. If other constructor methods with parameters are provided, the virtual machine will not provide you with the default constructor, in this case, you must manually write the construction method without parameters in the Code. Otherwise, an error will be reported during instantiation.

(3) we recommend that you do not use final to modify the object class.

When loading data with load delay, the object class will be inherited to generate a proxy object.

(4) It is recommended to generate the getter and setter methods for the object class.

If this parameter is not used, the field ID must be added to all attributes.

 

2. ing file (User. hbm. xml)

 

 


  
  
   
   
    
    
     
    
    
   
    
     
    
   
   
  

 

3. Database Configuration File: hibernate. cfg. xml

 

 
 
  
   
   
    
Com. mysql. jdbc. Driver
   
   
   
    
Jdbc: mysql: // localhost: 3306/hibernate_first
    
   
   
    
Root
   
   
    
123456
   
     
   
    
Org. hibernate. dialect. MySQLDialect
   
   
   
    
Update
    
   
   
    
True
   
   
   
  
 

4. Client

 

Public class Client {public static void main (String [] args) {// read Hibernate. cfg. xml file Configuration cfg = new Configuration (). configure (); // create SessionFactorySessionFactory = cfg. buildSessionFactory (); // get sessionSession session = null; try {session = factory. openSession (); // start the transaction session. beginTransaction (); User user = new User (); user. setName ("Gao Xiaoqing"); user. setPassword ("123"); // Save the object session. save (user ); // Submit the transaction session. getTransaction (). commit ();} catch (Exception e) {e. printStackTrace (); // roll back the transaction session. getTransaction (). rollback ();} finally {if (session! = Null) {if (session. isOpen () {// close sessionsession. close ();}}}}}

Result:

 

 

 

Note:

What should I do if I create a table separately?

Write a common main method in the program to generate tables in batches: ExportDB; it is not feasible to run directly, because there is no database hibernate_first, you need to create a database first, and then execute ExportDB.

 

Public class ExportDB {public static void main (String [] args) {// hibernate is read by default. cfg. xml file Configuration cfg = new Configuration (). configure (); SchemaExport export = new SchemaExport (cfg); // according to hibernate. cfg. xml automatically creates the database table export. create (true, true );}}


 

This blog post is mainly a small example of hibernate. Next we will continue to summarize other Mappings of hibernate.

 

 

 

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.