-->
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.  [ 5 posts ] 
Author Message
 Post subject: Problem with hibernate mapping
PostPosted: Mon Apr 07, 2008 9:08 am 
Newbie

Joined: Fri Apr 04, 2008 3:13 pm
Posts: 2
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.2.0 GA

Greetings,

I am fairly new to hibernate and I am trying to make a very small simple project to work. The project is the booking application from Seam and I am using SQL Server 2005 Express as the DB server. The project works, but my problem comes from generating the Hibernate mapping files. In Eclipse(I am using version 3.3 ie Europa), when you switch to the hibernate console perspective, I am creating a reveng.xml to generate the mapping. Here is what it looks like:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" >

<hibernate-reverse-engineering>
  <table-filter match-catalog="booking" match-schema="dbo" match-name="Booking"/>
  <table-filter match-catalog="booking" match-schema="dbo" match-name="Customer"/>
  <table-filter match-catalog="booking" match-schema="dbo" match-name="Hotel"/>
</hibernate-reverse-engineering>


I am able to do the process of generating the mapping files. However, still in the Hibernate perspective, I am not able to open the SessionFactory option. I have selected the "jdk 1.5 + Annotation" in the hibernate config setup in Eclipse. I am getting the following error:

Quote:
org.hibernate.MappingException: entity class not found: Customer
at org.hibernate.mapping.PersistentClass.getMappedClass(PersistentClass.java:99)
at org.hibernate.validator.event.ValidateEventListener.initialize(ValidateEventListener.java:90)
at org.hibernate.event.EventListeners.initializeListeners(EventListeners.java:356)
at org.hibernate.cfg.Configuration.getInitializedEventListeners(Configuration.java:1304)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1294)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:915)
at org.hibernate.console.ConsoleConfiguration$3.execute(ConsoleConfiguration.java:357)
at org.hibernate.console.execution.DefaultExecutionContext.execute(DefaultExecutionContext.java:65)
at org.hibernate.console.ConsoleConfiguration.execute(ConsoleConfiguration.java:88)
at org.hibernate.console.ConsoleConfiguration.buildSessionFactory(ConsoleConfiguration.java:352)
at org.hibernate.eclipse.console.workbench.LazySessionFactoryAdapter.getChildren(LazySessionFactoryAdapter.java:41)
at org.hibernate.eclipse.console.workbench.BasicWorkbenchAdapter.getChildren(BasicWorkbenchAdapter.java:97)
at org.hibernate.eclipse.console.workbench.BasicWorkbenchAdapter.fetchDeferredChildren(BasicWorkbenchAdapter.java:103)
at org.eclipse.ui.progress.DeferredTreeContentManager$1.run(DeferredTreeContentManager.java:196)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)
Caused by: java.lang.ClassNotFoundException: Customer
at java.lang.ClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.eclipse.osgi.framework.internal.core.BundleLoader.findClassInternal(BundleLoader.java:429)
at org.eclipse.osgi.framework.internal.core.BundleLoader.findClass(BundleLoader.java:369)
at org.eclipse.osgi.framework.internal.core.BundleLoader.findClass(BundleLoader.java:357)
at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:83)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:100)
at org.hibernate.mapping.PersistentClass.getMappedClass(PersistentClass.java:96)
... 14 more



Here is a little info about my setup:

Eclipse Europa
JDK 1.5
Hibernate 3.2.0 GA
Seam Framework 2.0.1 GA

This is my 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.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.cache.use_second_level_cache">false</property>
        <property name="hibernate.connection.autocommit">true</property>
        <property name="hibernate.connection.datasource">java:/DefaultDS</property>
        <property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
        <property name="hibernate.connection.password"><my password></property>
        <property name="hibernate.connection.provider_class">org.hibernate.connection.DriverManagerConnectionProvider</property>
        <property name="hibernate.connection.release_mode">auto</property>
        <property name="hibernate.connection.url">jdbc:jtds:sqlserver://127.0.0.1:1433/booking</property>
        <property name="hibernate.connection.username"><my username></property>
        <property name="hibernate.datasource"></property>
        <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
        <property name="hibernate.ejb.discard_pc_on_close">false</property>
        <property name="hibernate.query.jpaql_strict_compliance">true</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.transaction.factory_class">org.hibernate.ejb.transaction.JoinableCMTTransactionFactory</property>
        <property name="hibernate.transaction.flush_before_completion">false</property>
        <property name="hibernate.use_identifier_rollback">false</property>
        <mapping resource="Customer.hbm.xml" />
        <mapping resource="Hotel.hbm.xml" />
        <mapping resource="Booking.hbm.xml" />
    </session-factory>
</hibernate-configuration>


Here is the Customer.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">
<!-- Generated 4-Apr-2008 4:26:25 PM by Hibernate Tools 3.2.0.CR1 -->
<hibernate-mapping>
    <class name="Customer" table="Customer" schema="dbo" catalog="booking">
        <id name="username" type="string">
            <column name="username" length="15" />
            <generator class="assigned" />
        </id>
        <property name="name" type="string">
            <column name="name" length="100" not-null="true" />
        </property>
        <property name="password" type="string">
            <column name="password" length="15" not-null="true" />
        </property>
        <set name="bookings" inverse="true">
            <key>
                <column name="user_username" length="15" not-null="true" />
            </key>
            <one-to-many class="Booking" />
        </set>
    </class>
</hibernate-mapping>


I am not sure where my problem is, so if anyone can help me on this, I would greatly appreciate it. If you need any other info, please let me know. I thank you all in advance for looking at my problem.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 07, 2008 9:52 am 
Newbie

Joined: Mon Oct 01, 2007 3:39 am
Posts: 11
Try to move the hbm.xml files to the root directory of the BIN directory.
It means that for some reasons Hibernate cannot find the mapping files.

I had this problems many times in Eclipse. Sometimes hibernate is able to find the files wherever they are, sometimes not.

In the persistence.xml file under src/META_INF you should have this:

<persistence 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/ ... ce_1_0.xsd"
version="1.0">

<persistence-unit name="SyncroHL7">
<properties>
<!-- Scan for annotated classes and Hibernate mapping XML files -->
<property name="hibernate.archive.autodetection" value="class, hbm"/>


But it seems that this "autodetection" sometimes does not work.
I don't know exactly why this happen.

Greetings,
Fabio


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 07, 2008 1:04 pm 
Newbie

Joined: Fri Apr 04, 2008 3:13 pm
Posts: 2
Hi dapile Fabio,

thank you for answering promptly. I tried what you said but it did not work. Should only the hbm.xml files be moved in the BIN files or I can move everything in there(ie POJOs and DAOs files)? I also added the property in the persistence file. It seems I was missing that line. Again, if you can help me on this on anybody can, would greatly appreciate it.


Top
 Profile  
 
 Post subject: Help
PostPosted: Fri Aug 01, 2008 8:46 am 
Newbie

Joined: Fri Aug 01, 2008 8:11 am
Posts: 4
Hi,
I am also new to Hibernate and want to work this new small application what you have created so that I can also get the overview of hibernate. Could you plese zip it and mail me @vikashkumar.agrawal@hcl.in.

Many thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 01, 2008 9:41 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Here you go! Probably one of the smallest Hibernate applicaitons you'll ever see!

What you need to do is slowly learn the basics of Hibernate. Check out my signature links on how to get started with Hibernate. Start slowly, tackle one topic at a time, and you'll be an expert in no time!

http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=01howtogetstartedwithhibernate


Code:
package com.examscam.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

@Entity
public class User {
  private Long id;
  private String password;

  @Id
  @GeneratedValue
  public Long getId() {return id;}
  public void setId(Long id) {this.id = id;}
  public String getPassword() {return password;}
  public void setPassword(String password) {
    this.password = password;
  }
  public static void main(String args[]){
    AnnotationConfiguration config =
                     new AnnotationConfiguration();
    config.addAnnotatedClass(User.class);
    config.configure();
    // new SchemaExport(config).create(true, true);
    SessionFactory factory =
                  config.buildSessionFactory();
    Session session = factory.getCurrentSession();
    session.beginTransaction();
    System.out.println("creating user");
   
    User u = new User();
    u.setPassword("abc123");
    session.saveOrUpdate(u);
    System.out.println("user saved");
    session.getTransaction().commit();
    System.out.println("transaction successful!!!"); 
  }
}

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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