views:

102

answers:

1

Hi, I have four hibernate classes: Trader, Legal, Party and Relation. Trader and Legal are types of party so extend it. A Trader can be related to a legal, a trader can be related to a trader and a legal to a legal. Both types can b the child or parent in the relationship. These relationhips are maintained in the Relation class. Both the Legal and Trader classes have a collection of Relation classes, and the Relation class has a parentParty and a childParty both of type party. What I want to do using the Criteria api is select a Trader and return all relations where the trader is the child and the parent is a Legal. I have tried using filters, but I can't see how these can be applied across join asociations. Also I have specified a sub criteria in my query that only looks for partyType=legal, but in all cases the collections always populated with all the parent parties of both types, which is not what I want. Any help would be appreciated.

A: 

Yes, sure. The class names are slightly different from the original post but you'll see the relationships between them easily enough. So what I want to do is get a TradingCounterparty containing a set of PartyRelations where the ParentParty is of type LegalEntity only. I want to filter out any PartyRelations where the ParentParty is a TradingCounterparty.

It's the partyTypeCode in the Part class that flags the party type, so this is what I need to filter on.

@Entity

@Table(name = "Party", schema = "dbo") @Inheritance(strategy = InheritanceType.JOINED) public class Party {

private Integer partyId; private String partyTypeCode;

@Id @Column(name = "partyId") public Integer getPartyId() { return this.partyId; }

public void setPartyId(Integer partyId) { this.partyId = partyId; }

@Column(name = "partyTypeCode", insertable = false, updatable = false) public String getPartyTypeCode() { return this.partyTypeCode; }

public void setPartyTypeCode(String partyTypeCode) { this.partyTypeCode = partyTypeCode; } }

@Entity @Table(name = "TradingCounterparty", schema = "dbo") @PrimaryKeyJoinColumn(name="partyId") @XStreamAlias("tradingCounterparty") public class TradingCounterparty extends Party{

private String name; @XStreamAlias("partyRelations") private Set partyRelations;

@Column(name="name")
public String getName() { return this.name; }

public void setName(String name) { this.name = name; }

@OneToMany(mappedBy = "childParty") public Set getPartyRelations() { return partyRelations; }

public void setPartyRelations(Set partyRelations) { this.partyRelations = partyRelations; }

}

@Entity @Table(name = "LegalEntity", schema = "dbo") @PrimaryKeyJoinColumn(name="partyId") @XStreamAlias("legalEntity") public class LegalEntity extends Party{

private String name;
@XStreamAlias("tradingCounterParties")
private Set<PartyRelation> partyRelations;
@Column(name="legalEntityFullName")
public String getName() { return this.name; }

public void setName(String name) { this.name = name; }

@OneToMany(mappedBy = "parentParty") public Set getPartyRelations() { return partyRelations; }

public void setPartyRelations(Set partyRelations) { this.partyRelations = partyRelations; }

}

@Entity @Table(name = "PartyRelation", schema = "dbo") @XStreamAlias("partyRelation") public class PartyRelation {

private Integer partyRelationId; private PartyRelationType partyRelationType; @XStreamAlias("childParty") private Party childParty; @XStreamAlias("parentParty") private Party parentParty;

@Id @Column(name = "partyRelationId") public Integer getPartyRelationId() { return partyRelationId; }

public void setPartyRelationId(Integer partyRelationId) { this.partyRelationId = partyRelationId; }

@ManyToOne @JoinColumn(name="partyRelationTypeCode") public PartyRelationType getPartyRelationType() { return partyRelationType; }

public void setPartyRelationType(PartyRelationType partyRelationType) { this.partyRelationType = partyRelationType; }

@ManyToOne @JoinColumn(name="childPartyId") public Party getChildParty() { return childParty; }

public void setChildParty(Party childParty) { this.childParty = childParty; }

@ManyToOne @JoinColumn(name="parentPartyId")
public Party getParentParty() { return parentParty; }

public void setParentParty(Party parentParty) { this.parentParty = parentParty; }
}

Thanks in advance.

barryr
Did you answer your own question with a second account? It's best to post updates to the original question as edits, not answers.
matt b