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

hibernate hello world

naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

hi ranchers,

i am creating a java project in eclipse using the hibernate 3.0


i do not know where to put the mapping configuration file (applabsuser.hbm.xml)
and
hibernate configuration file (hibernate.cfg.xml)



i have my POJO class in src\org.applabs.quickstart\AppLabsUser.java
Madhavi Subramaniam
Ranch Hand

Joined: Nov 24, 2011
Posts: 33
Hi naveen,

you can have your all mapping configuration file in same project but in different package.

But you've to configure the mapping correctly.

For eg, you moving hbm file in src\org.applabs.quickstart.hbm\applabsuser.hbm.xml

you've to configure mapping in hibernate.cfg.xml as follows

<mapping resource="org\applabs\quickstart\hbm\applabsuser.hbm.xml " />


you can put hibernate.cfg.xml file where ever you want. But while reading this file for creating session factory, you should give exact file location.




Thanks.





naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

there is how i palce various file in Hibernate 3 hello application


mapping file in src\org.applabs.quickstart\hbm\applabsuser.hbm.xml



and

hibernate cong file in \src\hibernate.cfg.xml





here is my main class



and on running above file i am getting
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" java.lang.NullPointerException
at org.applabs.quickstart.FirstExample.main(FirstExample.java:39)
Resource: applabsuser.hbm.xml not found


it seems that session object has not been instantiated. but why ?
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384



Please note that i have included only hibernate3.jar in build-path Do i have to add other jar files ?

Madhavi Subramaniam
Ranch Hand

Joined: Nov 24, 2011
Posts: 33
Hi,

Have you created session factory for your database connection?
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

here it is

hibernate.cfg.xml


and

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
problem parsing configuration/hibernate.cfg.xml

Exception in thread "main" java.lang.NullPointerException
at org.applabs.quickstart.FirstExample.main(FirstExample.java:35)


what is problem in above xml file ?
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

Madhavi Subramaniam wrote:Hi,

Have you created session factory for your database connection?



how do i do that ?
Madhavi Subramaniam
Ranch Hand

Joined: Nov 24, 2011
Posts: 33
Hi,

Have you added sqljdbc.jar in your build path?
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

i have added mysql-connector-java-5.1.6-bin.jar in build-path

and i have added all jar files which comes with hibernate3 distribution.
Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 507

Post the full exception stack trace. It should have the details on what is wrong in the XML file.


Regards, Prasad
SCJP 5 (93%)
Madhavi Subramaniam
Ranch Hand

Joined: Nov 24, 2011
Posts: 33
Hi,

I think,your connnection url value in wrong format.

If your DB name is university,It should be like

<property name="hibernate.connection.url">jdbc:sqlserver://localhost:3306;databaseName=university</property>

3306 is your sql server running port number rite?
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

here is hibernate.cfg.xml




irrespective of <property name="hibernate.connection.url">jdbc:sqlserver://localhost:3306;databaseName=university</property> still throws exception

and full stack trace of exception.

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
org.hibernate.HibernateException: problem parsing configuration/hibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1222)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1161)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1147)
at org.applabs.quickstart.FirstExample.main(FirstExample.java:18)
Caused by: org.dom4j.DocumentException: Error on line 2 of document : The processing instruction target matching "[xX][mM][lL]" is not allowed. Nested [/b]exception: The processing instruction target matching "[xX][mM][lL]" is not allowed.
at org.dom4j.io.SAXReader.read(SAXReader.java:350)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1217)
... 3 more
Exception in thread "main" java.lang.NullPointerException
at org.applabs.quickstart.FirstExample.main(FirstExample.java:38)



Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 507

This means there are some white spaces before the XML declaration..
Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 507

Madhavi Subramaniam wrote:Hi,

I think,your connnection url value in wrong format.

If your DB name is university,It should be like

<property name="hibernate.connection.url">jdbc:sqlserver://localhost:3306;databaseName=university</property>



URL is in correct format, it should work with what the OP is having.
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384





log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
org.hibernate.MappingException: Error reading resource: org/applabs/quickstart/applabsuser.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:448)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1312)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1284)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1266)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1233)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1161)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1147)
at org.applabs.quickstart.FirstExample.main(FirstExample.java:15)
Caused by: org.hibernate.MappingException: class org.applabs.hibernate.quickstart.AppLabsUser not found while looking for property: userName
at org.hibernate.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.java:81)
at org.hibernate.mapping.SimpleValue.setTypeUsingReflection(SimpleValue.java:254)
at org.hibernate.cfg.HbmBinder.createProperty(HbmBinder.java:1753)
at org.hibernate.cfg.HbmBinder.createClassProperties(HbmBinder.java:1742)
at org.hibernate.cfg.HbmBinder.bindRootPersistentClassCommonValues(HbmBinder.java:317)
at org.hibernate.cfg.HbmBinder.bindRootClass(HbmBinder.java:235)
at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:151)
at org.hibernate.cfg.Configuration.add(Configuration.java:359)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:396)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:445)
... 7 more
Caused by: java.lang.ClassNotFoundException: org.applabs.hibernate.quickstart.AppLabsUser
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:108)
at org.hibernate.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.java:77)
... 16 more
Exception in thread "main" java.lang.NullPointerException
at org.applabs.quickstart.FirstExample.main(FirstExample.java:32)


i have AppLabsUser.java in org.applabs.quickstart but there is not a AppLabsUser.class file.why it is not been created ?
Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 507

naveen yadav wrote:
Caused by: java.lang.ClassNotFoundException: org.applabs.hibernate.quickstart.AppLabsUser


This is the root cause. It cannot find the specified class.
Post your hbm file and the AppLabsUser.java file.
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384


applabsuser.hbm.xml in org.applabs.quickstart




and here is AppLabsUser.java in org.applabs.quickstart



naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

in applabsuser.hbm.xml

replace
<hibernate-mapping>
<class name="org.applabs.hibernate.quickstart.AppLabsUser" table="applabsuser">

with
<hibernate-mapping>
<class name="org.applabs.quickstart.AppLabsUser" table="applabsuser">

and now

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
org.hibernate.MappingException: could not instantiate id generator
at org.hibernate.id.IdentifierGeneratorFactory.create(IdentifierGeneratorFactory.java:94)
at org.hibernate.mapping.SimpleValue.createIdentifierGenerator(SimpleValue.java:135)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:166)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1054)
at org.applabs.quickstart.FirstExample.main(FirstExample.java:15)
Caused by: org.hibernate.MappingException: Dialect does not support sequences
at org.hibernate.dialect.Dialect.getSequenceNextValString(Dialect.java:413)
at org.hibernate.id.SequenceGenerator.configure(SequenceGenerator.java:65)
at org.hibernate.id.IdentifierGeneratorFactory.create(IdentifierGeneratorFactory.java:90)
... 4 more
Exception in thread "main" java.lang.NullPointerException
at org.applabs.quickstart.FirstExample.main(FirstExample.java:32)
Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 507

naveen yadav wrote:
and here is AppLabsUser.java in org.applabs.quickstart


But, what you give in hbm file, is org.applabs.hibernate.quickstart.AppLabsUser.
Change it to org.applabs.quickstart.AppLabsUser.
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384




now after change applabsuser.hbm.xml


and now

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
org.hibernate.MappingException: could not instantiate id generator
at org.hibernate.id.IdentifierGeneratorFactory.create(IdentifierGeneratorFactory.java:94)
at org.hibernate.mapping.SimpleValue.createIdentifierGenerator(SimpleValue.java:135)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:166)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1054)
at org.applabs.quickstart.FirstExample.main(FirstExample.java:15)
Caused by: org.hibernate.MappingException: Dialect does not support sequences
at org.hibernate.dialect.Dialect.getSequenceNextValString(Dialect.java:413)
at org.hibernate.id.SequenceGenerator.configure(SequenceGenerator.java:65)
at org.hibernate.id.IdentifierGeneratorFactory.create(IdentifierGeneratorFactory.java:90)
... 4 more
Exception in thread "main" java.lang.NullPointerException
at org.applabs.quickstart.FirstExample.main(FirstExample.java:32)
Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 507

It tells, the database does not support sequences. You can try with generator class="increment" to have it incremented automatically or "assigned" to assign the value yourself.
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384


once again i have changed applabsuser.hbm.xml




Caused by: java.sql.SQLException: No suitable driver found for jdbc:sqlserver://localhost:3306;databaseName=university
at java.sql.DriverManager.getConnection(DriverManager.java:602)
at java.sql.DriverManager.getConnection(DriverManager.java:154)
at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110)
at org.hibernate.jdbc.AbstractBatcher.openConnection(AbstractBatcher.java:410)
... 7 more


but i have another simple application using same database is running fine
Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 507

naveen yadav wrote:
Caused by: java.sql.SQLException: No suitable driver found for jdbc:sqlserver://localhost:3306;databaseName=university


Change the driver to one, you had earlier in hibernate.cfg.xml.
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

after doing chnges


log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
org.hibernate.PropertyNotFoundException: Could not find a getter for id in class org.applabs.quickstart.AppLabsUser
at org.hibernate.property.BasicPropertyAccessor.createGetter(BasicPropertyAccessor.java:213)
at org.hibernate.property.BasicPropertyAccessor.getGetter(BasicPropertyAccessor.java:207)
at org.hibernate.tuple.PropertyFactory.getGetter(PropertyFactory.java:148)
at org.hibernate.tuple.PropertyFactory.buildIdentifierProperty(PropertyFactory.java:41)
at org.hibernate.tuple.EntityMetamodel.<init>(EntityMetamodel.java:105)
at org.hibernate.persister.entity.BasicEntityPersister.<init>(BasicEntityPersister.java:400)
at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:104)
at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:55)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:199)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1054)
at org.applabs.quickstart.FirstExample.main(FirstExample.java:15)
Exception in thread "main" java.lang.NullPointerException
at org.applabs.quickstart.FirstExample.main(FirstExample.java:34)


in AppLabsUser.java

[code=java
int id;
public int getUserId() {
return this.id;
}

public void setUserId(int id) {
this.id = id;
}[/code]


in university database
id is represented by USER_ID --type is int

and here is how i insert user id from FirstExample.java(main class)

int ii=88;
user.setUserId(ii);
Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 507

You need to have setters/getters in the standard format. My suggestion is use your IDE, to generate them.
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

i have creted setter and getter method using IDE.

applabsuser table in database




main class



log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: update applabsuser set USER_NAME=?, USER_PASSWORD=?, USER_FIRST_NAME=?, USER_LAST_NAME=?, USER_EMAIL=? where USER_ID=?
org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1
at org.hibernate.jdbc.BatchingBatcher.checkRowCount(BatchingBatcher.java:88)
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:74)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:172)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:226)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:137)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:675)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:293)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at org.applabs.quickstart.FirstExample.main(FirstExample.java:28)


org.hibernate.StaleStateException
Thrown when a version number or timestamp check failed, indicating that the Session contained stale data (when using long transactions with versioning).
Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 507

Ideally, it should have been insert query, don't know, why its become update query. Can you check by making it session.save(user) instead of session.saveOrUpdate(user)
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

Prasad Krishnegowda wrote: Can you check by making it session.save(user) instead of session.saveOrUpdate(user)


with that it is like


log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: insert into applabsuser (USER_NAME, USER_PASSWORD, USER_FIRST_NAME, USER_LAST_NAME, USER_EMAIL) values (?, ?, ?, ?, ?)
org.hibernate.exception.GenericJDBCException: could not insert: [org.applabs.quickstart.AppLabsUser]
at org.hibernate.exception.ErrorCodeConverter.handledNonSpecificException(ErrorCodeConverter.java:92)
at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:80)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1790)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:2192)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:34)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java: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.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:184)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:173)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:429)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:424)
at org.applabs.quickstart.FirstExample.main(FirstExample.java:29)
Caused by: java.sql.SQLException: Field 'USER_ID' doesn't have a default value
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2542)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1734)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2019)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1937)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1922)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1772)
... 14 more
Exception in thread "main" org.hibernate.AssertionFailure: null id in entry (don't flush the Session after an exception occurs)
at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:49)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:82)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:190)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:70)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:675)
at org.applabs.quickstart.FirstExample.main(FirstExample.java:37)
Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 507

have you mapped user_id column, how is the value set to this field?
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

userId is a primary key in a table


USER_ID does not have default value since it is a primary key
i could not understand why there is Caused by: java.sql.SQLException: Field 'USER_ID' doesn't have a default value .Should i assign default value to it.

this is how i mapped user_id in applasuser.hbm.xml




and in FirstExample.java(main) trying to set a user_id.



Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 507

Hibernate: insert into applabsuser (USER_NAME, USER_PASSWORD, USER_FIRST_NAME, USER_LAST_NAME, USER_EMAIL) values (?, ?, ?, ?, ?)


This statement doesn't show the userid, it means there is some mistake in mapping the column of the table.
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384


USER_ID is a primary key and have been mapped to POJP property userId in applabsuser.xml


Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 507

can you change that class from native to assigned.. in the java code, you are assigning that. Native means, the underlying database only decides its value.
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

Prasad Krishnegowda wrote:can you change that class from native to assigned.. in the java code, you are assigning that. Native means, the underlying database only decides its value.


after doing suggested changes.



Now there is a new row is inserted into the a database table.finally Hibernate Hello World is working.


Thank you very much Prasad Krishnegowda. I appreciates your effort. you help it right through the code. You kindly help at every single error
thanks.

Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 507

Great.. Nice to hear, you finally made it work..
Welcome brother
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hibernate hello world
 
Similar Threads
where is the error
Need help in hibernate project setup.Gettng exception when try to create console configuration
problem in mapping hibernate class
Include Hibernate in Eclipse
Hibernate NullPointerException