I have an object mapped with hibernate that contains an id and a collection. At some point in time when the object contains items in it's collection I want to do obj.setCollection(new ArrayList())
and have it delete the items from the database that were previously in the collection. If anyone could assist it would be much appreciated. Code to illustrate problem:
ORM: Foo 1-* Bar and Bar 1-1 Foo
class Foo {
Long id;
List<Bar> bars;
//getters & setters
}
<hibernate-mapping>
<class name="domain.Foo" table="Foos">
<id name="id" type="long" column="id">
<generator class="native" />
</id>
<list name="bars" lazy="true" cascade="all">
<key column="FOO_ID" not-null="true" />
<index column="SEQUENCE" />
<one-to-many class="domain.Bar" />
</list>
</class>
</hibernate-mapping>
@Entity
@Table(name="Bars")
public class Bar {
private Long id;
private Foo foo;
private String name;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="ID")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@ManyToOne
@JoinColumn(name="FOO_ID", insertable=false, updatable=false)
public Foo getFoo() {
return foo;
}
public void setFoo(Foo foo) {
this.foo = foo;
}
@Column(name="NAME")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}