1。Tproduct表为:
主键 Product_id varchar 50
Product_name varchar 50
2。Tproduct.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 package="com.hiber"> <class name="Tproduct" table="TProduct">
<id name="productId" column="Product_id" type="string">
<generator class="native"/>
</id>
<property name="productName" column="Product_name" type="string" />
</class>
</hibernate-mapping>3。插入程序代码为 :Transaction tran = null;
try {
Session session = HibernateSessionFactory.currentSession();
tran = session.beginTransaction();
Tproduct tp = new Tproduct();
tp.setProductId("eeeee");
tp.setProductName("fffff");
session.save(tp);
session.flush();
} catch (HibernateException e) {
e.printStackTrace();
Assert.fail(e.getMessage());
if (tran != null) {
try {
tran.rollback();
} catch (HibernateException e1) {
e1.printStackTrace();
} } }
4。报错内容如下:
org.hibernate.exception.ConstraintViolationException: could not insert: [com.hiber.Tproduct]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:63)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
。。
Caused by: com.jnetdirect.jsql.JSQLException: 无法将 NULL 值插入列 'Product_id',表 'querytest.dbo.TProduct';该列不允许空值。INSERT 失败。
主键 Product_id varchar 50
Product_name varchar 50
2。Tproduct.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 package="com.hiber"> <class name="Tproduct" table="TProduct">
<id name="productId" column="Product_id" type="string">
<generator class="native"/>
</id>
<property name="productName" column="Product_name" type="string" />
</class>
</hibernate-mapping>3。插入程序代码为 :Transaction tran = null;
try {
Session session = HibernateSessionFactory.currentSession();
tran = session.beginTransaction();
Tproduct tp = new Tproduct();
tp.setProductId("eeeee");
tp.setProductName("fffff");
session.save(tp);
session.flush();
} catch (HibernateException e) {
e.printStackTrace();
Assert.fail(e.getMessage());
if (tran != null) {
try {
tran.rollback();
} catch (HibernateException e1) {
e1.printStackTrace();
} } }
4。报错内容如下:
org.hibernate.exception.ConstraintViolationException: could not insert: [com.hiber.Tproduct]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:63)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
。。
Caused by: com.jnetdirect.jsql.JSQLException: 无法将 NULL 值插入列 'Product_id',表 'querytest.dbo.TProduct';该列不允许空值。INSERT 失败。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货