Hibernate series (i): simple data insertion

Source: Internet
Author: User

The first step, we need to write a Pojo class: User.java

Package Cn.itcast.hibernate.domain;import Java.util.date;public class User {private int id;private String name;private Date birthday;public int getId () {return ID;} public void setId (int id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public Date Getbirthday () {return birthday;} public void Setbirthday (Date birthday) {this.birthday = birthday;}}
In order for this javabean to have the ability to persist operations, we need to add a mapping file under the current package User.hbm.xml:

<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">


The Org.hibernate.cfg.Configuration instance represents an application-to-SQL database mapping configuration, and the configuration object provides a Buildsessionfactory method that can produce an immutable sessionfactor Y object. Therefore, we need to create Configuration:hibernate.cfg.xml by mapping the file

<?xml version= ' 1.0 ' encoding= ' utf-8 '?> <! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "http// Hibernate.sourceforge.net/hibernate-configuration-3.0.dtd ">

Finally, we write a simple main class to implement simple data insertion Base.java:

Package Cn.itcast.hibernate;import Java.util.date;import Org.hibernate.session;import org.hibernate.SessionFactory ; import Org.hibernate.transaction;import Org.hibernate.cfg.configuration;import cn.itcast.hibernate.domain.User; public class Base {public static void main (string[] args) {Configuration cfg = new configuration ();//Generate a Configuration object C Fg.configure (); Call the Configure () method to load the mapping file sessionfactory SF = Cfg.buildsessionfactory (); Produces a sessionfactorysession s = sf.opensession (); Open Sessiontransaction tx = S.begintransaction (); Open Transaction User user = new user (), User.setbirthday (New Date ()), User.setname ("name"), S.save (user); Save the user object in the session tx.commit (); Commit Transaction s.close (); Close SessionSystem.out.println ("End");}}

Simple data insertion can be achieved by running it.

The jar package that needs to be used:





Hibernate series (i): simple data insertion

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.