-->
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.  [ 3 posts ] 
Author Message
 Post subject: Update on entity causes link table rows to be deleted
PostPosted: Thu Jan 21, 2010 3:31 pm 
Beginner
Beginner

Joined: Thu Feb 28, 2008 11:53 am
Posts: 23
I have this entity which has a
Code:
@ManyToMany
association; below is a snippet of the entity with relevant parts
Code:
public class Label implements Serializable,Comparable<Label>{
    private static final long serialVersionUID = 123L;
    private String text;
    @Column(updatable=false)
    private String code;
    @Id
    @Column(name="label_id")
    @SequenceGenerator(name = "labelSeq", sequenceName = "label_id_seq")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "labelSeq")
    private Long id;
    @Column(name="client_id",updatable=false)
    private Long clientId;
    @org.hibernate.annotations.Type(type="boolean")
    @Column(name="is_system_label",updatable=false)
    private boolean isSystemLabel;
    @ManyToMany
    @JoinTable(
        name="labels_ctg_amounts",
        joinColumns = {@JoinColumn(name = "label_id")},
        inverseJoinColumns = {@JoinColumn(name = "ctg_id")}
    )
    @org.hibernate.annotations.Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
    @org.hibernate.annotations.Fetch(org.hibernate.annotations.FetchMode.SUBSELECT)
    @org.hibernate.annotations.LazyCollection(org.hibernate.annotations.LazyCollectionOption.EXTRA)
    private Set<Contingency> contingencies;


Now when I do a simple update on a Label with code like this:
Code:
Label l = new Label();
l.setId(52000L);
l.setText("test update capability");
session.saveOrUpdate(l);


I get the following partial dump:
Code:
16:26:08.319 [main] DEBUG org.hibernate.SQL -
    update
        labels
    set
        text=?
    where
        label_id=?
Hibernate:
    update
        labels
    set
        text=?
    where
        label_id=?
16:26:08.460 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - Executing batch size: 1
16:26:08.476 [main] DEBUG org.hibernate.jdbc.Expectations - success of batch update unknown: 0
16:26:08.476 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
16:26:08.476 [main] DEBUG o.h.p.c.AbstractCollectionPersister - Deleting collection: [net.taxstream.fin48.domain.Label.contingencies#52000]
16:26:08.476 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
16:26:08.476 [main] DEBUG org.hibernate.SQL -
    delete
    from
        labels_ctg_amounts
    where
        label_id=?
Hibernate:
    delete
    from
        labels_ctg_amounts
    where
        label_id=?
16:26:08.476 [main] DEBUG o.h.p.c.AbstractCollectionPersister - done deleting collection


I don't want the rows in the link table deleted. However if I use a proxy like this:
Code:
Label l = (Label)session.load(52000L);
l.setText("test update capability");
session.saveOrUpdate(l);

This works just great as I would expect -- a simple update of text and that's it. But i cannot rely on this second method as the updated Label object is bound to values in the Spring controller therefore using this second method would require me to set the properties in the proxy with the properties from the bound object, something like this:
Code:
Label l = (Label)session.load(52000L);;
l.setText(boundLabel.getText());//boundLabel is bound to values from the client via the Spring controller
...//setting every property is totally unacceptable


Any ideas on how I can resolve this?


Top
 Profile  
 
 Post subject: Re: Update on entity causes link table rows to be deleted
PostPosted: Mon Jan 25, 2010 5:48 am 
Newbie

Joined: Mon Jan 25, 2010 3:25 am
Posts: 6
Hi,

Please check if you are using cascade = "delete-orphan" in the hbm mapping.
The behavior described below you will get in the scenario where cascade = "delete-orphan" is used.
delete-orphan: automatically delete any object for whom the reference has been removed from the referencing object.

Hope this will help.

Regards,
Sudhir


Top
 Profile  
 
 Post subject: Re: Update on entity causes link table rows to be deleted
PostPosted: Mon Jan 25, 2010 4:29 pm 
Beginner
Beginner

Joined: Thu Feb 28, 2008 11:53 am
Posts: 23
You obviously didn't read the question -- I'm using annotations not xml and I am not removing any entity from a collection. The issue was solved by making Contingency the owner of the relationship and not Label.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.