-->
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: Mapping a composite pattern (without a parent)
PostPosted: Wed Oct 15, 2008 5:00 am 
Newbie

Joined: Tue Jan 06, 2004 5:52 am
Posts: 17
Location: Belgium
Hi,

I am trying to map a composite pattern based hierarchy. I have the following classes:

* Interface 'Filter'
* Class 'CombinedFilter' containing a List<Filter>
* Class 'AbstractSingleFilter'
* Some implementation classes that subclass AbstractSingleFilter

I am using Hibernate Annotations 3.4.0. I already found out that you cannot use annotations and interfaces, so I created a hbm.xml file for my interface
Code:
<hibernate-mapping>
    <class name="com.mycomp.domain.filter.Filter">
        <id name="id"/>
    </class>
</hibernate-mapping>


The combined filter class is mapped like this:
Code:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class CombinedFilter implements Filter
{

   @Id
   @Column(name = "Id")
   private Long m_id;

   @Column(name = "ChildFilters")
   private List<Filter> m_filters;

   @Column(name = "CombinationType")
   private CombinationType m_combinationType;

   @Column(name = "Name")
   private String m_name;


This the AbstractSingleFilter:
Code:
@MappedSuperclass
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class AbstractSingleFilter implements Filter
{

   @Id
   @Column(name = "Id")
   private Long m_id;

   @Column(name = "Name")
   private String m_name;


And this is a concrete implementation:
Code:
@Entity
@PrimaryKeyJoinColumn(name="AbstractSingleFilterId")
public class ZoneIdFilter extends AbstractSingleFilter
{
   @Column(name = "ZoneId")
   private int m_zoneId;



What do I need to annotate the m_filters variable in CombinedFilter with to make this work?

I found this example but it has a slightly different hierarchy. It does not use an interface and it has a link back to the parent, which I don't have here.

regards,

Wim


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 15, 2008 5:23 am 
Newbie

Joined: Tue Jan 06, 2004 5:52 am
Posts: 17
Location: Belgium
I found out that if I use this:

Code:
   @Column(name = "ChildFilters")
   @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
   @JoinColumn(name = "ParentFilterId")
   private List<Filter> m_filters;


Then I get 4 tables generated and the CombinedFilter and Filter tables are correctly filled up. However, the tables with the concrete types are still empty. I am using session.saveOrUpdate() with a CombinedFilter object containing 2 AbstractSingleFilter subclasses.

Any idea why the cascading does not work?

regards,

Wim
Update:
I switched to a join table (As recommended in the documentation: "A unidirectional one to many with join table is much preferred")

But this makes no difference. This is the sql that is generated:

Code:
Hibernate: select combinedfi_.Id, combinedfi_.CombinationType as Combinat2_3_, combinedfi_.Name as Name3_ from CombinedFilter combinedfi_ where combinedfi_.Id=?
Hibernate: select filter_.id from Filter filter_ where filter_.id=?
Hibernate: select filter_.id from Filter filter_ where filter_.id=?
Hibernate: insert into CombinedFilter (CombinationType, Name, Id) values (?, ?, ?)
Hibernate: insert into Filter (id) values (?)
Hibernate: insert into Filter (id) values (?)
Hibernate: insert into ChildFilters (ParentFilterId, ChildFilterId) values (?, ?)
Hibernate: insert into ChildFilters (ParentFilterId, ChildFilterId) values (?, ?)


What hibernate should do is see that the child filters are of a certain subclass and update those tables also, and not only insert records in the Filter table.

regards,

Wim


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.