【发布时间】:2016-04-28 17:21:08
【问题描述】:
当我尝试保存或更新一个简单实体时遇到了一个奇怪的问题:
@Entity
public class TimeTable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@OneToMany(fetch = FetchType.EAGER, orphanRemoval = false, targetEntity = Subject.class)
private Set<Subject> courses = new HashSet<Subject>();
@Entity
public class Subject {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column(nullable = false, unique = true)
private String name;
@Column(nullable = false)
private boolean mandatory = false;
@Column(nullable = false)
private Integer credits;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true, targetEntity = Period.class)
private Set<Period> periods = new HashSet<Period>();
`DAO 类
public synchronized void saveOrUpdate(E obj) {
log.log(Level.DEBUG, "iniciando save de " + obj.toString());
Session s = this.getSession();
s.beginTransaction();
s.saveOrUpdate(obj);
try {
s.getTransaction().commit();
} catch (Exception e) {
s.getTransaction().rollback();
e.printStackTrace();
}
s.close();
log.log(Level.DEBUG, "save concluido com sucesso");
}
和我的测试代码:
GenericDAO<TimeTable> asd = new GenericDAO<TimeTable>(TimeTable.class);
GenericDAO<Subject> qwe = new GenericDAO<Subject>(Subject.class);
Set<Subject> aasdasd = new HashSet<Subject>();
aasdasd.addAll(qwe.findAll());
TimeTable zxc = new TimeTable(aasdasd);
asd.saveOrUpdate(zxc);
但我在 saveOrUpdate 行遇到异常:
Caused by: java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (near ")": syntax error)
at org.sqlite.core.DB.newSQLException(DB.java:890)
Exception in thread "main" org.hibernate.exception.GenericJDBCException: [SQLITE_ERROR] SQL error or missing database (near ")": syntax error)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:52)
异常指向 s.saveOrUpdate(obj);在我的 dao 类中,但是当我尝试对其他实体做完全相同的事情时,它不会发生[甚至更复杂]。
有人有什么建议吗?
【问题讨论】:
-
您的输入数据中是否有任何可能破坏 hibernet 创建的 SQL 的值?你可以先验证一下。
-
我只有整数和布尔值,我不知道它怎么可能破坏语句。我以为这是一个变量名,但检查了所有并更改了一些但仍然发生
-
@RafaelLima 可以打印正在执行的 sql。根据异常,该语句未正确生成。可以调试得到sql语句吗
-
异常发生在show_sql打印之前,但是deep debugginh hibernate代码我看到生成的语句就像插入到TimeTable()1中,其中1应该是标识符,它确实是休眠中的一个错误
标签: java hibernate sqlite exception