Hello,
I heard from Hibernate some time ago, but I didn't use it so far. Now I need it for a new project and I have to get used to Hibernate. Unfortunately I stuck already at the beginning of learning Hibernate. Although I linked all required libs (see below) and Eclipse does not show any errors, my programm does not find the Configuration Class (see Example1.java). I used the following tutorial:
http://www.roseindia.net/hibernate/firstexample.shtmlUnlike in the tutorial, I use the Hibernate Tools for Eclipse, however the generated XML files are almost identical.
Here is my java and XML code. I hope you can help me out...
Contact.javaCode:
package de.hda.test;
public class Contact {
private Long id;
private String firstname;
private String lastname;
private String email;
public void setId(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getFirstname() {
return firstname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getLastname() {
return lastname;
}
public void setEmail(String email) {
this.email = email;
}
public String getEmail() {
return email;
}
}
Example1.javaCode:
package de.hda.test;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class Example1 {
public static void main(String[] args) {
Session mySession = null;
try {
SessionFactory mySessionFactory = new Configuration().configure().buildSessionFactory(); // <-- Error!!! (ClassNotFoundException)
mySession = mySessionFactory.openSession();
System.out.println("Insert");
Contact contact = new Contact();
contact.setFirstname("Mustermann");
contact.setLastname("Max");
contact.setEmail("max@mustermann.de");
contact.setId(1L);
mySession.save(contact);
System.out.println("Done!");
}catch (Exception e)
{
System.out.println(e.getMessage());
}finally{
mySession.flush();
mySession.close();
}
}
}
hibernate.cfg.xmlCode:
<?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">com.ibm.db2.jcc.DB2Driver</property>
<property name="hibernate.connection.password">*****</property>
<property name="hibernate.connection.url">jdbc:db2://localhost:50000/HIBETEST</property>
<property name="hibernate.connection.username">*****</property>
<property name="hibernate.dialect">org.hibernate.dialect.DB2Dialect</property>
<!-- Mapping files -->
<mapping resource="de/hda/test/Contact.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Contact.hbm.xmlCode:
<?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 17.03.2011 15:06:17 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="de.hda.test.Contact" table="CONTACT">
<id name="id" type="java.lang.Long">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="firstname" type="java.lang.String">
<column name="FIRSTNAME" />
</property>
<property name="lastname" type="java.lang.String">
<column name="LASTNAME" />
</property>
<property name="email" type="java.lang.String">
<column name="EMAIL" />
</property>
</class>
</hibernate-mapping>
Actually nothing should be wrong in such a minimal sample, right?
I linked the following libs:
Code:
hibernate3.jar (Version 3.6.1)
hibernate-testing.jar
antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
javassist-3.12.0.GA.jar
jta-1.1.jar
slf4j-api-1.6.1.jar
db2jcc4.jar
Anybody an idea?
Thank you!
Stefan
PS: I get the following message:
Code:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
I could get rid of that message by linking the jar file "
log4j-over-slf4j-1.6.1.jar" instead of "
slf4j-api-1.6.1.jar". I downloaded the file from the project page (
http://www.slf4j.org/download.html).