-->
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: Help with mapping
PostPosted: Thu Dec 11, 2008 5:21 pm 
Newbie

Joined: Fri May 13, 2005 2:02 pm
Posts: 8
Hibernate version: 2

How do I map this situation with annotations or xml for that matter.

I have two pretty different tables, a and b. Each table has a collection of table c.

I'm confused as to how to do the mapping in table c back to tables a and b.

I had each a and b mapped in c with a ManyToOne, but it wasn't working correctly. It would populate the ref column back to a/b. parent_id.

My thought now is to inherit from c to two new classes mapped to the same table.

Thank you,

Marty


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 12, 2008 4:17 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Can you post the table definitions and your current mapping ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 12, 2008 11:05 am 
Newbie

Joined: Fri May 13, 2005 2:02 pm
Posts: 8
I believe below outlines what I'm trying to do basically. A party will only every be mapped to either an allocation or to a rptside. Never both.

I left out fields/properties which weren't relevant.

Thank you for the help!

table Allocation (
id number,
party_id number
)

table RptSide (
id number,
party_id number
)

table Party (
id number,
parent_id number
)

[Class(Table="tc_alloc")]
public class Allocations
{
decimal _id;
ISet<Party> _Party = new HashSet<Party>();

[Id(1, Column="id", Type="Decimal")]
[Generator(2, Class="sequence")]
[Param(3, Content="tc_party_parent_seq", Name="sequence)]
virtual public decimal Id
{
....
}

[Set(1, Inverse=true, Cascade="save-update")]
[Key(2, ForeignKey="parent_id", Column="id")]
[OneToMany( 3, Class="ClearingReportDAO.Domain.Party, ClearingReportDAO")]
virtual public ISet<Party> Party
{
...
}

.
.
.
}

The other table (RptSide) is exactly the same.

[Class(Table="tc_party")]
public class Party
{
decimal _id;
RptSide _RptSide;
Allocation _Allc;

[Id(1, Column="id", Type="Decimal")]
[Generator(2, Class="sequence")]
[Param(3, Content="tc_party_parent_seq", Name="sequence)]
virtual public decimal Id
{
....
}

[ManyToOne(1, ForeignKey = "id", Column="parent_id", Class="ClearingReportDAO.Domain.RptSide, ClearingReportDAO, Cascade="save-update")]
virtual public RptSide RptSide
{
...
}

[ManyToOne(1, ForeignKey = "id", Column="parent_id", Class="ClearingReportDAO.Domain.Allocation, ClearingReportDAO, Cascade="save-update")]
virtual public Allocation Allocation
{
...
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 12, 2008 11:40 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
I think there at least two solutions:

- build a class hierarchy for Allocation and RptSide (e.g. same interface IHasSomethingToDoWithParty). Remember, you can still use separate tables for Allocations and RptSide. Then you can map the many-to-one from Party to IHasSomethingToDoWithParty instead of Allocation or RptSide
- you can use a "any" mapping: http://nhforge.org/doc/nh/en/index.html#mapping-types-anymapping


But I'm afraid I can't tell you how to do this with attributes. I've never used them.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 12, 2008 2:43 pm 
Newbie

Joined: Fri May 13, 2005 2:02 pm
Posts: 8
Thanks.

What I ended up doing was creating an interface to Party called IParty. Then two classes from that. One for allocations and one for rptside. Each had a discriminator.

Everything is working nicely now. Saving and retrieving.


[Class(Table = "tc_party")]
public interface IParty
{
[Id(1, Column = "id", Type = "Decimal", UnsavedValue = "-1")]
[Generator(2, Class = "sequence")]
[Param(3, Content = "tc_party_seq", Name = "sequence")]
decimal ID
{
get; set;
}

[Discriminator(Type = "string", Column = "parent_type")]
string ParentType
{
get; set;
}
}

[Subclass(DiscriminatorValue = "A", Extends = "ClearingReportDAO.Domain.IParty, ClearingReportDAO")]
public class AllocParty : IParty
{
private decimal _ID;
private string _ParentType;
private string _PartyID;
private decimal _PartyRole;

private Allocation _Allocation;

private ISet<SubParty> _SubParties = new HashedSet<SubParty>();

virtual public decimal ID
{
get { return _ID; }
set { _ID = value; }
}

[ManyToOne(1, ForeignKey = "id", Column = "parent_id", Class = "ClearingReportDAO.Domain.Allocation, ClearingReportDAO", Cascade = "save-update")]
virtual public Allocation Allocation
{
get { return _Allocation; }
set { _Allocation = value; }
}
.
.
.
}


[Subclass(DiscriminatorValue = "I", Extends = "ClearingReportDAO.Domain.IParty, ClearingReportDAO")]
public class RptSideParty : IParty
{
private decimal _ID;
//private decimal _ParentID;
private string _ParentType;
private string _PartyID;
private decimal _PartyRole;

private RptSide _rptSide;
private ISet<SubParty> _SubParties = new HashedSet<SubParty>();

virtual public decimal ID
{
get { return _ID; }
set { _ID = value; }
}

[ManyToOne(1, ForeignKey = "id", Column = "parent_id", Class = "ClearingReportDAO.Domain.RptSide, ClearingReportDAO")]
virtual public RptSide RptSide
{
get { return _rptSide; }
set { _rptSide = value; }
}
.
.
.
}


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.