?
Solved

Hibernate begineer error

Posted on 2011-10-25
24
Medium Priority
?
1,694 Views
Last Modified: 2012-05-12

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

0
Comment
Question by:zolf
  • 8
  • 8
  • 8
24 Comments
 
LVL 20

Expert Comment

by:chaitu chaitu
ID: 37022752
put Mapping files  in WEB-INF folder.
currently where did u place this files?
0
 
LVL 20

Expert Comment

by:chaitu chaitu
ID: 37022758
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
 

Author Comment

by:zolf
ID: 37022769

>>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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:zolf
ID: 37022781

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
 
LVL 47

Expert Comment

by:for_yan
ID: 37022783
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
 
LVL 47

Expert Comment

by:for_yan
ID: 37022791
This is  perhaps something with your schema passoword so login to database failed
0
 
LVL 20

Expert Comment

by:chaitu chaitu
ID: 37022794
0
 

Author Comment

by:zolf
ID: 37022796
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
 
LVL 47

Expert Comment

by:for_yan
ID: 37022797
can you connect to the same databses with thesame schema outside hibernate ?
0
 
LVL 20

Accepted Solution

by:
chaitu chaitu earned 1000 total points
ID: 37022800
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 1000 total points
ID: 37022801
is the password "secret=" with equal sign there ?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37022807
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
 

Author Comment

by:zolf
ID: 37022826

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
 
LVL 20

Expert Comment

by:chaitu chaitu
ID: 37022833

i think in USER table id is mandatory.check columns..
insert
    into
        USER
        (id,FIRSTNAME, LASTNAME)
    values
        (1,?, ?)
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37022845
Was it hibernate who was supposed to create the table ?
If so, can you check indpendently if the table was created ?
0
 

Author Comment

by:zolf
ID: 37022848

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
 
LVL 20

Expert Comment

by:chaitu chaitu
ID: 37022870
either create table USER with columns ID,FIRSTNAME,LASTNAME or change the

<class name="src.hib.User" table="TASK">
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37022872
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
 
LVL 20

Expert Comment

by:chaitu chaitu
ID: 37022875
in table TASK create column names..
0
 

Author Comment

by:zolf
ID: 37022893

in table TASK create column names..

what do you mean
0
 

Author Comment

by:zolf
ID: 37022904

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
 
LVL 20

Expert Comment

by:chaitu chaitu
ID: 37022910
great..
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37022917
very good! Yes, inedeed USER is reserved word in mand db
0
 

Author Closing Comment

by:zolf
ID: 37022920
cheers
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month14 days, 9 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question