错误如下:
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