我的添加代码适用于我刚刚添加的新表(报告)的每个对象。我似乎无法找到问题,我只是得到了这个错误:
使用.save(object)
时无法执行语句
Report.hbm.xml
<hibernate-mapping>
<class name="com.atp.Models.Report" table="report">
<meta attribute="class-description">
This class contains the report details.
</meta>
<id name="environmentName" type="string" column="environmentName">
<generator class="native"/>
</id>
<property name="individual" column="Individual" type="int"/>
<property name="corporation" column="Corporation" type="int"/>
<property name="dda" column="DDA" type="int"/>
<property name="sav" column="SAV" type="int"/>
<property name="mtg" column="MTG" type="int"/>
<property name="sepaOut" column="SEPAOUT" type="int"/>
<property name="sepaIn" column="SEPAIN" type="int"/>
<property name="atm" column="ATM" type="int"/>
</class>
MySqL表
Java添加到数据库(所有字段都正确)
//Create new object
Report report = new Report();
report.setEnvironmentName(environmentName);
report.setIndividual(individual);
report.setCorporation(corporation);
report.setDda(dda);
report.setSav(sav);
report.setMtg(mtg);
report.setSepaIn(sepaIn);
report.setSepaOut(sepaOut);
report.setAtm(atm);
ManageCreation.AddToDatabase(report);
public static void AddToDatabase(Object object) {
SessionFactory factory = HibernateUtil.GetSessionFactory();
Session session = factory.openSession();
Transaction tx = null;
try{
tx = session.beginTransaction();
session.save(object);
tx.commit();
}catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
}finally {
session.close();
}
}
答案 0 :(得分:0)
如果没有完整的堆栈跟踪,我只能猜测varchar pk上的xml配置(你实际设置并且没有自动生成)会以某种方式导致问题...尝试删除
如果您在代码中设置了pk字段
Report report = new Report();
report.setEnvironmentName(environmentName);
您不需要自动生成配置
<generator class="native"/>
我也认为hibernate期望自动增量的数值类型,这可能实际上搞乱了事情