Hallo,
ich möchte eine WebApp mit/für folgenden Mitteln entwickeln:
Webserver: Apache Tomcat 5.5.17
Java: Java SE 5.0
Logger: Log4J
DB: Mysql 5.0.22
und natürlich Hibernate (aktuelle Version) ;-)
Mein Problem befasst sich mit den Hibernate-Mapping XML-Dateien. Bevor ich hier lange mit eine Beschreibung ausholle, poste ich einfach einmal ein paar Codes stücke.
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="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.password">schule11</property>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/universe</property>
<property name="hibernate.connection.username">HibernateWeb</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">30</property>
<property name="hibernate.c3p0.timeout">2000</property>
<property name="hibernate.c3p0.max_statements">100</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="Abc.hbm.xml" ></mapping>
</session-factory>
</hibernate-configuration>
Abc.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">
<hibernate-mapping package="beans">
<class name="Abc" table="abc">
<id name="id" type="integer">
<column name="Id" />
<generator class="identity" />
</id>
<property name="a" type="integer">
<column name="a" not-null="true" />
</property>
</class>
</hibernate-mapping>
WebServerStartListenerCode:
package beans;
[...]
public class WebServerStartListener implements ServletContextListener {
[...]
public void contextInitialized(ServletContextEvent event) {
ServletContext context = event.getServletContext();
SessionFactory factory = null;
Logger logger;
/*
* Erstelle aktuelle ID
*/
context.setAttribute("ID", util.string.StringUtilities.createRandomString(64));
/*
* Logger instanzieren
*/
StringBuffer file = new StringBuffer(400);
file.append(context.getRealPath("/"));
file.append("conf");
file.append(System.getProperty("file.separator"));
file.append("config.xml");
DOMConfigurator.configureAndWatch(file.toString(), 60*1000);
logger = Logger.getRootLogger();
logger.log(Level.INFO, "WebApplication wird gestartet");
context.setAttribute("Logger", logger);
logger.log(Level.DEBUG, "Der Logger wurde erfolgreich gestartet");
/*
* Hibernate erstellen
*/
try {
WebsiteListener.erstelleHibernate(context);
factory = (SessionFactory) context.getAttribute("hibernate");
logger.log(Level.DEBUG, "Das Hibernate - Framework wurde erfolgreich gestartet");
} catch(HibernateException e) {
logger.log("listener.WebsiteListener", Level.FATAL, "Hibernate konnte nicht gestartet werden", e);
}
}
/**
* Stop
*
*/
public void contextDestroyed(ServletContextEvent event) {
ServletContext context = event.getServletContext();
Logger logger = (Logger) context.getAttribute("Logger");
/*
* Hibernate schließen
*/
try {
WebsiteListener.closeHibernate(context);
logger.log(Level.DEBUG, "Das Hibernate - Framework wurde erfolgreich beendet");
} catch(HibernateException e) {
logger.log("listener.WebsiteListener", Level.ERROR, "Beim beenden von Hibernate ist ein Fehler aufgetreten\n", e);
}
}
/**
* Gibt die SessionFactory zurück
*
* @param context
* @return
*/
public static final SessionFactory getSessionFactory(ServletContext context) {
return (SessionFactory) context.getAttribute("Hibernate");
}
/**
* Restart nach einem zusammenbruch der DB-Verbindung
*
* @param context
*/
public static final void restartHibernate(ServletContext context) throws HibernateException {
// Hibernate beenden
try {
WebsiteListener.closeHibernate(context);
} catch(Exception e) {
} finally {
context.setAttribute("Hibernate", null);
}
// Hibernate neustarten
try {
WebsiteListener.erstelleHibernate(context);
} catch(HibernateException e) {
}
/*
* SessionFactory aktuallisieren
*/
SessionFactory factory = (SessionFactory) context.getAttribute("Hibernate");
// Counter
((util.Counter) context.getAttribute("Counter")).setSessionFactory(factory);
}
/**
* Erstellt den ORMapper Hibernate
*
* @param context
*/
public static final void erstelleHibernate(ServletContext context) throws HibernateException {
StringBuffer buffer = new StringBuffer(400);
buffer.append(context.getRealPath("/"));
buffer.append("WEB-INF");
buffer.append(System.getProperty("file.separator"));
buffer.append("classes");
buffer.append(System.getProperty("file.separator"));
buffer.append("hibernate.cfg.xml");
File f = new File(buffer.toString());
org.hibernate.cfg.Configuration con = new org.hibernate.cfg.Configuration();
con.configure(f);
SessionFactory factory = con
.configure()
.buildSessionFactory();
context.setAttribute("Hibernate", factory);
}
/**
* Schließt dan ORMapper Hibernate
*
* @param context
*/
public static final void closeHibernate(ServletContext context) throws HibernateException {
SessionFactory factory = (SessionFactory) context.getAttribute("Hibernate");
factory.close();
}
}
Log-DateiCode:
2006-09-09 17:21:51,015 INFO [Thread-1] root: WebApplication wird gestartet
2006-09-09 17:21:51,015 DEBUG [Thread-1] root: Der Logger wurde erfolgreich gestartet
2006-09-09 17:21:51,359 INFO [Thread-1] org.hibernate.cfg.Environment: Hibernate 3.2 cr4
2006-09-09 17:21:51,375 INFO [Thread-1] org.hibernate.cfg.Environment: hibernate.properties not found
2006-09-09 17:21:51,375 INFO [Thread-1] org.hibernate.cfg.Environment: Bytecode provider name : cglib
2006-09-09 17:21:51,375 INFO [Thread-1] org.hibernate.cfg.Environment: using JDK 1.4 java.sql.Timestamp handling
2006-09-09 17:21:51,453 INFO [Thread-1] org.hibernate.cfg.Configuration: configuring from file: hibernate.cfg.xml
2006-09-09 17:21:51,500 DEBUG [Thread-1] org.hibernate.util.DTDEntityResolver: trying to resolve system-id [http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd]
2006-09-09 17:21:51,500 DEBUG [Thread-1] org.hibernate.util.DTDEntityResolver: recognized hibernate namespace; attempting to resolve on classpath under org/hibernate/
2006-09-09 17:21:51,500 DEBUG [Thread-1] org.hibernate.util.DTDEntityResolver: located [http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd] in classpath
2006-09-09 17:21:51,593 DEBUG [Thread-1] org.hibernate.cfg.Configuration: hibernate.connection.driver_class=org.gjt.mm.mysql.Driver
2006-09-09 17:21:51,593 DEBUG [Thread-1] org.hibernate.cfg.Configuration: hibernate.connection.password=schule11
2006-09-09 17:21:51,593 DEBUG [Thread-1] org.hibernate.cfg.Configuration: hibernate.connection.url=jdbc:mysql://127.0.0.1:3306/universe
2006-09-09 17:21:51,593 DEBUG [Thread-1] org.hibernate.cfg.Configuration: hibernate.connection.username=HibernateWeb
2006-09-09 17:21:51,593 DEBUG [Thread-1] org.hibernate.cfg.Configuration: hibernate.dialect=org.hibernate.dialect.MySQLDialect
2006-09-09 17:21:51,593 DEBUG [Thread-1] org.hibernate.cfg.Configuration: null<-org.dom4j.tree.DefaultAttribute@19b46dc [Attribute: name resource value "Abc.hbm.xml"]
2006-09-09 17:21:51,593 INFO [Thread-1] org.hibernate.cfg.Configuration: Reading mappings from resource: Abc.hbm.xml
2006-09-09 17:21:51,609 DEBUG [Thread-1] org.hibernate.util.DTDEntityResolver: trying to resolve system-id [http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd]
2006-09-09 17:21:51,609 DEBUG [Thread-1] org.hibernate.util.DTDEntityResolver: recognized hibernate namespace; attempting to resolve on classpath under org/hibernate/
2006-09-09 17:21:51,609 DEBUG [Thread-1] org.hibernate.util.DTDEntityResolver: located [http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd] in classpath
2006-09-09 17:21:51,984 INFO [Thread-1] org.hibernate.cfg.HbmBinder: Mapping class: beans.Abc -> abc
2006-09-09 17:21:51,984 DEBUG [Thread-1] org.hibernate.cfg.HbmBinder: Mapped property: id -> Id
2006-09-09 17:21:52,000 DEBUG [Thread-1] org.hibernate.cfg.HbmBinder: Mapped property: a -> a
2006-09-09 17:21:52,000 INFO [Thread-1] org.hibernate.cfg.Configuration: Configured SessionFactory: null
2006-09-09 17:21:52,000 DEBUG [Thread-1] org.hibernate.cfg.Configuration: properties: {hibernate.connection.password=schule11, java.runtime.name=Java(TM) 2 Runtime Environment, Standard Edition, sun.boot.library.path=C:\Server\Entwicklung\Scoure\Java\jdk\1.5.0.07\jre\bin, java.vm.version=1.5.0_07-b03, shared.loader=${catalina.base}/shared/classes,${catalina.base}/shared/lib/*.jar, hibernate.connection.username=HibernateWeb, java.vm.vendor=Sun Microsystems Inc., java.vendor.url=http://java.sun.com/, path.separator=;, java.vm.name=Java HotSpot(TM) Server VM, tomcat.util.buf.StringCache.byte.enabled=true, file.encoding.pkg=sun.io, java.util.logging.config.file=C:\Server\Servers\Apache\tomcat\5.5\conf\logging.properties, user.country=DE, sun.os.patch.level=Service Pack 2, java.vm.specification.name=Java Virtual Machine Specification, user.dir=C:\Server\Servers\Apache\tomcat\5.5, java.runtime.version=1.5.0_07-b03, java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment, java.endorsed.dirs=C:\Server\Servers\Apache\tomcat\5.5\common\endorsed, os.arch=x86, java.io.tmpdir=C:\Server\Servers\Apache\tomcat\5.5\temp, line.separator=
, java.vm.specification.vendor=Sun Microsystems Inc., user.variant=, java.util.logging.manager=org.apache.juli.ClassLoaderLogManager, java.naming.factory.url.pkgs=org.apache.naming, os.name=Windows XP, sun.jnu.encoding=Cp1252, java.library.path=C:\Server\Servers\Apache\tomcat\5.5\bin;.;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Programme\ATI Technologies\ATI Control Panel;C:\Programme\HPQ\IAM\bin;%JAVA_HOME%\bin;C:\Server\Servers\MySQL\5.0.22\bin;C:\Server\Servers\MySQL\5.0.22\bin;C:\Programme\QuickTime\QTSystem\, java.specification.name=Java Platform API Specification, java.class.version=49.0, sun.management.compiler=HotSpot Server Compiler, os.version=5.1, user.home=C:\, user.timezone=Europe/Berlin, catalina.useNaming=true, java.awt.printerjob=sun.awt.windows.WPrinterJob, file.encoding=Cp1252, java.specification.version=1.5, hibernate.connection.driver_class=org.gjt.mm.mysql.Driver, catalina.home=C:\Server\Servers\Apache\tomcat\5.5, java.class.path=C:\Server\Servers\Apache\tomcat\5.5\bin\bootstrap.jar, user.name=SYSTEM, hibernate.bytecode.use_reflection_optimizer=false, java.naming.factory.initial=org.apache.naming.java.javaURLContextFactory, package.definition=sun.,java.,org.apache.catalina.,org.apache.coyote.,org.apache.tomcat.,org.apache.jasper., java.vm.specification.version=1.0, java.home=C:\Server\Entwicklung\Scoure\Java\jdk\1.5.0.07\jre, sun.arch.data.model=32, hibernate.dialect=org.hibernate.dialect.MySQLDialect, hibernate.connection.url=jdbc:mysql://127.0.0.1:3306/universe, user.language=de, java.specification.vendor=Sun Microsystems Inc., awt.toolkit=sun.awt.windows.WToolkit, java.vm.info=mixed mode, java.version=1.5.0_07, java.ext.dirs=C:\Server\Entwicklung\Scoure\Java\jdk\1.5.0.07\jre\lib\ext, sun.boot.class.path=C:\Server\Entwicklung\Scoure\Java\jdk\1.5.0.07\jre\lib\rt.jar;C:\Server\Entwicklung\Scoure\Java\jdk\1.5.0.07\jre\lib\i18n.jar;C:\Server\Entwicklung\Scoure\Java\jdk\1.5.0.07\jre\lib\sunrsasign.jar;C:\Server\Entwicklung\Scoure\Java\jdk\1.5.0.07\jre\lib\jsse.jar;C:\Server\Entwicklung\Scoure\Java\jdk\1.5.0.07\jre\lib\jce.jar;C:\Server\Entwicklung\Scoure\Java\jdk\1.5.0.07\jre\lib\charsets.jar;C:\Server\Entwicklung\Scoure\Java\jdk\1.5.0.07\jre\classes, server.loader=${catalina.home}/server/classes,${catalina.home}/server/lib/*.jar, java.vendor=Sun Microsystems Inc., catalina.base=C:\Server\Servers\Apache\tomcat\5.5, file.separator=\, java.vendor.url.bug=http://java.sun.com/cgi-bin/bugreport.cgi, common.loader=${catalina.home}/common/classes,${catalina.home}/common/i18n/*.jar,${catalina.home}/common/endorsed/*.jar,${catalina.home}/common/lib/*.jar, sun.io.unicode.encoding=UnicodeLittle, sun.cpu.endian=little, package.access=sun.,org.apache.catalina.,org.apache.coyote.,org.apache.tomcat.,org.apache.jasper.,sun.beans., sun.desktop=windows, sun.cpu.isalist=pentium_pro+mmx pentium_pro pentium+mmx pentium i486 i386 i86}
2006-09-09 17:21:52,000 INFO [Thread-1] org.hibernate.cfg.Configuration: configuring from resource: /hibernate.cfg.xml
2006-09-09 17:21:52,000 INFO [Thread-1] org.hibernate.cfg.Configuration: Configuration resource: /hibernate.cfg.xml
2006-09-09 17:21:52,000 DEBUG [Thread-1] org.hibernate.util.DTDEntityResolver: trying to resolve system-id [http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd]
2006-09-09 17:21:52,000 DEBUG [Thread-1] org.hibernate.util.DTDEntityResolver: recognized hibernate namespace; attempting to resolve on classpath under org/hibernate/
2006-09-09 17:21:52,000 DEBUG [Thread-1] org.hibernate.util.DTDEntityResolver: located [http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd] in classpath
2006-09-09 17:21:52,015 DEBUG [Thread-1] org.hibernate.cfg.Configuration: hibernate.connection.driver_class=org.gjt.mm.mysql.Driver
2006-09-09 17:21:52,015 DEBUG [Thread-1] org.hibernate.cfg.Configuration: hibernate.connection.password=schule11
2006-09-09 17:21:52,015 DEBUG [Thread-1] org.hibernate.cfg.Configuration: hibernate.connection.url=jdbc:mysql://127.0.0.1:3306/universe
2006-09-09 17:21:52,015 DEBUG [Thread-1] org.hibernate.cfg.Configuration: hibernate.connection.username=HibernateWeb
2006-09-09 17:21:52,015 DEBUG [Thread-1] org.hibernate.cfg.Configuration: hibernate.dialect=org.hibernate.dialect.MySQLDialect
2006-09-09 17:21:52,015 DEBUG [Thread-1] org.hibernate.cfg.Configuration: null<-org.dom4j.tree.DefaultAttribute@e45b5e [Attribute: name resource value "Abc.hbm.xml"]
2006-09-09 17:21:52,015 INFO [Thread-1] org.hibernate.cfg.Configuration: Reading mappings from resource: Abc.hbm.xml
2006-09-09 17:21:52,015 DEBUG [Thread-1] org.hibernate.util.DTDEntityResolver: trying to resolve system-id [http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd]
2006-09-09 17:21:52,015 DEBUG [Thread-1] org.hibernate.util.DTDEntityResolver: recognized hibernate namespace; attempting to resolve on classpath under org/hibernate/
2006-09-09 17:21:52,015 DEBUG [Thread-1] org.hibernate.util.DTDEntityResolver: located [http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd] in classpath
2006-09-09 17:21:52,078 INFO [Thread-1] org.hibernate.cfg.Mappings: duplicate import: beans.Abc->beans.Abc
2006-09-09 17:21:52,078 INFO [Thread-1] org.hibernate.cfg.Mappings: duplicate import: beans.Abc->Abc
2006-09-09 17:21:52,078 INFO [Thread-1] org.hibernate.cfg.HbmBinder: Mapping class: beans.Abc -> abc
2006-09-09 17:21:52,078 DEBUG [Thread-1] org.hibernate.cfg.HbmBinder: Mapped property: id -> Id
2006-09-09 17:21:52,078 DEBUG [Thread-1] org.hibernate.cfg.HbmBinder: Mapped property: a -> a
2006-09-09 17:21:52,093 FATAL [Thread-1] root: Hibernate konnte nicht gestartet werden
org.hibernate.InvalidMappingException: Could not parse mapping document from resource Abc.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:523)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1511)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1479)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1458)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1432)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1352)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1338)
at listener.WebsiteListener.erstelleHibernate(WebsiteListener.java:228)
at listener.WebsiteListener.contextInitialized(WebsiteListener.java:73)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3729)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4187)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:759)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:739)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:524)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:904)
at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:867)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:474)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1122)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:310)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1021)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:718)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1013)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:442)
at org.apache.catalina.core.StandardService.start(StandardService.java:450)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:709)
at org.apache.catalina.startup.Catalina.start(Catalina.java:551)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:294)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:432)
Caused by: org.hibernate.DuplicateMappingException: Duplicate class/entity mapping beans.Abc
at org.hibernate.cfg.Mappings.addClass(Mappings.java:118)
at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:145)
at org.hibernate.cfg.Configuration.add(Configuration.java:424)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:465)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:520)
... 32 more
[...]
MySQL:
database: universe
table: abc
-> Id integer not null primary key auto_increment
-> a integer not null
Warum bekomme ich diese Fehlermeldung? Ich muss zugeben, dass ich gerade erst mit Hibernate befasse und somit noch keine Erfahrung damit besitzte. Doch ich komme hier einfach nicht weiter :-( Kann mir hier jemand helfen?
MfG,
Thomas