一个月前我发布了这个问题Why hibernate is trying to execute a malformed query?并且解决方案已经解决了问题。
现在错误再次出现在其他非常相似的对象中,建议的解决方案无法解决问题。那么错误可能还在别处。
我在这里重写了简化的例子:
第一个对象:
@Entity
@Table(name = "TABLE1")
public class FirstObject {
@Id
@GeneratedValue
private Long id; // database id
//Added as suggested in the previous solution.
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "object2Id")
private SecondOBject secondObject;
//Getters, setters and other stuff here.
}
第二个对象:
@Entity
@Table(name = "TABLE2")
public class SecondObject {
@Id
@GeneratedValue
private Long object2Id; // database id.
@ElementCollection
@CollectionTable(name = "T_MAPTABLE")
private Map<String, Integer> iAmAHashmap;
//More maps of Strings and Integers similar to the previous one, the getters, setters and other stuff.
}
当我执行测试时,我创建了一个带有“SecondObject”的对象“FirstObject”,并尝试使用 hibernate 持久化它,我可以看到 hibernate 正在生成此 sql 代码:
Hibernate:
insert
into
TABLE2
values
( )
与我之前的问题一样,同样没有插入任何参数或值,因此出现错误:
SqlExceptionHelper [main] - [SQLITE_ERROR] SQL error or missing database (near ")": syntax error)
我可以打印出 map 的值,它在持久化之前有值,因此不为空。
该应用程序是一个用于测试的旧应用程序,现在我正在学习 Hibernate 并将其用作测试沙箱。因此,在使用 hibernate 之前存在几个类,我需要保留所有这些类。我已经查找了此错误的解决方案,但没有成功。此外,我之前的帖子中提供的链接没有给我任何线索(可能是因为我缺乏 Hibernate 方面的专业知识)。
为什么不在查询中插入值?
更新:
hibernate.cfg.xml 看起来像:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="dialect">org.hibernate.dialect.SQLiteDialect</property>
<property name="connection.driver_class">org.sqlite.JDBC</property>
<property name="connection.url">jdbc:sqlite:database-test.db</property>
<property name="connection.username"></property>
<property name="connection.password"></property>
<property name="hibernate.hbm2ddl.auto">create-drop</property>
<mapping class="com.packet.FirstObject"></mapping>
<mapping class="com.packet.SecondObject"></mapping>
//More classes here.
</session-factory>
</hibernate-configuration>
此文件位于 src/test/resources
中,因为现在我正在使用 maven 和 testng 创建单元测试。
最佳答案
问题很可能是非标准方言“org.hibernate.dialect.SQLiteDialect”。
我用 postgres 方言 (org.hibernate.dialect.PostgreSQLDialect) 试过你的情况,它运行良好。
关于java - 为什么 hibernate 创建一个没有值的查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20855346/