为什么hibernate创建没有值的查询?

时间:2013-12-31 09:52:09

标签: java hibernate

一个月前,我发布了这个问题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)

我可以打印出地图的值,它在持久性之前有值,因此不是空的。

该应用程序是一个用于测试的旧应用程序,现在我正在学习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创建单元测试。

1 个答案:

答案 0 :(得分:1)

问题很可能是非标准方言“org.hibernate.dialect.SQLiteDialect”。

我用postgres方言(org.hibernate.dialect.PostgreSQLDialect)尝试了你的情况,它运行正常。