为什么hibernate正在尝试执行格式错误的查询? [英] Why hibernate is trying to execute a malformed query?

查看:119
本文介绍了为什么hibernate正在尝试执行格式错误的查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Hibernate 4.2.7.SP1的问题,我不知道是不是滥用或错误。
我正在使用带有注释的hibernate,我的代码与这两个对象类似:

I have a problem with Hibernate 4.2.7.SP1 and I do not know if it is a misuse or a bug. I am using hibernate with annotations and my code is similar to these two objects:

第一个对象:

@Entity
@Table(name = "TABLE1")
public class FirstObject { 
   @Id
   @GeneratedValue
   private Long id; // database id

   private SecondOBject secondObject

   //Getters, setters and other stuff here.
}

第二个对象:

@Entity
@Table(name = "TABLE2")
public class SecondObject {
    @Id
    @GeneratedValue
    private Long id; // database id.

    @ElementCollection   
    @CollectionTable(name = "T_MAPTABLE")
    private Map<String, Integer> iAmAHashmap;

    //More maps similar to the previous one, the getters, setters and other stuff.
}

当然,很多代码被省略,因为我认为这与问题。

Of course lot of code has been omitted because I think that is irrelevant to the question.

当我执行一个测试时,我用SecondObject创建一个对象FirstObject,并尝试用hibernate来保存它,我可以看到hibernate是生成此sql代码:

When I execute a test, where I create an object "FirstObject" with a "SecondObject" and try to persist it with hibernate, I can see that hibernate is generating this sql code:

Hibernate:
insert
into
    TABLE2

values
    ( )

如您所见,没有参数都没有价值。因此,启动了一个例外:

As you can see, there is no parameters and neither values. Therefore, an exception is launched:

 SqlExceptionHelper [main] - [SQLITE_ERROR] SQL error or missing database (near ")": syntax error)

SecondObject中的地图有值(我已经打印了地图的大小,以确定)。但Hibernate不存储它,并尝试在TABLE2中存储空参数。

The map in SecondObject has values (I have printed the size of the map to be sure). But Hibernate does not store it, and try to store empty parameters in TABLE2.

但是,如果我更改SecondObject并添加任何参数(即字符串):

But, if I change the SecondObject and add any parameter (i.e. a string):

@Entity
@Table(name = "TABLE2")
public class SecondObject {
    @Id
    @GeneratedValue
    private Long id; // database id.

    private String dummyText="hello!";

    @ElementCollection   
    @CollectionTable(name = "MAPTABLE")
    private Map<String, Integer> iAmAHashmap;

    //More maps similar to the previous one, the getters, setters and other stuff.
}

Hibernate生成的代码是:

The code generated by Hibernate is:

Hibernate:
insert
into
    TABLE2
    (dummyText)
values
    (?)

至少没有显示任何错误(但显然地图未插入)。

And at least, no errors is shown (but obviously the Map is not inserted).

我在我的hibernate.cfg.xml中:

I have in my hibernate.cfg.xml:

 ....
  <property name="hibernate.hbm2ddl.auto">create-drop</property>
 ....

当执行测试时,MAPTABLE将自动创建。

And the MAPTABLE is created automatically when executing the test.

然后我有两个问题:

为什么hibernate试图执行没有参数和值的查询?
为什么我的地图未插入? (我的所有课程中都有这个错误,只有Map并没有其他参数)。

Why hibernate is trying to execute a query without parameters and values? Why my Map is not inserted? (I have this error in all my classes that only have Maps and no other parameters).

推荐答案

我不知道是否省略了它,因为FirstObject是与外键的关系该数据库,您需要指定与注释。您可以在这里找到不同类型关系的示例,其中包含示例和进一步说明: http:// en .wikibooks.org / wiki / Java_Persistence / Relationships

I don't know if you have omitted it, as FirstObject<->SecondObject is a relationship with a foreign key inside the db, you need to specidfy that with annotations. You can find examples for the different types of relationships here, with examples and further explanations: http://en.wikibooks.org/wiki/Java_Persistence/Relationships

OneToOne-Relationship将如下所示,因此JPA知道外键是什么。 p>

A OneToOne-Relationship would look like this, so that JPA knows what the foreign key is.

@OneToOne
@JoinColumn(name="ID")
private SecondObject secondObject;

相同的集合: http://en.wikibooks.org/wiki/Java_Persistence/ElementCollection
CollectionTable-Annotation有一个属性joinColumn来指定外键关系。

Same goes for the Collection: http://en.wikibooks.org/wiki/Java_Persistence/ElementCollection The CollectionTable-Annotation has an attribute joinColumn to specify the foreign key relationship.

这篇关于为什么hibernate正在尝试执行格式错误的查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆