Hibernate begineer error


Hello there,

I am beginning to learn hibernate and i got my first error and i dont seem to find the cause. the error i get is
Initial SessionFactory creation failed.org.hibernate.MappingNotFoundException: resource: User.hbm.xml not found
Exception in thread "main" java.lang.ExceptionInInitializerError
      at src.hib.HibernateUtil.buildSessionFactory(HibernateUtil.java:21)
      at src.hib.HibernateUtil.<clinit>(HibernateUtil.java:8)
      at src.hib.Test.addUser(Test.java:43)
      at src.hib.Test.main(Test.java:15)
Caused by: org.hibernate.MappingNotFoundException: resource: User.hbm.xml not found
      at org.hibernate.cfg.Configuration.addResource(Configuration.java:596)
      at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1621)
      at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1589)
      at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1568)
      at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1542)
      at org.hibernate.cfg.Configuration.configure(Configuration.java:1462)
      at org.hibernate.cfg.Configuration.configure(Configuration.java:1448)
      at src.hib.HibernateUtil.buildSessionFactory(HibernateUtil.java:15)
      ... 3 more

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Oct 25, 2011 9:56:38 AM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="src.hib.User" table="USER">
        <id name="id" type="int">
            <column name="ID" />
            <generator class="native" />
        </id>
        <property name="firstName">
            <column name="FIRSTNAME" />
        </property>
        <property name="lastName">
            <column name="LASTNAME" />
        </property>
    </class>
</hibernate-mapping>

==============================================

package src.hib;

public class User
{
	private int	id;
	private String	firstName;
	private String	lastName;

	public int getId()
	{
		return id;
	}

	public void setId(int id)
	{
		this.id = id;
	}

	public String getFirstName()
	{
		return firstName;
	}

	public void setFirstName(String firstName)
	{
		this.firstName = firstName;
	}

	public String getLastName()
	{
		return lastName;
	}

	public void setLastName(String lastName)
	{
		this.lastName = lastName;
	}

}

====================================

<?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="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:jtds:sqlserver://localhost:1433;DatabaseName=hibtest</property>
		<property name="hibernate.connection.username">test</property>
		<property name="hibernate.connection.password">secret=</property>
		<property name="hibernate.dialect">custom.java.hibernate.dialect.UnicodeSQLServerDialect</property>
		<property name="hbm2ddl.auto">update</property>
		<property name="hibernate.show_sql">true</property>
		<property name="hibernate.format_sql">true</property>

		<!-- Mapping files -->
		<mapping resource="User.hbm.xml" />
		<mapping resource="task.hbm.xml" />
		
	</session-factory>
</hibernate-configuration>

Open in new window

zolfAsked:
Who is Participating?
 
chaitu chaituCommented:
put Mapping files  in WEB-INF folder.
currently where did u place this files?
0
 
chaitu chaituCommented:
r u running this example in a standalone app or web aplication;

if standalone app check whether mapping files are exists or not in the particular folder
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
zolfAuthor Commented:

>>r u running this example in a standalone app or web aplication;

standalone app

>>if standalone app check whether mapping files are exists or not in the particular folder
it is present
0
 
zolfAuthor Commented:

ok, i managed to get solve that issue(i had to give the path in the cfg.xml file as well). now i am getting aother error

org.hibernate.exception.GenericJDBCException: Cannot open connection
      at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:126)
      at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:114)
      at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
      at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)
      at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449)
      at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
      at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:142)
      at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:85)
      at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1353)
      at src.hib.Test.addUser(Test.java:46)
      at src.hib.Test.main(Test.java:15)
Caused by: java.sql.SQLException: Login failed for user 'test'.
      at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:368)
      at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2820)
      at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2258)
      at net.sourceforge.jtds.jdbc.TdsCore.login(TdsCore.java:603)
      at net.sourceforge.jtds.jdbc.ConnectionJDBC2.<init>(ConnectionJDBC2.java:345)
      at net.sourceforge.jtds.jdbc.ConnectionJDBC3.<init>(ConnectionJDBC3.java:50)
      at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184)
      at java.sql.DriverManager.getConnection(Unknown Source)
      at java.sql.DriverManager.getConnection(Unknown Source)
      at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:133)
      at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
      ... 6 more
0
 
for_yanCommented:
I think you hibernate.cfg.xml should be in the root of your source and full path to User.hbm.xml and task.hbm.xml should be splled out
if those are in pacakges the path should be reproduces in mapping resource and those xml files should follow the potential java files ofr these
classes
0
 
for_yanCommented:
This is  perhaps something with your schema passoword so login to database failed
0
 
chaitu chaituCommented:
0
 
zolfAuthor Commented:
for_yan:

i changed my mapping like this and it worked.thanks for it anyway. but now i am getting login fail error. i am sure i have created this user for the db

<!-- Mapping files -->
            <mapping resource="src/hib/User.hbm.xml" />
            <mapping resource="src/hib/task.hbm.xml" />
0
 
for_yanCommented:
can you connect to the same databses with thesame schema outside hibernate ?
0
 
for_yanConnect With a Mentor Commented:
is the password "secret=" with equal sign there ?
0
 
for_yanCommented:
don't you have any way to get to that schema form sql prompt ?
If not maybe you can try to write a small progam with jdbc connection with the same credentials
to chek it ?
0
 
zolfAuthor Commented:

cheers,please bear with me i am trying to run my first hibernate app and i am stumbling on the way but getting near. now i get another error

Hibernate:
    insert
    into
        USER
        (FIRSTNAME, LASTNAME)
    values
        (?, ?)
org.hibernate.exception.GenericJDBCException: could not insert: [src.hib.User]
      at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:126)
      at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:114)
      at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
      at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:64)
      at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2186)
      at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2666)
      at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71)
      at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
      at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:321)
      at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
      at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130)
      at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
      at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
      at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
      at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
      at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
      at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:562)
      at org.hibernate.impl.SessionImpl.save(SessionImpl.java:550)
      at org.hibernate.impl.SessionImpl.save(SessionImpl.java:546)
      at src.hib.Test.addUser(Test.java:50)
      at src.hib.Test.main(Test.java:15)
Caused by: java.sql.SQLException: Incorrect syntax near the keyword 'USER'.
      at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:368)
      at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2820)
      at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2258)
      at net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(TdsCore.java:632)
      at net.sourceforge.jtds.jdbc.JtdsStatement.processResults(JtdsStatement.java:584)
      at net.sourceforge.jtds.jdbc.JtdsStatement.executeSQL(JtdsStatement.java:546)
      at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.executeUpdate(JtdsPreparedStatement.java:504)
      at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:94)
      at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57)
      ... 17 more
Exception in thread "main" org.hibernate.AssertionFailure: null id in src.hib.User entry (don't flush the Session after an exception occurs)
      at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:78)
      at org.hibernate.event.def.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:187)
      at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:143)
      at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:219)
      at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
      at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:49)
      at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
      at src.hib.Test.addUser(Test.java:63)
      at src.hib.Test.main(Test.java:15)
0
 
chaitu chaituCommented:

i think in USER table id is mandatory.check columns..
insert
    into
        USER
        (id,FIRSTNAME, LASTNAME)
    values
        (1,?, ?)
0
 
for_yanCommented:
Was it hibernate who was supposed to create the table ?
If so, can you check indpendently if the table was created ?
0
 
zolfAuthor Commented:

chaituu:

in the db, only one table is created and is called TASK,the other table USER has not been created. the table TASK is empty
0
 
chaitu chaituCommented:
either create table USER with columns ID,FIRSTNAME,LASTNAME or change the

<class name="src.hib.User" table="TASK">
0
 
for_yanCommented:
That is strange - maybe some problem in User.hbm.xml
does it matter that this is upper case, another is lower case in the xml name of the faile?
It probably should not matter
0
 
chaitu chaituCommented:
in table TASK create column names..
0
 
zolfAuthor Commented:

in table TASK create column names..

what do you mean
0
 
zolfAuthor Commented:

ok,got it. MSSQL does not like to have a table by the name USER, i changed it to USERS and it worked.phew,at last managed to run my first hibernate app
0
 
chaitu chaituCommented:
great..
0
 
for_yanCommented:
very good! Yes, inedeed USER is reserved word in mand db
0
 
zolfAuthor Commented:
cheers
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.