Hi folks,
I started learning about hibernate annotation and set up a projekt, but I cannot get the code to work. It still asks for thehbm.xml files, and I couldn't find out why...
here is the Exception I get:
Exception in thread "main" java.lang.ExceptionInInitializerError
at de.community.persistence.PersistenceUtil.<clinit>(PersistenceUtil.java:42)
at de.community.test.MainClass.main(MainClass.java:19)
Caused by: org.hibernate.MappingNotFoundException: resource: de/community/dao/valueobject/Web.hbm.xml not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:533)
at org.hibernate.cfg.Configuration.addClass(Configuration.java:586)
at de.community.persistence.PersistenceUtil.<clinit>(PersistenceUtil.java:37)
... 1 more
The written class PersistenceUtil looks like this:
Code:
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
public class PersistenceUtil {
public static final ThreadLocal<Session> _session = new ThreadLocal<Session>();
public static final ThreadLocal<Transaction> _trx = new ThreadLocal<Transaction>();
// static block, only run once when the class is initialized the first time
private static SessionFactory _sessionFactory;
private static AnnotationConfiguration _annotationConfiguration;
static {
try {
_annotationConfiguration = new AnnotationConfiguration();
_annotationConfiguration.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
_annotationConfiguration.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
_annotationConfiguration.setProperty("hibernate.connection.url", "jdbc:mysql://localhost/mydatabase?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=UTF-8");
_annotationConfiguration.setProperty("hibernate.connection.username", "myUser");
_annotationConfiguration.setProperty("hibernate.connection.password", "myPassword");
_annotationConfiguration.setProperty("hibernate.hbm2ddl.auto", "create-drop");
_annotationConfiguration.addClass(de.community.dao.valueobject.Web.class);
_sessionFactory = _annotationConfiguration.buildSessionFactory();
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
// Alternativ kann hier auch JNDI genutzt werden
return _sessionFactory;
}
public static void shutdown() {
// Caches und Verbindungspools schliessen
if (getSessionFactory() != null) {
getSessionFactory().close();
} else {
_logger.error("Cannot close Session Factory since it is null");
}
}
public static Session getSession() throws HibernateException {
Session s = _session.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = _sessionFactory.openSession();
_session.set(s);
getTransaction();
}
return s;
}
}
The added properties can be deleted since they are also set in the persistence.xml file, which I will post, too.
My main-class doesn't do more than creating an instance of the PersistenceUtil.
The Object Web, that should be mapped to the table, looks like this:
Code:
import javax.persistence.*;
import java.io.Serializable;
@Entity
@Table(name = "WEB")
public class Web implements Serializable{
@Id @GeneratedValue
@Column(name = "web_id")
private Long WebId;
@Column(name = "name")
private String Name;
@Column(name = "note")
private String Note;
public Long getWebId() {
return WebId;
}
public void setWebId(Long WebId) {
this.WebId = WebId;
}
public String getName() {
return Name;
}
public void setName(String Name) {
this.Name = Name;
}
public String getNote() {
return Note;
}
public void setNote(String Note) {
this.Note = Note;
}
}
The persistence.xml lies in the META-INF directory of my application. The same data is loaded through the properties in the PersitenceUtil class, I used the properties to find out if there is something wrong with the data in the xml-file, but without success:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="CORE" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>de.community.dao.valueobject.Web</class>
<properties>
<property name="hibernate.connection.username" value="myUser"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.password" value="myPassword"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost/mydatabase?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
</properties>
</persistence-unit>
</persistence>
Last but not least, the hibernate.cfg.xml, lying in the META-INF directory, too (Do I really need this file? its pretty empty anyway):
Code:
<!DOCTYPE hibernate-configuration SYSTEM
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
</session-factory>
</hibernate-configuration>
But even if I fill the hibernate.cfg.xml file with the connection data, I still get the same exception. The connection does not seem to be the problem anyway, for some reason it wants the hbm.xml file and isn't satified with the annotation I provide within the Web.class. So I guess I'm missing something. Can anyone tell me what it might be?
Thanks in advance,
macmo