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;}
}