The mapping of exhibit side:
Code:
@ManyToMany(fetch = FetchType.EAGER, cascade = {javax.persistence.CascadeType.MERGE, javax.persistence.CascadeType.PERSIST})
@Fetch(FetchMode.JOIN)
@JoinTable(name = "exhibit_tags", joinColumns = { @JoinColumn(name = "exhibit_id") }, inverseJoinColumns = { @JoinColumn(name = "tag_id") })
@Basic(optional = true)
public Set<Tag> getTags()
{
return this.tags;
}
Mapping of tag side:
Code:
@ManyToMany(fetch = FetchType.EAGER, targetEntity = Exhibit.class, mappedBy = "tags")
@Sort(type = SortType.COMPARATOR, comparator = Exhibit.class)
@Fetch(FetchMode.JOIN)
@Basic(optional = true)
public SortedSet<Exhibit> getExhibits()
{
return this.exhibits;
}
In my ExhibitDAO I update an exhibit like this:
Code:
public Exhibit update(Exhibit exhibit)
{
if (exhibit != null)
{
getSession().merge(exhibit);
}
return exhibit;
}
The n:m relation table is mapped like this:
Code:
@Entity
@Table(name = "exhibit_tags")
public class ExhibitTagRelation implements Serializable{
private Integer id;
private Exhibit exhibit;
private Tag tag;
public ExhibitTagRelation() {
}
@Id
@GeneratedValue
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@ManyToOne(optional = false)
@Fetch(FetchMode.JOIN)
@JoinColumn(name = "exhibit_id")
public Exhibit getExhibit() {
return this.exhibit;
}
public void setExhibit(Exhibit exhibit) {
this.exhibit = exhibit;
}
@ManyToOne(optional = false)
@Fetch(FetchMode.JOIN)
@JoinColumn(name = "tag_id")
public Tag getTag() {
return this.tag;
}
public void setTag(Tag tag) {
this.tag = tag;
}
public String toString() {
return this.getId().toString();
}
}
I work with Spring WebMVC. Exhibit is my command object which I load like:
Code:
final Exhibit exhibit = exhibitDAO.find(ServletRequestUtils
.getRequiredIntParameter(request, "id"));
//find method from exhibitDAO class:
public Exhibit find(Integer id)
{
return (Exhibit) getSession().createCriteria(Exhibit.class).add(Restrictions.eq("id", id)).uniqueResult();
}
Please tell me if you need more information, and thanks for any answers!