-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 posts ] 
Author Message
 Post subject: Need help with creating Hibernate Session.
PostPosted: Mon Nov 27, 2006 4:47 pm 
Newbie

Joined: Mon Nov 27, 2006 4:15 pm
Posts: 5
I am very very new to Hibernate and I am going through the Hibernate Documentation trying to follow the exercise using Tomcat. I have a HibernateUtil class that uses the Hibernate SessionFactory for creating Hibernate Session. I have a Cat.java and I have a Test.java that has the code that uses the HibernateUtil to get the Hibernate session to try to persist to the Cat table using the setters in Cat. I was able to successfully compile all these classes. I have also added all the hibernate related jars in the classpath and also added Cat explicitly in the classpath(I am not really sure if this is required - it sure did not help solve my problem). However, when I try to run the Test class, I get the error posted below. I have been struggling to get this to work for a long time now. Please help!

Nov 27, 2006 1:48:47 PM net.sf.hibernate.cfg.Environment <clinit>
INFO: Hibernate 2.1.4
Nov 27, 2006 1:48:47 PM net.sf.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
Nov 27, 2006 1:48:47 PM net.sf.hibernate.cfg.Environment <clinit>
INFO: using CGLIB reflection optimizer
Nov 27, 2006 1:48:47 PM net.sf.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
Nov 27, 2006 1:48:47 PM net.sf.hibernate.cfg.Configuration getConfigurationInput
Stream
INFO: Configuration resource: /hibernate.cfg.xml
Nov 27, 2006 1:48:47 PM net.sf.hibernate.cfg.Configuration addResource
INFO: Mapping resource: Cat.hbm.xml
Nov 27, 2006 1:48:47 PM HibernateUtil <clinit>
SEVERE: Initial SessionFactory creation failed.
java.lang.NoClassDefFoundError: own/sf/hibernate/examples/quickstart/Cat (wrong
name: Cat)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
4)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at net.sf.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:9
9)
at net.sf.hibernate.cfg.Binder.bindClass(Binder.java:81)
at net.sf.hibernate.cfg.Binder.bindRootClass(Binder.java:221)
at net.sf.hibernate.cfg.Binder.bindRoot(Binder.java:1243)
at net.sf.hibernate.cfg.Configuration.add(Configuration.java:249)
at net.sf.hibernate.cfg.Configuration.addInputStream(Configuration.java:
285)
at net.sf.hibernate.cfg.Configuration.addResource(Configuration.java:333
)
at net.sf.hibernate.cfg.Configuration.doConfigure(Configuration.java:990
)
at net.sf.hibernate.cfg.Configuration.doConfigure(Configuration.java:946
)
at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:874)
at HibernateUtil.<clinit>(HibernateUtil.java:18)
at Test.main(Test.java:16)
[code][/code]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 6:01 pm 
Beginner
Beginner

Joined: Thu Oct 12, 2006 6:19 pm
Posts: 34
Location: Guatemala
Hi aniyathi, the message is indicating you the Cat class isn't in the dir specified in Cat.hbm.xml, It seems obvious but are you sure it is in own/sf/hibernate/examples/quickstart/ dir? and is it a POJO?

I hope it helps

_________________
God is Love


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 28, 2006 6:36 am 
Senior
Senior

Joined: Mon Oct 23, 2006 5:12 am
Posts: 141
Location: Galicia, Spain
Post Cat.hbm.xml, Cat.java and hibernate configuration

_________________
andresgr (--don't forget to rate)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 28, 2006 11:07 am 
Newbie

Joined: Mon Nov 27, 2006 4:15 pm
Posts: 5
In answer to halconGT, yes, Cat.java is a POJO and it is in
own/sf/hibernate/examples/quickstart/

I am attaching the Cat.hbm.xml, Cat.java and hibernate.cfg.xml.

Thanks!

Cat.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="own.sf.hibernate.examples.quickstart.Cat" table="cat" >
<id name="id" column="cat_id" unsaved-value="0">
<generator class="identity"/>
</id>


<property column="NAME" name="name" type="java.lang.String" />

<property column="SEX" name="sex" type="java.lang.String" />

<property column="WEIGHT" name="weight" type="java.lang.Float" />

</class>


</hibernate-mapping>


Cat.java

public class Cat {

private String id;
private String name;
private String sex;
private float weight;

public Cat() {
}

public String getId() {
return id;
}

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

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

public float getWeight() {
return weight;
}

public void setWeight(float weight) {
this.weight = weight;
}

}

hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD/EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<hibernate-configuration>

<session-factory>


<property name="connection.datasource">java:comp/env/jdbc/quickstart</property>
<property name="show_sql">false</property>
<property name="dialect">net.sf.hibernate.dialect.DB2400Dialect</property>
<property name="connection.driver_class">com.ibm.as400.access.AS400JDBCDriver</property>
<property name="connection.url">jdbc:as400://MW400T</property>
<property name="connection.username">odbccon</property>
<property name="connection.password">odbccon</property>
<property name="default_schema">maydb</property>

<mapping resource="Cat.hbm.xml" />


</session-factory>

</hibernate-configuration>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 28, 2006 11:13 am 
Senior
Senior

Joined: Mon Oct 23, 2006 5:12 am
Posts: 141
Location: Galicia, Spain
Did you put post Cat.java entirely?

Maybe your problem is you must put Cat.java in the package xxx:

package own.sf.hibernate.examples.quickstart.Cat;

public class Cat{

etc-

_________________
andresgr (--don't forget to rate)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 28, 2006 11:40 am 
Beginner
Beginner

Joined: Thu Oct 12, 2006 6:19 pm
Posts: 34
Location: Guatemala
Hi again guys...
aniyathi have you tested adding the entire resource path in:
Code:
<mapping resource="Cat.hbm.xml" />
?

Thanks

_________________
God is Love


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 28, 2006 12:58 pm 
Newbie

Joined: Mon Nov 27, 2006 4:15 pm
Posts: 5
The Cat.hbm.xml is directly under web-inf/classes. So I believe I have given the full path(?).

I did add the package names at the top and something interesting is happening now. I am still getting the NoClassDefFoundError, but it is for the Test.class. I checked the directory and the class file is there. Should I add this specifically to the classpath? I am going to try that. But this is what I am getting right now.

C:\apache-tomcat-6.0.1\webapps\quickstart\WEB-INF\classes\own\sf\hibernate\examp
les\quickstart>javac Cat.java

C:\apache-tomcat-6.0.1\webapps\quickstart\WEB-INF\classes\own\sf\hibernate\examp
les\quickstart>javac HibernateUtil.java
Note: HibernateUtil.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

C:\apache-tomcat-6.0.1\webapps\quickstart\WEB-INF\classes\own\sf\hibernate\examp
les\quickstart>javac Test.java

C:\apache-tomcat-6.0.1\webapps\quickstart\WEB-INF\classes\own\sf\hibernate\examp
les\quickstart>java Test
Exception in thread "main" java.lang.NoClassDefFoundError: Test (wrong name: own
/sf/hibernate/examples/quickstart/Test)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
4)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 28, 2006 1:27 pm 
Newbie

Joined: Mon Nov 27, 2006 4:15 pm
Posts: 5
Adding the class explicitly to the classpath did not really help.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 28, 2006 1:30 pm 
Senior
Senior

Joined: Mon Oct 23, 2006 5:12 am
Posts: 141
Location: Galicia, Spain
Hey, a generic suggestion: use an IDE like Netbeans or Eclipse, and not javac.

They'll help you with this classpath etc. issues

_________________
andresgr (--don't forget to rate)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 28, 2006 1:33 pm 
Beginner
Beginner

Joined: Thu Oct 12, 2006 6:19 pm
Posts: 34
Location: Guatemala
Hi again aniyathi, r u adding your Test.hbm.xml resource through hibernate.cfg.xml or in your java code?

Thanks

_________________
God is Love


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 28, 2006 3:24 pm 
Newbie

Joined: Mon Nov 27, 2006 4:15 pm
Posts: 5
I do not have a Test.hbm.xml, I only have Cat.hbm.xml and yes, I am adding that through the hibernate.cfg.xml


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.