-->
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.  [ 3 posts ] 
Author Message
 Post subject: Feedback sought on example to be given to students
PostPosted: Mon Oct 04, 2004 11:17 am 
Newbie

Joined: Mon Oct 04, 2004 11:10 am
Posts: 9
Location: Toronto
I'm putting together an example of non-trivial Hibernate use to show students at the University of Toronto. What I'm trying to model is a whole/part hierarchy with three classes:

* UserEntity is an abstract base class;

* User (derived from UserEntity) represents an actual user; and

* UserGroup (also derived from UserEntity) represents a group of users and other user groups (i.e. groups can contain groups, though cycles are not allowed).

My first cut at a mapping file is shown below; I use hbm2java to generate classes from this file. I'd be grateful for any comments anyone has on how to make this more Hibernate-ish...

Thanks,
Greg

Hibernate version:

2.1

Mapping documents:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

<class name="ca.hippo.model.UserEntity" table="USER_ENTITIES" discriminator-value="UE">

<meta attribute="class-description">Whole/part implementation of users and groups.</meta>

<meta attribute="implement-equals">true</meta>

<id name="id" type="long" column="UNIQUE_ID">
<meta attribute="field-description">Unique ID for internal database use.</meta>
<meta attribute="use-in-equals">true</meta>
<meta attribute="use-in-tostring">true</meta>
<generator class="native"/>
</id>

<discriminator column="USER_ENTITIES_TYPE" type="string"/>

<property name="uid" type="string" not-null="true">
<meta attribute="field-description">Unique user-visible name.</meta>
<meta attribute="use-in-tostring">true</meta>
</property>

<property name="fullName" type="string" not-null="true">
<meta attribute="field-description">User's actual name.</meta>
<meta attribute="use-in-tostring">true</meta>
</property>

<subclass name="ca.hippo.model.User" discriminator-value="U">
<property name="email" type="string">
<meta attribute="field-description">User's preferred email address.</meta>
</property>
</subclass>

<subclass name="ca.hippo.model.UserGroup" discriminator-value="UG">
<set name="members" table="USER_GROUP_MEMBERS" lazy="true" cascade="save-update">
<key column="UNIQUE_ID"/>
<many-to-many class="ca.hippo.model.UserEntities" column="UNIQUE_ID"/>
</set>
</subclass>

</class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 04, 2004 3:14 pm 
Beginner
Beginner

Joined: Wed Nov 19, 2003 6:46 pm
Posts: 41
Location: Auckland, New Zealand
Just a few comments...

From a modelling perspective, I'm not sure that having an abstract base class for User and UserGroup would be something that I'd do. What do they share in common apart from a name? If you want to show how to implement inheritance in Hibernate, surely you can come up with something better. How about something that uses polymorphism?

Secondly, shouldn't your id field be of type Long - follow Hibernate's recommendation of using a nullable type for the ID.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 04, 2004 7:55 pm 
Beginner
Beginner

Joined: Fri Mar 12, 2004 8:40 pm
Posts: 30
Location: SF Bay Area
I haven't figured out how to get
net.sf.hibernate.tool.hbm2java.CodeGenerator
to create an Interface or an Abstract class.
But this might help ...

Write your own UserEntity abstract class.

Then use the CodeGenerator to create both UserEntity using
a meta like this one :

<meta inherit="true" attribute="extends">User</meta>

and UserGroup as

<meta inherit="true" attribute="extends">User</meta>


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