4

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.

4
  • is the session factory object being created properly? Have you specified the database name in hibernate.cfg.xml Commented Dec 31, 2013 at 9:54
  • In my experience Hibernate generates improper queries when your environment is incorrectly setup and you're feeding it lies. Bad/mismatched annotations are a prime suspect. Commented Dec 31, 2013 at 9:59
  • Thanks for your comments @Alok. I have update the question with the hibernate configuration file. For the possibility of mismatched annotations, I have already reviewed it without success. Commented Dec 31, 2013 at 10:35
  • Still no success to find the cause of this error. The session factory object is created as shown in several examples and the notation seems good to me. If anybody have an idea that can help me to find the error I would be grateful. Commented Dec 31, 2013 at 14:26

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I can change the database to a more common one like MySQL and test it again. Thanks for your post.
I have tested with other dialect (MySQLDialect) as you suggested and the error has dissapear. Therefore is right to assume that the SQLDialect was the problem. I have never suspect it! The problem is I am really interested in use SQLite in this application, but seems that is not going to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.