-->
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: super interface as member of subinterface impl mapping
PostPosted: Fri Sep 30, 2005 7:39 pm 
Newbie

Joined: Fri Sep 30, 2005 6:57 pm
Posts: 5
[b]Hibernate version:[/b] 3.0.5

Hello Hibernate Experts!

I have the following heirarchy (adapted from AppFuse) and have been struggling with the mapping file(s). My goal is a MinimalUser-centric system with decorators (E.G. Model is a decorator), as well as "aspects" (E.G. PictureSet), and I don't want repeated data in the DB (I have soutions that violate these preferences, but I am looking for something better). Any advice is appreciated.

[code]public interface MinimalUser extends BaseInterface {

public Long getId();
public void setId(Long id);
public String getUsername();
public void setUsername(String username);
public String getPassword();
public void setPassword(String password);
public Integer getVersion();
public void setVersion(Integer version);
// more (irrelevant) getter/setters
}

public interface DerivedUser extends MinimalUser {
// marker for derived users - like serializable
}

public interface PictureSet {

public Long getId();
public void setId(Long id);
public Set getPictures();
public void setPictures(Set pictures);
}

public interface PictureSetDerivedUser extends DerivedUser, PictureSet { }

public interface Model extends DerivedUser, PictureSet { }

public class MinimalUserImpl implements MinimalUser {
// Implemented as expected from MinimalUser
}

public abstract class AbstractDerivedUser implements DerivedUser {

protected Long id;
protected Integer version;
protected MinimalUser minimalUser;
protected final Log log = LogFactory.getLog(getClass());

// getters and setters dispatch to minimalUser
// except for id and version
}

public abstract class AbstractPictureSetDerivedUser extends AbstractDerivedUser implements PictureSetDerivedUser {

protected Set pictures;

public Set getPictures() {
return pictures;
}

public void setPictures(Set pictures) {
this.pictures = pictures;
}
}

public class ModelImpl extends AbstractPictureSetDerivedUser implements Model { }
[/code]

My current mapping file is:

[code]<?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 default-cascade="all,delete-orphan"
>
<class
name="com.bayareamodels.model.MinimalUser" abstract="true"
>

<id
name="id"
column="id"
type="java.lang.Long"
unsaved-value="null"
>
<generator class="native"/>
</id>

<discriminator column="subclass"/>

<version
name="version"
column="version"
type="java.lang.Integer"
/>

<property
name="username"
type="java.lang.String"
update="true"
insert="true"
column="username"
length="20"
not-null="true"
unique="true"
/>

<property
name="password"
type="java.lang.String"
update="true"
insert="true"
column="password"
length="20"
not-null="true"
/>

<property
name="firstName"
type="java.lang.String"
update="true"
insert="true"
column="firstName"
length="20"
not-null="true"
/>

<property
name="lastName"
type="java.lang.String"
update="true"
insert="true"
column="lastName"
length="20"
not-null="true"
/>

<property
name="email"
type="java.lang.String"
update="true"
insert="true"
column="email"
length="20"
not-null="true"
unique="true"
/>

<property
name="passwordHint"
type="java.lang.String"
update="true"
insert="true"
column="passwordHint"
length="50"
not-null="false"
/>

<set
name="roles"
lazy="false"
cascade="none"
sort="unsorted"
>

<key
column="userId"
>
</key>

<many-to-many
class="com.bayareamodels.model.Role"
column="roleName"
outer-join="auto"
/>

</set>

<property
name="enabled"
type="java.lang.Boolean"
update="true"
insert="true"
column="enabled"
/>

<subclass name="com.bayareamodels.model.impl.AbstractDerivedUser" abstract="true">
<join table="AbstractDerivedUser">
<key column="id"/>
<many-to-one name="minimalUser" column="decoratedMinimalUser" class="com.bayareamodels.model.MinimalUser" not-null="true"/>
</join>

<subclass name="com.bayareamodels.model.impl.AbstractPictureSetDerivedUser" abstract="true">
<join table="AbstractPictureSetDerivedUser">
<key column="id"/>

<set
name="pictures"
lazy="false"
cascade="none"
sort="unsorted"
>

<key
column="userId"
>
</key>

<many-to-many
column="pictureId"
class="com.bayareamodels.model.Picture"
/>
</set>

</join>

<subclass name="com.bayareamodels.model.impl.ModelImpl">
<join table="ModelImpl">
<key column="id"/>
</join>
</subclass>

</subclass>
</subclass>

<subclass name="com.bayareamodels.model.impl.MinimalUserImpl">
</subclass>

</class>

</hibernate-mapping>[/code]


However, the picture set nested in the AbstractPictureSetDerivedUser subclass join doesn't follow the dtd.

Thanks,
Chris[code][/code]


Top
 Profile  
 
 Post subject: updated mapping
PostPosted: Fri Sep 30, 2005 8:44 pm 
Newbie

Joined: Fri Sep 30, 2005 6:57 pm
Posts: 5
I think this is a better/workable mapping - I'd still like suggestions if you've got them.

<?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 default-cascade="all,delete-orphan"
>
<class
name="com.bayareamodels.model.MinimalUser" abstract="true"
>

<id
name="id"
column="id"
type="java.lang.Long"
unsaved-value="null"
>
<generator class="native"/>
</id>

<discriminator column="subclass"/>

<version
name="version"
column="version"
type="java.lang.Integer"
/>

<property
name="username"
type="java.lang.String"
update="true"
insert="true"
column="username"
length="20"
not-null="true"
unique="true"
/>

<property
name="password"
type="java.lang.String"
update="true"
insert="true"
column="password"
length="20"
not-null="true"
/>

<property
name="firstName"
type="java.lang.String"
update="true"
insert="true"
column="firstName"
length="20"
not-null="true"
/>

<property
name="lastName"
type="java.lang.String"
update="true"
insert="true"
column="lastName"
length="20"
not-null="true"
/>

<property
name="email"
type="java.lang.String"
update="true"
insert="true"
column="email"
length="20"
not-null="true"
unique="true"
/>

<property
name="passwordHint"
type="java.lang.String"
update="true"
insert="true"
column="passwordHint"
length="50"
not-null="false"
/>

<set
name="roles"
lazy="false"
cascade="none"
sort="unsorted"
>

<key
column="userId"
>
</key>

<many-to-many
class="com.bayareamodels.model.Role"
column="roleName"
outer-join="auto"
/>

</set>

<property
name="enabled"
type="java.lang.Boolean"
update="true"
insert="true"
column="enabled"
/>

<joined-subclass name="com.bayareamodels.model.impl.MinimalUserImpl">
<key column="id"/>
</joined-subclass>

<joined-subclass name="com.bayareamodels.model.impl.AbstractDerivedUser" table="AbstractDerivedUser">
<key column="id"/>
<many-to-one name="minimalUser" column="decoratedMinimalUser" class="com.bayareamodels.model.MinimalUser" not-null="true"/>

<joined-subclass name="com.bayareamodels.model.impl.AbstractPictureSetDerivedUser" table="AbstractPictureSetDerivedUser">
<key column="id"/>

<set
name="pictures"
lazy="false"
cascade="none"
sort="unsorted"
>

<key
column="userId"
>
</key>

<many-to-many
column="pictureId"
class="com.bayareamodels.model.Picture"
/>
</set>

<joined-subclass name="com.bayareamodels.model.impl.ModelImpl">
<key column="id"/>
</joined-subclass>

</joined-subclass>
</joined-subclass>

</class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject: My Solution
PostPosted: Mon Oct 03, 2005 10:37 pm 
Newbie

Joined: Fri Sep 30, 2005 6:57 pm
Posts: 5
For those of you keeping track ...

The problem I eventually faced was "querying by interface". This is not particularly supported by either Hibernate (nor JDO implementations I tried: jpox and xcalia), though it may be supported in the future. I could have used the "any" Hibernate element, but that was just too much work, so I'm going with Hibernate and I'm ditching the decorator pattern.


Because I spent so much time on this, I'd like others to benefit from it. So to summarize:

Neither Hibernate (3.0.5), nor JDO provide "easy" solutions to interface queries.

Some keywords for search engines:
interface inheritance, multiple inheritance, query by interface, interface queries, interface query.

Hope this helps someone.

Chris


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 04, 2005 2:12 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Querying an interface in Hibernate is totally trivial. You apparently didn't even *try* it.


The following example is straight from the documentation:

Code:
from java.io.Serializable


Top
 Profile  
 
 Post subject: RE: triviality
PostPosted: Tue Oct 04, 2005 6:21 pm 
Newbie

Joined: Fri Sep 30, 2005 6:57 pm
Posts: 5
Gavin,

Thank you for taking the time to respond, as well as for your outstanding contributions to this great project. However, there was not enough information in your response for me to find the example you are referring to.

And while I don’t want to come off as simple or argumentative, I have found Hibernate wanting in a few areas. My wish list:
-Constructor based initialization during DB retrieval
-XML schema that mirrors Java inheritance (the JDO schema does this well, Hibernate is limited to class and subclass)
-Better support for native ids ;-)
--http://raibledesigns.com/wiki/Wiki.jsp?page=AppFuseOnDB2#ref-AppFuseOnDB2-5
--http://forum.hibernate.org/viewtopic.php?p=2264498#2264498

Having complained, I want to reiterate my praise for your work and for Hibernate. I am comparing persistence technologies and so far Hibernate is my preference.

As far as my design, which I have now scrapped, I still have not found a “non-trivial” (for me) persistence solution for multiple interface inheritance especially with the decorator design pattern. I am able to get single interface inheritance persistence and queries to work (I failed to mention that previously). If you have a solution to this dilemma I would like to be enlightened. Further, I understand that this is an open-source project and that I could implement it myself. But, I’m not even willing to implement an “any”, so that is very unlikely.

Best wishes, and continued gratitude,
Chris


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.