Why is hibernate trying to execute an invalid request?

I have a problem with Hibernate 4.2.7.SP1 and I don’t know if this is a misuse or an error. I use 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.
}

The 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, a lot of code was omitted because I think this is not relevant to the issue.

When I run a test where I create a "FirstObject" object with "SecondObject" and try to save it using hibernate, I see that this sleep mode generates this sql code:

Hibernate:
insert
into
    TABLE2

values
    ( )

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

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

The map in SecondObject matters (I printed the size of the map to make sure). But Hibernate does not save it and tries to save empty parameters in TABLE2.

, SecondObject (.. ):

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

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

, , (, , ).

hibernate.cfg.xml:

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

MAPTABLE .

:

hibernate ? ? ( , ).

0
1

, , FirstObject ↔ SecondObject - db, , . : http://en.wikibooks.org/wiki/Java_Persistence/Relationships

A OneToOne-Relationship , JPA , .

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

: http://en.wikibooks.org/wiki/Java_Persistence/ElementCollection CollectionTable-Annotation joinColumn .

+1

All Articles