-->
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 
Newbie

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:

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;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");
      _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;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"/>
    </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


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

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

Code:
    ${file.reference.ejb3-persistence.jar}:\
    ${file.reference.hibernate-commons-annotations.jar}:\
    ${file.reference.hibernate-annotations.jar}:\
    ${file.reference.hibernate3.jar}:\
    ${file.reference.ant-launcher-1.6.5.jar}:\
    ${file.reference.ant-swing-1.6.5.jar}:\
    ${file.reference.antlr-2.7.6.jar}:\
    ${file.reference.asm-attrs.jar}:\
    ${file.reference.asm.jar}:\
    ${file.reference.c3p0-0.9.1.jar}:\
    ${file.reference.cglib-2.1.3.jar}:\
    ${file.reference.commons-logging-1.0.4.jar}:\
    ${file.reference.concurrent-1.3.2.jar}:\
    ${file.reference.dom4j-1.6.1.jar}:\
    ${file.reference.ehcache-1.2.3.jar}:\
    ${file.reference.javassist.jar}:\
    ${file.reference.log4j-1.2.11.jar}:\
    ${file.reference.commons-collections-3.2.jar}:\
    ${file.reference.commons-configuration-1.5.jar}:\
    ${file.reference.jboss-archive-browsing.jar}:\
    ${file.reference.commons-lang-2.3.jar}:\
    ${file.reference.mysql-connector-java-5.1.5-bin.jar}:\
    ${file.reference.hibernate-entitymanager.jar}:\
    ${file.reference.hibernate-search.jar}:\
    ${file.reference.lucene-core-2.2.0.jar}:\
    ${file.reference.j2ee.jar}


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

regards,
macmo


Top
 Profile  
 
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.