-->
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.  [ 6 posts ] 
Author Message
 Post subject: Getting started with Hibernate Tools and Eclipse
PostPosted: Tue May 02, 2006 4:33 am 
Newbie

Joined: Fri Apr 28, 2006 5:01 am
Posts: 4
Location: Karlstad, Sweden
Hello

This is my first post here, and also my first attempt at using Hibernate (although I am familiar with Eclipse in other contexts). If I make any stupid mistakes, please tell me so I can avoid them in the future.

I am trying to get a minimal Hibernate project working, so I can build from there. I have created a HBM.XML file and then I want to generate both a Java POJO and database tables from that file.

The Java project was created with separate source and output directories, something I learned should be done after browsing other topics on this forum.

My project file structure looks like this (the bin directory, although it exists, is hidden by Eclipse):

src
| se.fyrplus.UserDatabase
| | UserDatabase.java (contains main method, no code yet)
| | User.hbm.xml
JRE System Library
postgresql.jar
hibernate.cfg.xml

The hibernate.cfg.xml was generated with the New Wizard (plus a manually added mapping tag), and looks like this:

<?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.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://fyrpc200/jenryEdu</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<mapping resource="se/fyrplus/UserDatabase/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>

Finally, the User.hbm.xml mapping file looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="se.fyrplus.UserDatabase">
<class name="User" table="USERS">
<id name="id" column="USER_ID">
<generator class="native"></generator>
</id>
<property name="username" type="string" column="USER_NAME"></property>
</class>
</hibernate-mapping>

It seems to me that this should be enough to be able to run code generation and create a User.java file. However, when I try to do so, I get the following exception (in the details field in the appearing error dialog):

Could not read mappings from resource: se/fyrplus/UserDatabase/User.hbm.xml
org.hibernate.MappingException: class se.fyrplus.UserDatabase.User not found while looking for property: id
[...]
java.lang.ClassNotFoundException: se.fyrplus.UserDatabase.User
[...]

This seems strange to me, as the code generation I am trying to run is intended to generate the User class. More precisely, it is set up to generate domain Java code and output the results to the src directory.

In my Hibernate Console configuration, I have not added the mapping file (since it is present in the cfg.xml), and both the src and bin directories are in the classpath.

Can anyone please help me by pointing out what I am missing here?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 02, 2006 8:24 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
you need to specify the type for the id.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: Thank you
PostPosted: Tue May 02, 2006 10:04 am 
Newbie

Joined: Fri Apr 28, 2006 5:01 am
Posts: 4
Location: Karlstad, Sweden
Thanks, that helped. Now I can generate both POJO code and HTML Schema documentation successfully.

I fail to connect to the database at the moment, but that is probably due to my configuration (connection pool etc.) and/or the database server (not managed by me, I am unsure if the database exists yet, and so on). I will beat my head against those walls for a while and see if I can solve them. If I fail, I will be back here.

EDIT: My database connection problems depended on server configuration, but it now resolved and I am up and running.


Last edited by Jens_Rydholm on Wed May 03, 2006 10:05 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue May 02, 2006 11:16 am 
Newbie

Joined: Fri Apr 28, 2006 5:01 am
Posts: 4
Location: Karlstad, Sweden
Re-reading the online Hibernate documentation here, I see (in section 1.2.2) that the example does not have a type attribute for the ID.

<hibernate-mapping>
<class name="events.Event" table="EVENTS">
<id name="id" column="EVENT_ID">
<generator class="native"/>
</id>
<property name="date" type="timestamp" column="EVENT_DATE"/>
<property name="title"/>
</class>
</hibernate-mapping>

Am I misunderstanding something, or is the documentation actually incorrect?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 02, 2006 11:26 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
the type is of course not requried when you already have the classes (as you have in that introduction guide), but it is *required* when you want to generate code...how else should we know the type ? :)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 02, 2006 11:42 am 
Newbie

Joined: Fri Apr 28, 2006 5:01 am
Posts: 4
Location: Karlstad, Sweden
Ah, I assumed that the example used a default type for ID or something like that.

Thanks again.


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