| Filtering a OneToMany relationship [message #531440] |
Tue, 04 May 2010 17:55  |
Chris Mathrusse 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 10:56   |
Gilberto Caetano de Andrade Messages: 44 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 10:59] Report message to a moderator
|
|
|
|
|
|
|
| Re: Filtering a OneToMany relationship [message #531988 is a reply to message #531440] |
Thu, 06 May 2010 13:24  |
James Sutherland Messages: 1834 Registered: July 2009 |
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
|
|
|
Powered by
FUDForum. Page generated in 0.01968 seconds