日期:2014-05-19 浏览次数:20721 次
package com.test.hibernate; import java.util.Date; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class Start { public static void main(String[] args) { Configuration cfg = new Configuration().configure(); SessionFactory factory = cfg.buildSessionFactory(); Session session =null ; try{ session = factory.openSession(); session.beginTransaction(); Test test = new Test(); test.setName("????张三"); test.setPassword("123456"); test.setStsdate(new Date()); test.setEnddate(new Date()); test.setId(1); session.save(test); session.getTransaction().commit(); }catch(Exception e){ e.printStackTrace(); System.out.println("?抛出异常"); session.getTransaction().rollback(); }finally{ if(session != null) { if(session.isOpen()) { session.close(); } } } } }
package com.test.hibernate; import java.util.Date; public class Test { private String name; private String password; private Date stsdate; private Date enddate; private int id; public int getId() { return id; } public void setId(int id) { this.id = id; } public Date getEnddate() { return enddate; } public void setEnddate(Date enddate) { this.enddate = enddate; } 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; } public Date getStsdate() { return stsdate; } public void setStsdate(Date stsdate) { this.stsdate = stsdate; } }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping > <class name="com.test.hibernate.Test"> <id name="id" column="id"> <generator class="uuid"/> </id> <property name="id" column="int"/> <property name="name" column="name"/> <property name="password" column="password"/> <property name="stsdate" column="stsdate" /> <property name="enddate" column="enddate"/> </class> </hibernate-mapping>
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://h