Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Filtering a OneToMany relationship
Filtering a OneToMany relationship [message #531440] Tue, 04 May 2010 21:55 Go to next message
Chris Mathrusse is currently offline Chris MathrusseFriend
Messages: 24
Registered: July 2009
Junior Member
I'm using EclipseLink 2.0.2 and I have a OneToMany relationship. Let's call my first entity Party and my second one Address:

@Entity
@Table(name="Party")
public class Party {
	@OneToMany(cascade = CascadeType.ALL, mappedBy = "party")
	private Set<Address> addresses;
...
}

@Entity
@Table(name = "Address")
public class Address  {
    @JoinColumn(name = "PartyID", referencedColumnName = "PartyID", nullable = false)
    @ManyToOne(optional = false)
    private Party party;

    @Column(name = "isDeleted", nullable = false)
    private boolean isDeleted;
...
}



My problem is that our system never deletes Address record but rather performs a logical delete. So what I need, from the above relationship, is to retrieve only the Addresses that have isDeleted == false.

Is there any way I can accomplish this?

Thanks for the help...
Re: Filtering a OneToMany relationship [message #531611 is a reply to message #531440] Wed, 05 May 2010 14:56 Go to previous messageGo to next message
Gilberto Caetano de Andrade is currently offline Gilberto Caetano de AndradeFriend
Messages: 45
Registered: July 2009
Member
JSR-317 Final Release:
Quote:

3.5.2 Semantics of the Life Cycle Callback Methods for Entities
.
.
.
The PostLoad method for an entity is invoked after the entity has been loaded into the current persistence context from the database or after the refresh operation has been applied to it. The PostLoad method is invoked before a query result is returned or accessed or before an association is traversed. It is implementation-dependent as to whether callback methods are invoked before or after the cascading of the lifecycle events to related entities. Applications should not depend on this ordering.
.
.
.
3.5.3   Example
@Entity
@EntityListeners(com.acme.AlertMonitor.class)
public class Account {
Long accountId;
Integer balance;
boolean preferred;
@Id
public Long getAccountId() { ... }
...
public Integer getBalance() { ... }
...
    @Transient // because status depends upon non-persistent context
    public boolean isPreferred() { ... }
...
    public void deposit(Integer amount) { ... }
    public Integer withdraw(Integer amount) throws NSFException {... }
    @PrePersist
    protected void validateCreate() {
        if (getBalance() < MIN_REQUIRED_BALANCE)
        throw new AccountException("Insufficient balance to open an 
account");
    }
    @PostLoad
    protected void adjustPreferredStatus() {
        preferred =
            (getBalance() >= AccountManager.getPreferredStatu-
sLevel());
    }
}
public class AlertMonitor {
    @PostPersist
    public void newAccountAlert(Account acct) {
        Alerts.sendMarketingInfo(acct.getAccountId(), acct.getBal-
ance());
}
}




You've got the idea!

Gilberto

[Updated on: Wed, 05 May 2010 14:59]

Report message to a moderator

Re: Filtering a OneToMany relationship [message #531613 is a reply to message #531611] Wed, 05 May 2010 15:12 Go to previous messageGo to next message
Chris Mathrusse is currently offline Chris MathrusseFriend
Messages: 24
Registered: July 2009
Junior Member
I'm sorry, but I must be missing something here. Based upon your recommendation I would need to traverse the list of Entities that are retrieved from the database and then remove the ones that I don't want from the list in a @PostLoad operation?

Somehow that just doesn't seem right. Isn't there a way that I can simply tell EclipseLink to load only the Entities that match a certain criteria rather than performing a load based upon a foreign key alone? I'm attempting to reproduce functionality that currently exists in an existing application that is using Hibernate, and looking at the mapping file for this relationship, there is specified a where attribute, which allows you to filter the joined rows. Doesn't EclipseLink have something similar?
Re: Filtering a OneToMany relationship [message #531656 is a reply to message #531613] Wed, 05 May 2010 17:36 Go to previous messageGo to next message
Gilberto Caetano de Andrade is currently offline Gilberto Caetano de AndradeFriend
Messages: 45
Registered: July 2009
Member
Chris Mathrusse wrote on Wed, 05 May 2010 11:12
I'm sorry, but I must be missing something here. Based upon your recommendation I would need to traverse the list of Entities that are retrieved from the database and then remove the ones that I don't want from the list in a @PostLoad operation?


Yes!

Chris Mathrusse wrote on Wed, 05 May 2010 11:12

Somehow that just doesn't seem right. Isn't there a way that I can simply tell EclipseLink to load only the Entities that match a certain criteria rather than performing a load based upon a foreign key alone?



I don't think so!

Chris Mathrusse wrote on Wed, 05 May 2010 11:12

I'm attempting to reproduce functionality that currently exists in an existing application that is using Hibernate, and looking at the mapping file for this relationship, there is specified a where attribute, which allows you to filter the joined rows. Doesn't EclipseLink have something similar?

Are you talking about this one?
I have not seen anything like it in the specification. Perhaps by an extension the eclipselink project could provide something, but I don't know it enough to comment about.
Re: Filtering a OneToMany relationship [message #531664 is a reply to message #531656] Wed, 05 May 2010 17:53 Go to previous messageGo to next message
Chris Mathrusse is currently offline Chris MathrusseFriend
Messages: 24
Registered: July 2009
Junior Member
No, I was referring to the where attribute of the set element defined in http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd

 <set name="addresses" cascade="all-delete-orphan"
            lazy="true" where="isDeleted = 0">
            <key column="PartyID" not-null="true"/>
            <one-to-many class="com....model.Address"/>
 </set>

Re: Filtering a OneToMany relationship [message #531877 is a reply to message #531656] Thu, 06 May 2010 13:16 Go to previous messageGo to next message
Chris Mathrusse is currently offline Chris MathrusseFriend
Messages: 24
Registered: July 2009
Junior Member
The more I thought about the PostLoad operation and the removing entities from it the less likely it seemed that it would work as the list of associated objects are contained with in a List that is managed by the Persistence Provider. If I were to remove any from the list it would effectively be the same as a delete operation.

Isn't there some way that I intercept or modify the query that EclipseLink is going to execute when loading the associated objects?
Re: Filtering a OneToMany relationship [message #531988 is a reply to message #531440] Thu, 06 May 2010 17:24 Go to previous message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

In EclipseLink you can use a DescriptorCustomizer (@Customizer) to set an additionalJoinExpression for you class that will filter all "deleted"=true rows for every query.

You can also customize the selectionCriteria per mapping.

EclipseLink also have full history support that will automatically maintain a mirror table with a history of changes and a start and end column.


James : Wiki : Book : Blog : Twitter
Previous Topic:JPA Questions
Next Topic:NamedNativeQuery
Goto Forum:
  


Current Time: Thu Apr 25 12:44:46 GMT 2024

Powered by FUDForum. Page generated in 0.03583 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top