乖乖,hibernate 居然会这样??Why?
1。表名:tUser1
CREATE TABLE [dbo].[tUser1] (
[uid] [int] NOT NULL ,
[username] [varchar] (255) COLLATE Chinese_PRC_CI_AS NULL ,
[password] [varchar] (255) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
2。User.hbm.xml
<?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.hibernate.User " table= "tUser1 ">
<id name= "uid " column= "uid " type= "java.lang.Integer ">
<generator class= "increment "/>
</id>
<property name= "username "/>
<property name= "password "/>
</class>
</hibernate-mapping>
3.class: HibernateUtil
package com.hibernate;
import
org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static{
try{
sessionFactory=new Configuration().configure().buildSessionFactory();
}catch(Throwable ex){
throw new
ExceptionInInitializerError(ex);
}
}
public static final ThreadLocal session=new ThreadLocal();
public static Session currentSession()throws
HibernateException{
Session s=(Session)session.get();
if(s==null){
s=sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession()throws HibernateException{
Session s=(Session)session.get();
session.set( null);
if(s!=null){
s.close();
}
}
}
4 测试类 test.java
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class Test {
public static void main(String args[]){
try{
Session session=HibernateUtil.currentSession();
Transaction tx=session.beginTransaction();
User user=new User();
user.setUsername( "中国 ");
user.setPassword( "123 ");
ses