Getting Exception : org.hibernate.exception.sqlgrammarexception - java

Hi I am implementing hibernate, When I am trying to insert a new record getting the Exception org.hibernate.exception.sqlgrammarexception
My hibernate.cfg.xml file :
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_db</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">system</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="com.hib.UserDetails"/>
</session-factory>
</hibernate-configuration>
If I changed the
<property name="hbm2ddl.auto">update</property>
<mapping class="com.hib.UserDetails"/>
It worked, But I have to create table manually.
What might be the issue?

Look for the piece of code where you are throwing your SQL/HQL query.
There you may find some simple yet hard to find mistakes like wrongly spelt commands,etc.
That's why sql 'grammar' exception

Check mapping class "UserDeatails".
There is mismatch between manually created table and UserDetails mapping class.
Possibly mismatch Column_Names, Id, Constraints ..etc

check for column names in table and match with column names in #column annotation in bean. Most probably its column mismatch.

Could you post the Stacktrace of the Exception, also it would be of help if you could post the code which is adding the User to Database.
It can either be a problem with the Column names of Database table and the declared variables.
<property name="show_sql">true</property>
<property name="format_sql">true</property>
These must be generating the SQL Statement, check if there is an error in the Generated SQL Statement. Posting the SQL could also help.

Related

can i have 2 configuration bdd of Hibernate?

i develop a J2EE application with Hibernate, and i want to have 2 database, the first will be local (in client computer ), and the second will be in the server.
indeed , i want to configure hibernate to connect in the local databases when the global is not accessible.
My actual config file :
<hibernate-configuration >
<session-factory>
<property name= "hibernate.dialect"> org.hibernate.dialect.MySQLDialect</property>
<property name= "hibernate.connection.driver_class"> com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/serverBdd </property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="hibernate.connection.password"></property>
<mapping resource="hibernateConfiguration/Usine.hbm.xml"/>
<mapping resource="hibernateConfiguration/Produit.hbm.xml"/>
<mapping resource="hibernateConfiguration/Machine.hbm.xml"/>
<mapping resource="hibernateConfiguration/Operation.hbm.xml"/>
<mapping resource="hibernateConfiguration/Utilisateur.hbm.xml"/>
</session-factory>
</hibernate-configuration>
how Can i do this ? and if i can't what should i do to solve my problem ?
thanks
Sure, here you can find an example that writes in 2 databases at same time, but with a simple if condition you can decide which to use:
First, create 2 hibernate.cfg.xml files as usual but with different names
serverconfig.cfg.xml
localconfig.cfg.xml
After, in your DAO or wherever you get your hibernate Session simply check if server database and if it is not available use local configuration:
SessionFactory sessionFactory = new Configuration().configure("serverconfig.cfg.xml").buildSessionFactory();
if (sessionFactory == null)
sessionFactory = new Configuration().configure("localconfig.cfg.xml").buildSessionFactory();
Also, if you use Hibernate 4 or later check this answer

Hibernate auto create database

I have a Java EE Hibernate project, and I'm using MySQL as a database.
I want that when I first time run the project, it will create the database automatically.
This is my hibernate.cnf.xml:
<?xml version='1.0' encoding='utf-8' ?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/InternetProject</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="connection.pool_size">10</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<mapping class="entities.Business" />
<mapping class="entities.Coupon" />
<mapping class="entities.User" />
<mapping class="entities.LastLogin" />
</session-factory>
</hibernate-configuration>
When I first time run this project on another computer, how can I make the database InternetProject to be created?
According to the config file, it might already do it and I'm not aware to it.
Thanks in advance.
<property name="hibernate.hbm2ddl.auto">create</property>
will create tables. But it will not create database. Change the connection url to generate the database.
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/InternetProject?createDatabaseIfNotExist=true</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="connection.pool_size">10</property>
Update : character encoding when creating database
<property name="connection.url">
jdbc:mysql://localhost/InternetProject?createDatabaseIfNotExist=true&useUnicode=yes&characterEncoding=UTF-8
</property>
<property name="hibernate.hbm2ddl.auto">create</property>
will do
Hibernate will not create the database for you, only the tables. To create the database you need to tell MySQL to create it if it does'nt already exist by adding a parameter to the URL. E.g.:
jdbc:mysql://db:3306/mydatabase?createDatabaseIfNotExist=true
There are multiple option for auto property.
create - It creates new tables corresponding mapping or annotation. It drops existing tables and data.
update - It keeps existing data and tables. It updates schema. here we have to take care contrants.
create-drop - It is same like create but once session gets closed it drops everything.
validate - it validates or matches schema with map or annotation. It's valid for Production environment.
<!-- create create-drop validate update -->
<property name="hbm2ddl.auto">update</property>
I hope it helps.

Hibernate + SQLite db outside jar

After few years of PHP I decided to learn Java, so I'm writing simple desktop app that uses Hibernate and SQLite.
I'd like to keep database outside of the .jar generated by mvn package, so one can perform quick backup of the data just by copying one file. So far, my hibernate.cfg.xml looks like this:
<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:mydb.db</property>
<property name="connection.username"></property>
<property name="connection.password"></property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping class="com.ex3v.hibernate.Contact"/>
</session-factory>
</hibernate-configuration>
what connection.url do I have to specify to make hibernate create db file next to .jar in working directory?
You can find your answer here:
Just a summary from above site,
different formats are :
jdbc:sqlite:mydb.db
jdbc:sqlite://dirA/dirB/mydb.db
jdbc:sqlite:/DRIVE:/dirA/dirB/mydb.db
jdbc:sqlite:///COMPUTERNAME/shareA/dirB/mydb.db
Use relative path like this.
jdbc:sqlite:./DB/mydb.db

Hibernate + embedded database - setup

I have created Java application using Hibernate with this configuration:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306 /bee</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
<property name="connection.username">root</property>
<property name="connection.password"/>
<property name="hibernate.connection.charSet">UTF-8</property>
<property name="hibernate.connection.characterEncoding">UTF-8</property>
<property name="hibernate.connection.useUnicode">true</property>
<mapping resource="DatabaseMapping.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Everything works fine when Iam using jdbc:mysql://localhost..., but now I need to have the database embedded in my application. Which database should I use? I need to have all my data stored and load it after start the application, update data, save, delete. I use HQL query or SQL query.
What is the simplest way to make the database embedded? I donĀ“t want to change my queries. It would be fine to change only hibernate configuration and set it to the embedded database, is it possible?
One of the advantages of ORM's like Hibernate is to shield you from DB differences. You can use any one of the below as an embedded DB solution. Just change the dialect, driver and URL in hibernate cfg file.
H2
SQLite
HSQLDB
Hava a look at using the H2 database in embedded mode
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.h2.Driver</property>
<property name="hibernate.connection.url">jdbc:h2:~/test</property>
<property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
<property name="connection.username">sa</property>
<property name="connection.password"/>
<property name="hibernate.connection.charSet">UTF-8</property>
<property name="hibernate.connection.characterEncoding">UTF-8</property>
<property name="hibernate.connection.useUnicode">true</property>
<property name="hibernate.default_schema">PUBLIC</property>
<mapping resource="DatabaseMapping.hbm.xml"/>
</session-factory>
</hibernate-configuration>
If you don't want to change your queries, consider mysql-mxj (embedded mysql) http://dev.mysql.com/doc/connector-mxj/en/connector-mxj.html
It is not under active development (thanks Oracle). But is prefectly usable and the connector is open source. It is trivial to embed different versions of mysql, but the latest mxj connector embeds 5.5.9

Changing Database from mysql to oracle using Hibernate

Hi I have a small java project and am using hibernate with it. Now I would like to change the database to oracle, what changes would I have to do in configuration file to make it possible.
In you hibernate.cfg.xml, you should have a dialect defined. The line looks like this:
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
change it to this:
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
Also change the database connection parameters in the same file.
Modify these properties. In hibernate.cfg.xml
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.password">your_new_password</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:#loalhost:xe</property>
<property name="hibernate.connection.username">user_name</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>

Categories