• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SQL Exception : while trying to add record in database using Hibernate

 
tajinder pal singh
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone.

I am trying to save data using hibernate but i am getting exception.
I have two hbm files card and cardList. I am trying to achieve functionality in such a way if i will add cardList in db ,corresponding data gets stored in card table as well.

********Exception:**********

org.hibernate.exception.SQLGrammarException: could not insert: [de.htwg.se.setgame.database.Pojo.CardListPojo]
at org.hibernate.exception.SQLStateConverter.convert( SQLStateConverter.java:59)
at org.hibernate.exception.JDBCExceptionHelper.conver t(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.BasicEntityPersiste r.insert(BasicEntityPersister.java:1790)
at org.hibernate.persister.entity.BasicEntityPersiste r.insert(BasicEntityPersister.java:2192)
at org.hibernate.action.EntityIdentityInsertAction.ex ecute(EntityIdentityInsertAction.java:34)
at org.hibernate.engine.ActionQueue.execute(ActionQue ue.java:239)
at org.hibernate.event.def.AbstractSaveEventListener. performSaveOrReplicate(AbstractSaveEventListener.j ava:238)
at org.hibernate.event.def.AbstractSaveEventListener. performSave(AbstractSaveEventListener.java:158)
at org.hibernate.event.def.AbstractSaveEventListener. saveWithGeneratedId(AbstractSaveEventListener.java :104)
at org.hibernate.event.def.DefaultSaveOrUpdateEventLi stener.saveWithGeneratedOrRequestedId(DefaultSaveO rUpdateEventListener.java:184)
at org.hibernate.event.def.DefaultSaveOrUpdateEventLi stener.entityIsTransient(DefaultSaveOrUpdateEventL istener.java:173)
at org.hibernate.event.def.DefaultSaveOrUpdateEventLi stener.performSaveOrUpdate(DefaultSaveOrUpdateEven tListener.java:96)
at org.hibernate.event.def.DefaultSaveOrUpdateEventLi stener.onSaveOrUpdate(DefaultSaveOrUpdateEventList ener.java:69)
at org.hibernate.impl.SessionImpl.saveOrUpdate(Sessio nImpl.java:416)
at org.hibernate.impl.SessionImpl.saveOrUpdate(Sessio nImpl.java:411)
at de.htwg.se.setgame.database.Hibernate.CardList_dbO peration.addOrUpdateOperation(CardList_dbOperation .java:82)
at de.htwg.se.setgame.database.Hibernate.CardList_dbO peration.add(CardList_dbOperation.java:67)
at de.htwg.se.setgame.database.Hibernate.CardList_dbO perationTest.testAddCard(CardList_dbOperationTest. java:34)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:43)
at org.junit.internal.runners.JUnit38ClassRunner.run( JUnit38ClassRunner.java:86)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.rt.execution.junit.JUnitStarter.main( JUnitStarter.java:68)
Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorExceptio n: 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
at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.ja va:2985)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:16 31)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java :1723)
at com.mysql.jdbc.Connection.execSQL(Connection.java: 3283)
at com.mysql.jdbc.PreparedStatement.executeInternal(P reparedStatement.java:1332)
at com.mysql.jdbc.PreparedStatement.executeUpdate(Pre paredStatement.java:1604)
at com.mysql.jdbc.PreparedStatement.executeUpdate(Pre paredStatement.java:1519)
at com.mysql.jdbc.PreparedStatement.executeUpdate(Pre paredStatement.java:1504)
at org.hibernate.persister.entity.BasicEntityPersiste r.insert(BasicEntityPersister.java:1772)
... 32 more



******** Exception Ends*************




******** Mapping File**************
cardList.hbm.xml

HTML Code:
<hibernate-mapping>
<class name="de.htwg.se.setgame.database.Pojo.CardListPojo" table="CARD_LIST">
<meta attribute="class-description">
This class contains the CardList detail.
</meta>
<id name="cardList_id" type="int" column="CARDLIST_ID">
<generator class="native"/></id>
<bag name="cards" table="card" inverse="true" lazy="true" fetch="select">
<key column ="card_id" not-null="true"></key>
<one-to-many class="de.htwg.se.setgame.database.Pojo.CardPojo"></one-to-many></bag>

</class>
</hibernate-mapping>

******** Mapping File Ends**************


************ MApping file *************
card.hbm.xml

<hibernate-mapping>
<class name="de.htwg.se.setgame.database.Pojo.CardPojo" table="CARD">
<meta attribute="class-description">
This class contains the employee detail.
</meta>
<id name="card_id" type="int" column="CARD_ID">
<generator class="native"/>
</id>
<property name="color" column="CARD_COLOR" type="string"/>
<property name="form" column="CARD_FORM" type="string"/>
<property name="panelFilling" column="CARD_PANELFILLING" type="string"/>
<property name="count" column="CARD_COUNT" type="int"/>


<many-to-one name="cardList" class="de.htwg.se.setgame.database.Pojo.CardListPojo" fetch="select">
<column name="cardList_id" ></column>
</many-to-one>
</class>
</hibernate-mapping>
******** Mapping File Ends**************

************ Hibernate.cfg.xml ************

HTML Code:
<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="show_sql">true</property>

<!-- Assume test is the database name -->
<property name="hibernate.connection.url">
jdbc:mysql://127.0.0.1:3306/NewDB
</property>
<property name="hibernate.connection.username">
root
</property>
<property name="hibernate.connection.password">
root123
</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>


<!-- List of XML mapping files -->

<mapping resource="de/htwg/se/setgame/database/Hibernate/hbm/player.hbm.xml"/>
<mapping resource="de/htwg/se/setgame/database/Hibernate/hbm/cardList.hbm.xml"/>
<mapping resource="de/htwg/se/setgame/database/Hibernate/hbm/card.hbm.xml"/>
<mapping resource="de/htwg/se/setgame/database/Hibernate/hbm/game.hbm.xml"/>
</session-factory>
</hibernate-configuration>
************** JUNIT test method to test Add functionality in CardList *****************

HTML Code:
public void testAddCard() throws Exception {
System.out.println("-> testAddCard");
boolean cardIsAdded = false;
daoManager=new DaoManager_Operation();
ICardList cardList = daoManager.getCardList();
ICard card = null;
card = daoManager.getCard();
card.setColor("blue");
card.setForm("pik");
card.setPanelFilling("panelFilling_2");
cardList.addCard(card);
cardList_dbOperation=new CardList_dbOperation();
cardList_dbOperation.add(cardList);
List <ICard> list = cardList.getCards();
if (list.contains(card) == true){
cardIsAdded = true;
}
assertTrue(cardIsAdded); //#############
System.out.println("-> testAddCard END");
}

***************Actual method ****************

HTML Code:
public void addOrUpdateOperation(ICardList list){
Session session=getSession();
Transaction t=null;
try {
t = session.beginTransaction();
session.saveOrUpdate(list);
t.commit();
}
catch (Exception e) {
if (t != null) {
t.rollback();
throw e;
}
}
finally {
session.close();
}
}



Please give me your ideas what exactly wrong i am doing here.


Thanks & Regards
Tajinder
 
Claude Moore
Ranch Hand
Posts: 828
7
IBM DB2 Java Netbeans IDE
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your hibernate XML files, you set the dialect property first equal to Mysql, and then to SqlServer. Since your stacktrace is telling you there is a syntax error in generated statement, I am guessing the culprit may be in the dialect you are using.
 
tajinder pal singh
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Claude,
thanks a lot changing dialect helped. i would not have figured it out myself.
 
tajinder pal singh
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am facing another problem. when i am adding data in Card_list,only ID is getting added . i cannot find the data in card table. Please check and tell me if my mapping is fine in card.hbm.xml and cardlst.hbm.xml.
Thanks.
 
Claude Moore
Ranch Hand
Posts: 828
7
IBM DB2 Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know hibernate, so I may be only of little help... In your code I cannot see where you are persisting your card object.
 
tajinder pal singh
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for prompt reply once again. I am pasting my code below , you can give a try that would be great help.











Please let me know if i can provide any other details. Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic