Why hibernate is trying to execute a malformed que

2019-09-09 20:40发布

问题:

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:

First Object:

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

   private SecondOBject secondObject

   //Getters, setters and other stuff here.
}

Second Object:

@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.

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)

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.

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.
}

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).

I have in my hibernate.cfg.xml:

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

And the MAPTABLE is created automatically when executing the test.

Then I have two questions:

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).

回答1:

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

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

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

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.