Simple hibernate demo and simple hibernate demo

Source: Internet
Author: User

Simple hibernate demo and simple hibernate demo

Mysql table

CREATE TABLE `t_user` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `name` varchar(45) NOT NULL,  `pwd` varchar(45) DEFAULT NULL,  PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=10113 DEFAULT CHARSET=utf8;

 

Hibernate. cfg. xml

<?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"><!-- Generated by MyEclipse Hibernate Tools.                   -->


User. java

package com.domain;public class User {Integer id;String name;String pwd;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 String getPwd() {return pwd;}public void setPwd(String pwd) {this.pwd = pwd;}}


Test. java

Package com. test; import org. hibernate. session; import org. hibernate. sessionFactory; import org. hibernate. cfg. configuration; import com. domain. *; public class Test {public static void main (String [] args) {Configuration cfg = new Configuration (). configure (); SessionFactory factory = cfg. buildSessionFactory (); Session session = null; try {session = factory. openSession (); session. beginTransaction (); User user = New User (); user. setName ("Wolf brother"); user. setPwd ("123345"); session. save (user); session. getTransaction (). commit ();} catch (Exception e) {e. printStackTrace (); session. getTransaction (). rollback ();} finally {// close sessionif (session! = Null & session. isOpen () session. close ();}}}


 

 

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.