-->
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: Set null value on delete with @OneToMany
PostPosted: Mon Apr 11, 2011 10:52 am 
Newbie

Joined: Mon Apr 11, 2011 10:14 am
Posts: 1
Hi,
I have two classes, Person and Position. One Person can be assigned to many Positions, and one Position can have only one Person (or no Person assigned). Everything works fine, but deletions of Persons. ConstraintViolationException occurs. And when I use "cascade=CascadeType.ALL" it deletes Position too, what I don't want to.

I need to set annotations to achieve this:
when I delete Person, hibernate will set null value to all "connected" Positions and delete Person itself. Is it possible?
Did I missed anything?

Code:
@Entity
@Table(name="persons")
public class PersonDAO {

    private Long id;
    private List positions;

    public PersonDAO() {}

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(updatable = false, nullable = false)
    public Long getId() {return this.id;}
    public void setId(Long id) {this.id=id;}

    @OneToMany(mappedBy="person")
    public List getPositions() {return this.positions;}
    public void setPositions(List positions) {this.positions=positions;}
}

Code:
@Entity
@Table(name="org_positions")
public class PositionDAO {
    private Long id;
    private PersonDAO person;

    public PositionDAO() {}

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(updatable = false, nullable = false)
    public Long getId() {return this.id;}
    public void setId(Long id) {this.id=id;}

    @OneToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="person", nullable=true)
    public PersonDAO getPerson() {return person;}
    public void setPerson(PersonDAO person) {this.person=person;}
}


Top
 Profile  
 
 Post subject: Re: Set null value on delete with @OneToMany
PostPosted: Tue Apr 26, 2011 4:00 pm 
Beginner
Beginner

Joined: Wed Sep 03, 2003 9:31 am
Posts: 26
Did you try this?

@OneToMany(mappedBy="person", orpanRemoval=true)


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.