gpt4 book ai didi

java - 无法执行语句

转载 作者:太空宇宙 更新时间:2023-11-04 10:52:25 24 4
gpt4 key购买 nike

我的添加代码适用于除我刚刚添加的新表(报告)之外的每个对象。我似乎找不到问题,只是收到此错误:

Could not execute statement when using .save(object)

报告.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 表

My Sql Table

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();
}
}

最佳答案

如果没有完整的堆栈跟踪,我只能猜测 varchar pk 上的 xml 配置(您实际设置的并且不是自动生成的)在某种程度上导致了问题...尝试删除它

如果你在代码中设置了pk字段

Report report = new Report();
report.setEnvironmentName(environmentName);

您不需要自动生成配置

<generator class="native"/>

我还认为 hibernate 期望自动增量的数字类型的值,这实际上可能会搞乱事情

关于java - 无法执行语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47651727/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com