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.  [ 2 posts ] 
Author Message
 Post subject: Cannot get Hibernate Annotation to work....
PostPosted: Tue Jan 15, 2008 1:15 pm 

Joined: Tue Jan 15, 2008 12:44 pm
Posts: 4
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:


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;amp;useUnicode=true&amp;amp;characterEncoding=UTF-8");
      _annotationConfiguration.setProperty("hibernate.connection.username", "myUser");
      _annotationConfiguration.setProperty("hibernate.connection.password", "myPassword");
      _annotationConfiguration.setProperty("hibernate.hbm2ddl.auto", "create-drop");

      _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) {
    } 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();
    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:
import javax.persistence.*;
import java.io.Serializable;

@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:

<?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">
      <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;amp;useUnicode=true&amp;amp;characterEncoding=UTF-8"/>
      <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
      <property name="hibernate.hbm2ddl.auto" value="create-drop"/>

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):

<!DOCTYPE hibernate-configuration SYSTEM


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,

 Post subject: Got it running....
PostPosted: Tue Jan 15, 2008 4:28 pm 

Joined: Tue Jan 15, 2008 12:44 pm
Posts: 4
Using the following libraries in NetBeans 6 and the property-coding style:


Don't know why it doesn't work with the xml - config files, but I might still find out.


Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.