-->
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.  [ 6 posts ] 
Author Message
 Post subject: bi-directional one-to-many - HOW TO DELETE??? (ANNOTATIONS)
PostPosted: Thu Mar 26, 2009 1:40 am 
Newbie

Joined: Thu Mar 19, 2009 6:30 pm
Posts: 7
Hi, I have 2 classes in bidirectional one-to-many relation. User:
Code:
@Entity
@Table(name="users")
public class User implements Serializable {

    @Id
    @GeneratedValue
    private long id;

    @Column(name="name", length=50, nullable=false)
    private String name;

    @Column(name="password", length=20, nullable=false)
    private String password;

    //fetch is eager, as it is almost sure that notes list will be needed
    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER, mappedBy="author")
    @JoinColumn(name="user_id")
    private List<Note> notes;


and Note:
Code:
@Entity
@Table(name="notes")
public class Note implements Serializable {

    @Id
    @GeneratedValue
    private long id;

    @Column(name="date")
    @Temporal(TIMESTAMP)
    private Date date;

    @Column(name="content")
    private String content;

    @ManyToOne
    @JoinColumn(name="user_id")
    private User author;


How can I remove some note? I was trying this way:
Code:
session.beginTransaction();
            User user = Utils.getLoggedUser();
            Note note = (Note)session.get(Note.class, id);
            if(user.getNotes().contains(note)) {
                user.getNotes().remove(note);
                session.update(user);
            }

But it doesn't work!!! I was trying many ways like:
session.delete(note), and other combinations. Nothing helps so far. Even this:
Code:
@Cascade(CascadeType.DELETE_ORPHAN)
    @OneToMany(cascade=javax.persistence.CascadeType.ALL, fetch=FetchType.EAGER, mappedBy="author")
    @JoinColumn(name="user_id")
    private List<Note> notes;


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 26, 2009 3:44 am 
Newbie

Joined: Thu Mar 19, 2009 6:30 pm
Posts: 7
PLEASE HELP!!! Does anybody was deleting object from bidirectional oneToMany relation?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 26, 2009 4:05 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Are you getting any exceptions?

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 26, 2009 4:32 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Have you tried to add a cascade option for DELETE_ORPHAN as described in http://www.hibernate.org/hib_docs/annot ... ec-cascade


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 26, 2009 4:43 am 
Newbie

Joined: Thu Mar 19, 2009 6:30 pm
Posts: 7
Now it finally works (almost). Note can be deleted now, but when I try to add another note I get exception:

Batch update returned unexpected row count from update [1]; actual row count: 0; expected: 1

When I log out and log in again I can add another notes, until I delete one.

Can you help me?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 26, 2009 6:21 am 
Newbie

Joined: Thu Mar 19, 2009 6:30 pm
Posts: 7
my solution:

Code:
session.update(user);
session.flush();
session.refresh(user);


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.