tags:

views:

403

answers:

2

I am changing configuration of hibernate 3.0 from MSSQL to MySQL. This is done but Hibernate is not generating proper queries for MySQL. I have also updated my configuration file with MySql dialect and Mysql jdbc connector. The problem i am facing here is --

I have one table only with PK with one more column(FK of other table). Now while inserting data in that table it is generating query like --

insert in to table_name default values

This query works for MSSql but not with MySQL. Please let me know any solution for this. How the Hibernate will generate right query for MySQL?

+1  A: 

I'm pretty sure that highlighted statement is invalid in both MSSQL and MySQL - or any other database for that matter :-)

Are you absolutely sure you've changed the dialect to point to MySQL? Assuming the statement you quoted is actually "INSERT INTO my_table DEFAULT VALUES", that's valid syntax for MS SQL, but not MySQL.

Can you post the actual query as logged by Hibernate as well as actual MySQL exception? Your Hibernate configuration, mapping for the entity in question and table schema may also help diagnose the problem.

Update (based on clarification):

The configuration file you've provided is definitely not getting picked up. "DEFAULT VALUES" string is produced by Dialect.getNoColumnsInsertString() overwritten for dialects that support it (Sybase, MSSql, Postgres, DB2). MySQLDialect uses VALUES() form instead.

You can verify what dialect you're actually using in your code by casting your sessionFactory to SessionFactoryImplementor and calling its getDialect() method.

ChssPly76
Query generated by hibernate is --Hibernate: insert into Tr default valueswhere Tr is the table name.MySQL Exception is -"com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'default values' at line 1"
Mohit
Thanks for quick reply. it is still using MSSQL dialect (org.hibernate.dialect.SQLServerDialect).Please help me to change the dialect to MySQL. From where it is taking the reference of MSSQL? I have removed the jar file of SQLjdbc also and added for mysql.Please help.
Mohit
Your program is picking the wrong (old?) hibernate configuration file. Run a clean build; if that doesn't help do a global search on hibernate.cfg.xml (or whatever it's named) and see if you can locate and remove (or overwrite) the other (older) copy(ies).
ChssPly76
I am searching for configuration file but meanwhile i want to ask a query that --If it would have been picking the wrong hibernate configuration file then it should have not be configured with MySQL (For other tables data is inserting in to MySQL only) as before that i was using MSSQL. So why is it happening that the properties changed to configure with MySQL from MSSQL are working but only for dialect it is not working?
Mohit
Sorry, I'm not quite sure what you mean by that. Are you surprised that no other errors popped up? This one just happened to be first :) Besides, MSSQL and MySQL are not _drastically_ different; standard run-of-the-mill SQL statements would work in both.
ChssPly76
I have searched but there is no other configuration file in my system. Can i go for any other work around? (Really surprised). :(
Mohit
There is no workaround. You must have another hibernate.cfg.xml somewhere in your classpath; that's what's getting picked up instead of the one you've supplied.
ChssPly76
Thanks for your great help and support.
Mohit
A: 

Tr.java -->

@Entity @AccessType("property")
@Table (name="Tr")
public class Tr{


protected int tr_autoID;
protected List<Data> dataObj;

//Parameterized Constructor
public Tr(List<Data> data) {
 this.dataObj = data;
}

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public int getTr_autoID() {
 return tr_autoID;
}

public void setTr_autoID(int tr_autoID) {
 this.tr_autoID = tr_autoID;
}


@OneToMany
@JoinColumn(name="Tr_FK")
public List<Data> getDataObj() {
 return dataObj;
}
public void setDataObj(List<Data> dataObj) {
 this.dataObj = dataObj;
}
}

Hibernate Configuration File --

<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/cdaapp</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

<property name="configurationClass">org.hibernate.cfg.AnnotationConfiguration</property>

<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<!--  thread is the short name for
  org.hibernate.context.ThreadLocalSessionContext
  and let Hibernate bind the session automatically to the thread
-->
<property name="current_session_context_class">thread</property>
<!-- this will show us all sql statements -->
<property name="hibernate.show_sql">true</property>
Mohit
I've updated my answer. Your configuration file is not getting picked up - you're clearly using some other dialect (not MySQLDialect)
ChssPly76