当前位置: 代码迷 >> J2EE >> 最简单的hibernate程序运行出错,该如何处理
  详细解决方案

最简单的hibernate程序运行出错,该如何处理

热度:46   发布时间:2016-04-22 01:21:35.0
最简单的hibernate程序运行出错
User类对应User表
public class User {
private long id;
private String name;
private String username;
private String password;
private String address;
private int mobile;
  //set get函数
  。。。。
}
User.hbm.xml
<hibernate-mapping>
<class name="com.domain.User" table="user">
<id name="id" column="id" type="long">
<generator class="increment"></generator>
</id>

<property name="name" column="name" type="string"></property>
<property name="username" column="username" type="string"></property>
<property name="password" column="password" type="string"></property>
<property name="address" column="address" type="string"></property>
<property name="mobile" column="mobile" type="int"></property>

</class>

</hibernate-mapping>

然后做了简单的用户保存

public class HibernateTest {
private static SessionFactory sessionFactory;

static{
try{
sessionFactory = new Configuration().configure().buildSessionFactory();
}
catch(Exception ex){
ex.printStackTrace();
}
}

public static void main(String[] args){
Session session = sessionFactory.openSession();
Transaction tx = null;

try{
tx = session.beginTransaction();

User user = new User();
user.setUsername("zhangsan");
user.setPassword("123");
user.setMobile(1234567);
user.setAddress("hangzhou");

session.save(user);
tx.commit();
}
catch(Exception ex){
if(null != tx){
tx.rollback();
}
}
finally{
session.close();
}

}
}

但是一直报错误
org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]

------解决方案--------------------
你用的是什么数据库?
------解决方案--------------------
很久没有用过了 不过我觉得可能是少加了一个属性user.setname("");
------解决方案--------------------
有没有在hibernate-cfg.xml中配置你的User??
如果有尝试用annotation的方法来配置一下,看是否成功
代码迷推荐解决方案:Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer],http://www.daimami.com/search?q=174629
  相关解决方案