体验了一把hibernate,感觉中间有很多坑,先把坑汇总一下
首先导包真的麻烦,网上有很多包都不全得自己一点点调,一点点加
自己创了一个名为User的数据库,程序跑起来 结果老是出现 关键字 'user' 附近有语法错误 的报错信息
最后发现这是因为user是sql server中的一个关键字改成Users成功解决BUG
然后出现 不能将值 NULL 插入列 'userid',表 'thePotato.dbo.Users';列不允许有 Null 值。INSERT 失败。信息
最后找了很多资料发现主键需要设置成自增的,将主键userid设置成自增的后问题解决(这个问题的解决方法有点粗暴,具体原因是Hibernate 的<generator class="native"></generator>惹的祸具体这些属性的作用见http://blog.sina.com.cn/s/blog_7b8a637f010142if.html)
下面放一下工程文件和目录
database:
pojo 文件
package com.hib.pojo;
public class Users {
private Integer userid;
private String username;
private String password;
public Users() {
}
public Users(int userid) {
this.userid = userid;
}
public Users(int userid, String username, String password) {
this.userid = userid;
this.username = username;
this.password = password;
}
public Integer getUserid() {
return userid;
}
public void setUserid(Integer userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
dao
package com.hib.dao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.hib.pojo.Users;
public class UserDao {
Configuration conf = new Configuration().configure();
SessionFactory sf = conf.buildSessionFactory();
public void insertUser(Users user){
Session session = sf.openSession();
Transaction tx = null;
try{
tx = session.beginTransaction();
session.save(user);
tx.commit();
}catch(Exception e){
if(tx != null){
tx.rollback();
}
e.printStackTrace();
}finally{
session.close();
}
}
public Users getUser(Integer userid){
Session session = sf.openSession();
Users u1;
try{
u1=(Users)session.get(Users.class,userid);
}finally{
session.close();
}
return u1;
}
}
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">
<hibernate-configuration>
<session-factory >
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="connection.url">jdbc:sqlserver:// localhost.....</property>
<property name="connection.username">username</property>
<property name="connection.password">password</property>
<property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="myeclipse.connection.profile">thePotato</property>
<property name="show_sql">true</property>
<mapping resource="com/hib/pojo/Users.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Users.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.hib.pojo.Users" table="Users" schema="dbo" catalog="thePotato">
<id name="userid" type="java.lang.Integer">
<column name="userid" />
<generator class="native" />
</id>
<property name="username" type="java.lang.String">
<column name="username" length="10" not-null="false"/>
</property>
<property name="password" type="java.lang.String">
<column name="password" length="10" not-null="false"/>
</property>
</class>
</hibernate-mapping>