-->
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: Remove elements from subcollection on update
PostPosted: Sat Jun 09, 2012 8:22 am 
Newbie

Joined: Sat Jun 09, 2012 8:08 am
Posts: 1
Hi,

I have two entity classes. ClientProfile.java and ClientOtherLoan.java.
They are related with OneToMany.

ClientProfle:
Code:
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "client", orphanRemoval = true)
@Fetch (FetchMode.SELECT)
private List<ClientOtherLoan> otherLoans;


ClientOtherLoan:
Code:
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "client_id", nullable = false)
@ForeignKey(name = "fk_client_other_loan")
private ClientProfile client;


When I insert or update client profile everything works fine - client other loans are also inserted/updated.
But when I want to delete some of the loans (removing them from otherLoans list and calling update() on client), they won't disappear from DB.
They still exist.

Before calling update() on ClientProfile object I will loop through otherLoans and remove those that are empty:
Code:
private ClientProfile manageOtherLoans(ClientProfile client) throws ControllerException {
        if (client.getOtherLoans() != null) {
            List<ClientOtherLoan> list = new ArrayList<ClientOtherLoan>();
            for (ClientOtherLoan loan : client.getOtherLoans()) {
                if (loan.getAmount() != null && loan.getPayback() != null) {
                    loan.setClient(client);
                    list.add(loan);
                }
            }
            client.setOtherLoans(list);
        }
        return client;
}


I was expecting that if hibernate removes all loans it doesn't find from otherLoans list - but it doesn't.
Can anyone help and say what am I doing wrong?

Many thanks,
Risto


Top
 Profile  
 
 Post subject: Re: Remove elements from subcollection on update
PostPosted: Mon Jun 11, 2012 8:37 am 
Beginner
Beginner

Joined: Mon Jun 04, 2012 12:31 pm
Posts: 20
What does setOtherLoans() look like?

You can't directly set a Collection to the otherLoans property.

You need to pull the instance of the Collection and then modify it.

Code:
client.getOtherLoans().clear();
client.getOtherLoans().addAll(list);


That's one approach.


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.