King Midas
King Midas

Reputation: 1699

Why hibernate is creating a query without values?

One month ago I have posted this question Why hibernate is trying to execute a malformed query? And the solution has solve the issue.

Now the error has appear again in other very similar object and the suggested solution is not solving the issue. Then probably the error was still elsewhere.

I rewrite here the simplified example:

First Object:

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

Second Object:

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

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 in my previous question, again no parameters or values are inserted and, therefore, the error:

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

I can print out the values of the map, and it has values before persistence, therefore are not empty.

The application is an old one used for testing and now I am learning Hibernate and using it as a sand box for testing. Therefore several classes exists before using hibernate and I need to persist all of them. I have look up for a solution of this error, but not success. Also the links provided in my previous post doesn't give me a clue (probably by my lack of expertise in Hibernate).

Why is not inserting the values in the query?

UPDATED:

The hibernate.cfg.xml looks like:

<?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>

This file is in src/test/resources because now I am creating unit tests with maven and testng.

Upvotes: 4

Views: 528

Answers (1)

Eugene
Eugene

Reputation: 2711

The problem is most likely with non-standart dialect "org.hibernate.dialect.SQLiteDialect".

I tried your case with postgres dialect (org.hibernate.dialect.PostgreSQLDialect) and it runs fine.

Upvotes: 1

Related Questions