Hallo,
ich habe ein Problem bei meinem Hibernate Testprogramm.
21:40:08,789 INFOEnvironment:564 - Hibernate 3.5.0-Final
21:40:08,798 INFOEnvironment:597 - hibernate.properties not found
21:40:08,805 INFOEnvironment:775 - Bytecode provider name : javassist
21:40:08,811 INFOEnvironment:656 - using JDK 1.4 java.sql.Timestamp handling
21:40:08,950 INFOConfiguration:1518 - configuring from resource: /hibernate.cfg.xml
21:40:08,950 INFOConfiguration:1495 - Configuration resource: /hibernate.cfg.xml
21:40:09,067 INFOConfiguration:655 - Reading mappings from resource : de/Honey.hbm.xml
21:40:09,260 INFOHbmBinder:348 - Mapping class: de.Honey -> honey
21:40:09,280 INFOConfiguration:655 - Reading mappings from resource : de/Bee.hbm.xml
21:40:09,323 INFOHbmBinder:348 - Mapping class: de.Bee -> Bee
21:40:09,416 INFOConfiguration:1633 - Configured SessionFactory: null
21:40:09,423 INFOHbmBinder:2473 - Mapping collection: de.Honey.bees -> honey
21:40:09,447 INFODriverManagerConnectionProvider:64 - Using Hibernate built-in connection pool (not for production use!)
21:40:09,447 INFODriverManagerConnectionProvider:65 - Hibernate connection pool size: 20
21:40:09,448 INFODriverManagerConnectionProvider:68 - autocommit mode: false
21:40:09,452 INFODriverManagerConnectionProvider:103 - using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost/hibernatetest
21:40:09,454 INFODriverManagerConnectionProvider:109 - connection properties: {user=root, password=****}
21:40:09,640 INFOSettingsFactory:117 - RDBMS: MySQL, version: 5.1.37
21:40:09,640 INFOSettingsFactory:118 - JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.0.8 ( Revision: ${svn.Revision} )
21:40:09,658 INFODialect:222 - Using dialect: org.hibernate.dialect.MySQLDialect
21:40:09,662 INFOJdbcSupportLoader:79 - Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
21:40:09,663 INFOTransactionFactoryFactory:62 - Transaction strategy: org.hibernate.transaction.JDBCTransactionFactory
21:40:09,664 INFOTransactionManagerLookupFactory:80 - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
21:40:09,664 INFOSettingsFactory:169 - Automatic flush during beforeCompletion(): disabled
21:40:09,664 INFOSettingsFactory:173 - Automatic session close at end of transaction: enabled
21:40:09,664 INFOSettingsFactory:180 - JDBC batch size: 15
21:40:09,664 INFOSettingsFactory:183 - JDBC batch updates for versioned data: disabled
21:40:09,665 INFOSettingsFactory:188 - Scrollable result sets: enabled
21:40:09,665 INFOSettingsFactory:196 - JDBC3 getGeneratedKeys(): enabled
21:40:09,665 INFOSettingsFactory:204 - Connection release mode: auto
21:40:09,666 INFOSettingsFactory:228 - Maximum outer join fetch depth: 2
21:40:09,666 INFOSettingsFactory:231 - Default batch fetch size: 1
21:40:09,666 INFOSettingsFactory:235 - Generate SQL with comments: disabled
21:40:09,666 INFOSettingsFactory:239 - Order SQL updates by primary key: disabled
21:40:09,666 INFOSettingsFactory:243 - Order SQL inserts for batching: disabled
21:40:09,666 INFOSettingsFactory:410 - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
21:40:09,668 INFOASTQueryTranslatorFactory:47 - Using ASTQueryTranslatorFactory
21:40:09,668 INFOSettingsFactory:251 - Query language substitutions: {}
21:40:09,668 INFOSettingsFactory:256 - JPA-QL strict compliance: disabled
21:40:09,668 INFOSettingsFactory:261 - Second-level cache: enabled
21:40:09,668 INFOSettingsFactory:265 - Query cache: disabled
21:40:09,671 INFOSettingsFactory:395 - Cache region factory : org.hibernate.cache.impl.bridge.RegionFactoryCacheProviderBridge
21:40:09,671 INFORegionFactoryCacheProviderBridge:61 - Cache provider: org.hibernate.cache.EhCacheProvider
Exception in thread "main" java.lang.ExceptionInInitializerError
at de.Test.createRelation(Test.java:73)
at de.Test.main(Test.java:24)
Caused by: org.hibernate.HibernateException: could not instantiate RegionFactory [org.hibernate.cache.impl.bridge.RegionFactoryCacheProviderBridge]
at org.hibernate.cfg.SettingsFactory.createRegionFactory(SettingsFactory.java:402)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:270)
at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2163)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2159)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1383)
at hibernate.InitSessionFactory.<clinit>(InitSessionFactory.java:30)
... 2 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.hibernate.cfg.SettingsFactory.createRegionFactory(SettingsFactory.java:397)
... 7 more
Caused by: java.lang.NoClassDefFoundError: net/sf/ehcache/CacheException
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2389)
at java.lang.Class.getConstructor0(Class.java:2699)
at java.lang.Class.newInstance0(Class.java:326)
at java.lang.Class.newInstance(Class.java:308)
at org.hibernate.cache.impl.bridge.RegionFactoryCacheProviderBridge.<init>(RegionFactoryCacheProviderBridge.java:63)
... 12 more
Caused by: java.lang.ClassNotFoundException: net.sf.ehcache.CacheException
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:315)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:330)
at java.lang.ClassLoader.loadClass(ClassLoader.java:250)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:398)
... 18 moreDer Fehler wird durch diese Zeile verursacht:
Code:
Session session = InitSessionFactory.getInstance().getCurrentSession();
Meine Testklasse:
Code:
package de;
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.*;
import org.hibernate.*;
import org.hibernate.transform.DistinctRootEntityResultTransformer;
import hibernate.InitSessionFactory;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class Test {
private static Logger log = Logger.getLogger(Test.class);
public static void main(String[] args) {
try {
//clean();
//createHoney();
createRelation();
//delete();
//update();
//query();
//initBees();
} catch (RuntimeException e) {
try {
Session session = InitSessionFactory.getInstance().getCurrentSession();
if(session.getTransaction().isActive())
session.getTransaction().rollback();
} catch (HibernateException el) {
log.error("Fehler beim Rollback der Transaktion");
}
throw e;
}
}
//Zeigt ein INSERT INTO
private static Honey createHoney() {
//Honig anlegen
Honey tollerHonig = new Honey();
tollerHonig.setName("Waldhonig");
tollerHonig.setTaste("sehrsuess");
//Session und Transaktion initialisieren & vorbereiten
Session session = InitSessionFactory.getInstance().getCurrentSession();
Transaction tx = session.getTransaction();
session.save(tollerHonig);
//abgeht die Post
tx.commit();
return tollerHonig;
}
//Zeigt update auf Datensatz
private static void update() {
Honey honig = createHoney();
Session session = InitSessionFactory.getInstance().getCurrentSession();
Transaction tx = session.getTransaction();
honig.setName("ModernerMix");
session.update(honig);
tx.commit();
}
//Zeigt wie Beziehungen zwischen den Objekten entstehen
private static void createRelation() {
Session session = InitSessionFactory.getInstance().getCurrentSession();
Transaction tx = session.beginTransaction();
Honey honig = new Honey();
honig.setName("OdenwaldHonig");
honig.setTaste("Herrlich, geschmeidig im Abgang");
session.save(honig);
Bee biene = new Bee("Sonja");
session.save(biene);
// Beziehung auf beiden Seiten erstellen
biene.setHoney(honig);
honig.getBees().add(biene);
tx.commit();
}
private static void query() {
Session session = InitSessionFactory.getInstance().getCurrentSession();
Transaction tx = session.beginTransaction();
List honigListe = session.createQuery("SELECT h from Honey as h").list();
for(Iterator iter = honigListe.iterator(); iter.hasNext();) {
Honey element = (Honey) iter.next();
log.debug(element);
}
tx.commit();
}
}
InitSessionFactory:
Code:
package hibernate;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class InitSessionFactory {
private static org.hibernate.SessionFactory sessionFactory;
/**
* Default constructor. It is private to guaranty singleton
*/
private InitSessionFactory() {
}
static {
final Configuration cfg = new Configuration();
// final AnnotationConfiguration cfg = new AnnotationConfiguration();
/*
* change the cfg to a annotation configuration, if you want to use
* Hibernate annotations as well. Annotation configuration supports both
* XML and annotations
*/
/*
* configure Hibernate from hibernate.cfg.xml. This is expected to be in
* the class path = "src folder" = "root folder of classes"
*/
cfg.configure("/hibernate.cfg.xml");
cfg.buildSessionFactory();
sessionFactory = cfg.buildSessionFactory();
}
/**
* Returns the single instance of the session factory
*
* @return
*/
public static SessionFactory getInstance() {
return sessionFactory;
}
}
Honey.java
Code:
package de;
import java.io.Serializable;
import java.text.MessageFormat;
import java.util.HashSet;
import java.util.Set;
public class Honey implements Serializable {
private int id;
private String name;
private String taste;
private Set<Bee> bees = new HashSet<Bee>();
public Honey() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTaste() {
return taste;
}
public void setTaste(String taste) {
this.taste = taste;
}
public Set<Bee> getBees() {
return bees;
}
public void setBees(Set<Bee> bees) {
this.bees = bees;
}
public String toString() {
return MessageFormat.format("Honey: {0} {1} {2}", new Object[] {id, name, taste});
}
}
Bee.java
Code:
package de;
import java.io.Serializable;
import java.text.MessageFormat;
public class Bee implements Serializable {
private int id;
private String name;
private Honey honey;
public Bee() {
}
public Bee(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Honey getHoney() {
return honey;
}
public void setHoney(Honey honey) {
this.honey = honey;
}
public String toString() {
return MessageFormat.format("{0}: id={1}, name={2}", new Object[] { getClass().getSimpleName(), id, name});
}
}
hibernate.cfg.xml
Code:
<?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="connection.url">jdbc:mysql://localhost/hibernatetest</property>
<property name="connection.username">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.password"></property>
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="transaction.auto_close_session">true</property>
<!-- zeige alle sql statements -->
<property name="show_sql">true</property>
<!-- erstellt die tabellen in der datenbank -->
<property name="hbm2ddl.auto">update</property>
<mapping resource="de/Honey.hbm.xml" />
<mapping resource="de/Bee.hbm.xml" />
</session-factory>
</hibernate-configuration>
Bee.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="de">
<class name="Bee" table="bee">
<id name="id">
<generator class="native" />
</id>
<property name="name" type="string"></property>
<many-to-one name="honey"></many-to-one>
</class>
</hibernate-mapping>
Honey.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="de">
<class name="Honey" table="honey">
<id name="id" column="id">
<generator class="native" />
</id>
<property name="name" type="string"></property>
<property name="taste" type="string"></property>
<set name="bees" inverse="true">
<key column="honey_id"></key>
<one-to-many class="Honey" />
</set>
</class>
</hibernate-mapping>
Kann mir jemand bei der Fehlersuche helfen?
Vielen Dank im Voraus.