aspose file tools*
The moose likes Object Relational Mapping and the fly likes CRUD by Hibernate in MySql. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "CRUD by Hibernate in MySql." Watch "CRUD by Hibernate in MySql." New topic
Author

CRUD by Hibernate in MySql.

Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 504

As per this...
I am trying to learn hibernate with mysql...i am not expert of java or sql.
I am using various forums/books/tutorials/examples etc but its hard to understand for me or hard to find out what i needed currently...

I create 1 table within mysql database named temp. In temp i create 1 table named player which contain 3 columns playerid(this is pk,NN,AI)(int),payer_name(Varchar),country_name(Varchar).

I create 1 project name Exaample_hiber using eclipse helios with hibernate plug-in
i create 3 java class files in respected 3 folders in src folder which name's Country_Club-->main_app.java, Country_Management-->hiber_management.java, Country_Player-->player.java & player.hbm.xml
main_app.java as follows:-


player.java file contain following code:-


hiber_management.java file contian following code:-

player.hbm.xml which i create by using wizard as follows:-

In src i create one more xml named hibernate.cfg.xml as follows:-

Now after run program i try to put 1 character in each text fields & try to save it in table...
i got error

Hibernate: insert into PLAYER (PLAYER_NAME, COUNTRY_NAME, PLAYERID) values (?, ?, ?)
org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at Country.Club.main_app$2.actionPerformed(main_app.java:98)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.sql.BatchUpdateException: Unknown column 'COUNTRY_NAME' in 'field list'
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1103)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:853)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
... 44 more

For learning purpose anybody help me how in small program i can implement CRUD (Create, Retrieve, Update, Delete) in this program...
Thanks in advance...


Millions saw the apple fall, but Newton asked why.
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 859

Welcome,

A few Things, your error is: Unknown column 'COUNTRY_NAME' in 'field list'

You should not be defining attributes with the "_" character, instead use countryName, and getCountryName() setCountryName().

WP
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 504

William P O'Sullivan,
A few Things, your error is: Unknown column 'COUNTRY_NAME' in 'field list'

What a Hawk-eye you have...actually my stupid mistake...
Now i can enter table entries by text fields ....
So update complete...

You should not be defining attributes with the "_" character, instead use countryName, and getCountryName() setCountryName().

Is there any rule for that?

Now i tried this from yesterday
in main_app.java

By running this code i got error as follows:-

Hibernate: select player0_.PLAYERID as PLAYERID0_, player0_.PLAYER_NAME as PLAYER2_0_, player0_.player_country as player3_0_ from PLAYER player0_ where (player0_.PLAYER_NAME like 'a%' )
1
org.hibernate.InstantiationException: No default constructor for entity: Country.Player.player
at org.hibernate.tuple.PojoInstantiator.instantiate(PojoInstantiator.java:84)
at org.hibernate.tuple.PojoInstantiator.instantiate(PojoInstantiator.java:100)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.instantiate(AbstractEntityTuplizer.java:351)
at org.hibernate.persister.entity.AbstractEntityPersister.instantiate(AbstractEntityPersister.java:3606)
at org.hibernate.impl.SessionImpl.instantiate(SessionImpl.java:1275)
at org.hibernate.impl.SessionImpl.instantiate(SessionImpl.java:1264)
at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1299)
at org.hibernate.loader.Loader.getRow(Loader.java:1206)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:580)
at org.hibernate.loader.Loader.doQuery(Loader.java:701)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.doList(Loader.java:2220)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
at org.hibernate.loader.Loader.list(Loader.java:2099)
at org.hibernate.hql.classic.QueryTranslatorImpl.list(QueryTranslatorImpl.java:912)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
at Country.Club.main_app.executeHQLQuery(main_app.java:59)
at Country.Club.main_app.runQueryBasedOnplayerName(main_app.java:45)
at Country.Club.main_app.getShowjButtonActionPerformed(main_app.java:201)
at Country.Club.main_app.access$0(main_app.java:199)
at Country.Club.main_app$1.actionPerformed(main_app.java:76)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 504

I got my self answer
No default constructor for entity: Country.Player.player

So i write it down ..then program work smoothly...
Now new question arise that By entering value in textfield i try to search it into db & by resultlist i got display in table...
instead of that can i do it without any search i should got values from table in some integers like a,b,c...
I think for that something should be change in following code...


What "%'" does in above code? i think it use for comparing with resultlist value?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: CRUD by Hibernate in MySql.