Sorry, turns out that I actually have a Set of Bs, though I believe the principle behind the question remains the same.
Code:
public class A implements Serializable {
int id;
private Set<B> foos;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Set<B> getFoos() {
return foos;
}
public void setFoo(Set<B> foos) {
this.foos = foos;
}
}
public class B implements Serializable {
int id;
private String bar;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBar() {
return bar;
}
public void setBar(String bar) {
this.bar = bar;
}
}
Code:
<hibernate-mapping>
<class name="A" table="as">
<id name="id" type="int">
<column name="id_a"/>
<generator class="increment"/>
</id>
<set name="foos"
table="a_foos"
cascade="save-update">
<key column="id_a"/>
<many-to-many class="B" column="id_b"/>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="B" table="bs">
<id name="id" type="int">
<column name="id_b"/>
<generator class="increment"/>
</id>
<property name="bar" type="string">
<column name="b_bar" not-null="true" unique="true"/>
</property>
</class>
<hibernate-mapping>
I suspect that the problem right now might lie in the fact that the unique constraint is only on a single column of B. Is there a simple way to get the desired behavior?