当前位置: 代码迷 >> Java Web开发 >> hibernate报错,该如何解决
  详细解决方案

hibernate报错,该如何解决

热度:127   发布时间:2016-04-12 22:32:09.0
hibernate报错
错误如下:
org.hibernate.exception.ConstraintViolationException: could not insert: [orm.Userinfo]
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43

Userinfo.hbm.xml代码:
<hibernate-mapping>
    <class name="orm.Userinfo" table="Userinfo" schema="dbo" catalog="woniu">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="native" />
        </id>
        <property name="username" type="java.lang.String">
            <column name="username" length="50" not-null="true" />
        </property>
        <property name="password" type="java.lang.String">
            <column name="password" length="50" not-null="true" />
        </property>
        <property name="age" type="java.lang.Integer">
            <column name="age" />
        </property>
    </class>
</hibernate-mapping>


Insert.java代码:
package controller;

import orm.Userinfo;
import orm.UserinfoDAO;

public class Insert {

public String execute(){
Userinfo userinfo = new Userinfo();
userinfo.setUsername("woniu");
userinfo.setPassword("000123");
userinfo.setAge(10);


UserinfoDAO userinfoDAO = new UserinfoDAO();
userinfoDAO.getSession().beginTransaction();
userinfoDAO.attachDirty(userinfo);
userinfoDAO.getSession().getTransaction().commit();
userinfoDAO.getSession().close();
return "";

}
}
Hibernate Java
  相关解决方案