I have a problem with a hibernate mapping for which I didn't get an answer on the IRC channel and now I want to try it here. Please don't tell me anything about design considerations or so. I just want to know how to get the following mapping to work. I can't create the SessionFactory/EntityManagerFactory because of an error that doesnt seem like an error to me but more like a bug. Anyways I could also be false and just mapped the relations I would like to have wrong. So here is the example code that should illustrate what kind of mapping I would like to have:
Code:
@Embeddable
public class ArticleId implements java.io.Serializable {
private String articleId1;
private String articleId2;
@Column(name = "article_id_1", nullable = false)
public String getArticleId1() {
return articleId1;
}
public void setArticleId1(String articleId1) {
this.articleId1= articleId1;
}
@Column(name = "article_id_2", nullable = false)
public String getArticleId2() {
return articleId2;
}
public void setArticleId2(String articleId2) {
this.articleId2= articleId2;
}
}
@javax.persistence.Entity
@Table(name = "article")
public class Article implements java.io.Serializable {
private ArticleId id;
private Set<ArticleAttributeMapping> articleAttributeMappings = new HashSet<ArticleAttributeMapping>();
@EmbeddedId
public ArticleId getId() {
return id;
}
public void setId(ArticleId id) {
this.id = id;
}
@OneToMany(mappedBy="article")
public Set<ArticleAttributeMapping> getArticleAttributeMappings() {
return articleAttributeMappings ;
}
public void setArticleAttributeMappings(Set<ArticleAttributeMapping> articleAttributeMappings ) {
this.articleAttributeMappings = articleAttributeMappings ;
}
}
@Embeddable
public class AttributeId implements java.io.Serializable {
private String attributeId1;
private String attributeId2;
@Column(name = "attribute_id_1", nullable = false)
public String getAttributeId1() {
return attributeId1;
}
public void setAttributeId1(String attributeId1) {
this.attributeId1= attributeId1;
}
@Column(name = "attribute_id_2", nullable = false)
public String getAttributeId2() {
return attributeId2;
}
public void setAttributeId2(String attributeId2) {
this.attributeId2= attributeId2;
}
}
@javax.persistence.Entity
@Table(name = "attribute")
public class Attribute implements java.io.Serializable {
private AttributeId id;
private Set<ArticleAttributeMapping> articleAttributeMappings = new HashSet<ArticleAttributeMapping>();
@EmbeddedId
public AttributeId getId() {
return id;
}
public void setId(AttributeId id) {
this.id = id;
}
@OneToMany(mappedBy="attribute")
public Set<ArticleAttributeMapping> getArticleAttributeMappings() {
return articleAttributeMappings ;
}
public void setArticleAttributeMappings(Set<ArticleAttributeMapping> articleAttributeMappings ) {
this.articleAttributeMappings = articleAttributeMappings ;
}
}
@Embeddable
public class ArticleAttributeMappingId implements java.io.Serializable {
private String articleId1;
private String articleId2;
private String attributeId1;
private String attributeId2;
@Column(name = "mapping_article_id_1", nullable = false)
public String getArticleId1() {
return articleId1;
}
public void setArticleId1(String articleId1) {
this.articleId1= articleId1;
}
@Column(name = "mapping_article_id_2", nullable = false)
public String getArticleId2() {
return articleId2;
}
public void setArticleId2(String articleId2) {
this.articleId2= articleId2;
}
@Column(name = "mapping_attribute_id_1", nullable = false)
public String getAttributeId1() {
return attributeId1;
}
public void setAttributeId1(String attributeId1) {
this.attributeId1= attributeId1;
}
@Column(name = "mapping_attribute_id_2", nullable = false)
public String getAttributeId2() {
return attributeId2;
}
public void setAttributeId2(String attributeId2) {
this.attributeId2= attributeId2;
}
}
@javax.persistence.Entity
@Table(name = "article_attribute_mapping")
public class ArticleAttributeMapping implements java.io.Serializable {
private ArticleAttributeMappingId id;
private Article article;
private Attribute attribute;
private String value;
@EmbeddedId
public ArticleAttributeMappingId getId() {
return id;
}
public void setId(ArticleAttributeMappingId id) {
this.id = id;
}
@ManyToOne(optional = false)
@JoinColumns({
@JoinColumn(name = "mapping_article_id_1", referencedColumnName = "article_id_1", nullable = false, insertable = false, updatable = false),
@JoinColumn(name = "mapping_article_id_2", referencedColumnName = "article_id_2", nullable = false, insertable = false, updatable = false) })
public Article getArticle() {
return this.article;
}
public void setArticle(Article article) {
this.article = article;
}
@ManyToOne(optional = false)
@JoinColumns({
@JoinColumn(name = "mapping_attribute_id_1", referencedColumnName = "attribute_id_1", nullable = false, insertable = false, updatable = false),
@JoinColumn(name = "mapping_attribute_id_2", referencedColumnName = "attribute_id_2", nullable = false, insertable = false, updatable = false) })
public Attribute getAttribute() {
return this.attribute;
}
public void setAttribute(Attribute attribute) {
this.attribute = attribute;
}
}
Also note that I asked the question on stackoverflow: http://stackoverflow.com/questions/16225410/hibernate-bidirectional-mapping-with-composite-keys