-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Problem with bidirectional mapping and composite keys
PostPosted: Thu Apr 25, 2013 5:58 pm 
Beginner
Beginner

Joined: Sat May 21, 2011 7:40 am
Posts: 22
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


Top
 Profile  
 
 Post subject: Re: Problem with bidirectional mapping and composite keys
PostPosted: Fri Apr 26, 2013 9:01 am 
Beginner
Beginner

Joined: Sat May 21, 2011 7:40 am
Posts: 22
I found out that this example actually works and now found the root of my real problem. I was mostly confused by the error message hibernate gave me because it wasn't clear about the location of the problem.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.