Reputation:
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?
Upvotes: 0
Views: 2110
Reputation: 100726
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.
Upvotes: 1
Reputation:
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>
Upvotes: 0