File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Simple Hibernate Help (Im Noob) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Simple Hibernate Help (Im Noob)" Watch "Simple Hibernate Help (Im Noob)" New topic
Author

Simple Hibernate Help (Im Noob)

agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56
Dear friend I think I might have a simple problem which ofcourse I do not have the solution yet.
I have set up my working file below neatly. I hope it does not confused you.
At the bottom you would see the outcome from my project.

This is my "hibernate.cfg.xml"


This is my Articles.hbm.xml


Below is my Article class. It just have setter and getter only.


Finally my Test class



This is the outcome by executing the Test.java
Could anyone help me out please. Im sorry to bother you.

init:
deps-module-jar:
deps-ear-jar:
deps-jar:
compile-single:
run-main:
31 Mar 11 16:29:40 org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.2.5
31 Mar 11 16:29:40 org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
31 Mar 11 16:29:40 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : cglib
31 Mar 11 16:29:40 org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
31 Mar 11 16:29:41 org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
31 Mar 11 16:29:41 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
31 Mar 11 16:29:41 org.hibernate.cfg.Configuration addResource
INFO: Reading mappings from resource : demo/classes/Articles.hbm.xml
31 Mar 11 16:29:41 org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues
INFO: Mapping class: demo.classes.Articles -> articles
31 Mar 11 16:29:41 org.hibernate.cfg.Configuration doConfigure
INFO: Configured SessionFactory: null
31 Mar 11 16:29:41 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Using Hibernate built-in connection pool (not for production use!)
31 Mar 11 16:29:41 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Hibernate connection pool size: 20
31 Mar 11 16:29:41 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: autocommit mode: false
31 Mar 11 16:29:41 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://127.0.0.1:3306/new_schema
31 Mar 11 16:29:41 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: connection properties: {user=root, password=****}
31 Mar 11 16:29:41 org.hibernate.cfg.SettingsFactory buildSettings
INFO: RDBMS: MySQL, version: 5.1.51-community
31 Mar 11 16:29:41 org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.1.6 ( Revision: ${svn.Revision} )
31 Mar 11 16:29:41 org.hibernate.dialect.Dialect <init>
INFO: Using dialect: org.hibernate.dialect.MySQLDialect
31 Mar 11 16:29:41 org.hibernate.transaction.TransactionFactoryFactory buildTransactionFactory
INFO: Using default transaction strategy (direct JDBC transactions)
31 Mar 11 16:29:41 org.hibernate.transaction.TransactionManagerLookupFactory getTransactionManagerLookup
INFO: No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
31 Mar 11 16:29:41 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic flush during beforeCompletion(): disabled
31 Mar 11 16:29:41 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic session close at end of transaction: disabled
31 Mar 11 16:29:41 org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC batch size: 15
31 Mar 11 16:29:41 org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC batch updates for versioned data: disabled
31 Mar 11 16:29:41 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Scrollable result sets: enabled
31 Mar 11 16:29:41 org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC3 getGeneratedKeys(): enabled
31 Mar 11 16:29:41 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Connection release mode: auto
31 Mar 11 16:29:41 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Maximum outer join fetch depth: 2
31 Mar 11 16:29:41 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default batch fetch size: 1
31 Mar 11 16:29:41 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Generate SQL with comments: disabled
31 Mar 11 16:29:41 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Order SQL updates by primary key: disabled
31 Mar 11 16:29:41 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Order SQL inserts for batching: disabled
31 Mar 11 16:29:41 org.hibernate.cfg.SettingsFactory createQueryTranslatorFactory
INFO: Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
31 Mar 11 16:29:41 org.hibernate.hql.ast.ASTQueryTranslatorFactory <init>
INFO: Using ASTQueryTranslatorFactory
31 Mar 11 16:29:41 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query language substitutions: {}
31 Mar 11 16:29:41 org.hibernate.cfg.SettingsFactory buildSettings
INFO: JPA-QL strict compliance: disabled
31 Mar 11 16:29:41 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Second-level cache: enabled
31 Mar 11 16:29:41 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query cache: disabled
31 Mar 11 16:29:41 org.hibernate.cfg.SettingsFactory createCacheProvider
INFO: Cache provider: org.hibernate.cache.NoCacheProvider
31 Mar 11 16:29:41 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Optimize cache for minimal puts: disabled
31 Mar 11 16:29:41 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Structured second-level cache entries: disabled
31 Mar 11 16:29:41 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Statistics: disabled
31 Mar 11 16:29:41 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Deleted entity synthetic identifier rollback: disabled
31 Mar 11 16:29:41 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default entity-mode: pojo
31 Mar 11 16:29:41 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Named query checking : enabled
31 Mar 11 16:29:41 org.hibernate.impl.SessionFactoryImpl <init>
INFO: building session factory
31 Mar 11 16:29:41 org.hibernate.impl.SessionFactoryObjectFactory addInstance
INFO: Not binding factory to JNDI, no JNDI name configured
BUILD SUCCESSFUL (total time: 1 second)


My database sql

David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

and what is the problem?
agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56
I believe this sir
INFO: Not binding factory to JNDI, no JNDI name configured
agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56
David O'Meara wrote:and what is the problem?


I think the problem is "INFO: Not binding factory to JNDI, no JNDI name configured".

I have also tried to create a resource in the TOMCAT context



I have also update the hibernate.cfg.xml file to this


It produce this error

SEVERE: Could not find datasource: java:comp/env/jdbc/new_schema
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:52)
at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:124)
at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:56)
at org.hibernate.cfg.SettingsFactory.createConnectionProvider(SettingsFactory.java:414)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:62)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2009)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1292)
at demo.classes.Test.main(Test.java:22)
Exception in thread "main" org.hibernate.HibernateException: Could not find datasource
at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:56)
at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:124)
at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:56)
at org.hibernate.cfg.SettingsFactory.createConnectionProvider(SettingsFactory.java:414)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:62)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2009)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1292)
at demo.classes.Test.main(Test.java:22)
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:52)
... 7 more
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Are you using Tomcat? It is the first time you mentioned it.
Going back to your first example, you need some way to connect your code to the Hibernate configuration.
It logs your connection details so no problem there.
Your SessionFactory is not bound to JNDI so that's not a problem.
Looking at your test code, you open a transaction and then do nothing.
Committing it could be useful.
You can also look for the property which logs SQL statements as this is handy to test what it going to the database.
agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56
Sir Sherif, thank you for the response
unfortunately it gave me this error

Test.class update


I also try added the HibernateUtil.java

Error code:
SEVERE: Could not synchronize database state with session
org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
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)
Could not execute JDBC batch update
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at demo.classes.Test.main(Test.java:29)
Caused by: java.sql.BatchUpdateException: Data truncated for column 'Copyright' at row 1
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1666)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1082)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
... 8 more


I have not eaten yet, becauase i have not resolve this problem since 10PM
agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56
agustinus lumbantoruan wrote:Sir Sherif, thank you for the response
Yes Sherif, I do use Tomcat because it's cool )
unfortunately it gave me this error

Test.class update


I also try added the HibernateUtil.java

Error code:
SEVERE: Could not synchronize database state with session
org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
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)
Could not execute JDBC batch update
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at demo.classes.Test.main(Test.java:29)
Caused by: java.sql.BatchUpdateException: Data truncated for column 'Copyright' at row 1
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1666)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1082)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
... 8 more


I have not eaten yet, becauase i have not resolve this problem since 10PM
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Excellent, now we're getting somewhere!
Does that item already exist in the database?
agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56
I dont think so hmmmmm
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

It says data is truncated. Can you check, if you are trying to insert value which is more the allowed length by the table's column?


If you are not laughing at yourself, then you just didn't get the joke.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

My bad, I was looking at the wrong error:

I'm not sure I like that field, maybe you should remove it for now, or make it a simple Date field while you're learning?
Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 594
    
    3

In your create script of articles table, you declare it has year(4), and here you are trying to insert new Date(2009), that will result in Thu Jan 01 05:30:02 IST 1970.. try inserting only year.. it should work..

EDIT: Didn't notice David's reply.. Yes, you can do what david has suggested, for learning make the type as date..
agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56
Sorry dear programmers, i did not reply quick because I had a nap
Im surprised there are so many response.
You guys are great, let me try your solutions.
I will be back with you soon
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

I wondered where you went
agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56
My dear programmers

At last, I would like to end this month with a great news for all of you that "The Hibernate is working!!!" yayyyyyy
Now i see a light at the end of the tunnel

anyway so, I "Alter table Articles drop copyright" as most of you suggested me to.

I managed to delete the unnecesary copyright variable in the Articles.hbm.xml and Articles.java

Many thanks to David, Arun and Prasad for your help.
You guys saved me from having a nightmare.


The latest Articles.hbm.xml



the latest Articles.java

agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56
So, how could you insert a variable with a data type "Date" into a database using Hibernate?

That is interesting.

Or should i make a new thread?
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Probably a new thread.
If you only wanted to store a (single) year value and weren't concerned about filtering, sorting or selecting the value then a varchar(4) might suffice. It's larger than a Date but easy to read and harder to get wrong than a Date-which-only-has-a-year
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

Nice to hear you had it working. Pleasure was ours.
You might want to turn on "hibernate.show_sql=true" in your config file and actually inspect the query and what values are getting inserted.
I also faintly think that hibernate might show something (?, ?, ?, ?) while inserting instead of actual values, which I'm not very sure.
If it does, then excuse me.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simple Hibernate Help (Im Noob)