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.  [ 4 posts ] 
Author Message
 Post subject: Inheritance using composite primary keys
PostPosted: Fri Dec 01, 2006 6:20 am 
Newbie

Joined: Fri Dec 01, 2006 5:02 am
Posts: 2
I want to create an inheritance structure by using annotations; the inheritance type is InheritanceType.JOINED. When using an integer as ID in the base entity everything works fine, but when using a composite ID the binding of the concrete entity fails.
Does anyone why?

The code looks like follows:
[code]@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Base implements Serializable {
private BasePK pk;

@EmbeddedId
public BasePK getPk() {
return pk;
}

public void setPk(BasePK pk) {
this.pk = pk;
}
}[/code]

[code]@Entity
@PrimaryKeyJoinColumns( {
@PrimaryKeyJoinColumn(name = "firstID", referencedColumnName = "firstID"),
@PrimaryKeyJoinColumn(name = "secondID", referencedColumnName = "secondID") })
public class Concrete extends Base {
}[/code]

[code]@Embeddable
public class BasePK implements Serializable {
private Long firstID;
private Foo secondID;

public BasePK() {
}

public Long getFirstID() {
return firstID;
}

public void setFirstID(Long firstID) {
this.firstID = firstID;
}

@ManyToOne
@JoinColumn(name = "secondID")
public Foo getSecondID() {
return secondID;
}

public void setSecondID(Foo secondID) {
this.secondID = secondID;
}

@Override
public boolean equals(Object obj) {...}

@Override
public int hashCode() { ...}
}[/code]

[code]@Entity
public class Foo implements Serializable {
private Long fooID;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getFooID() {
return fooID;
}

public void setFooID(Long fooID) {
this.fooID = fooID;
}
}[/code]

[b]Hibernate version:[/b] 3.2

[b]Name and version of the database you are using:[/b] MySQL 5.0

[b]Full stack trace of any exception that occurs:[/b]

org.hibernate.MappingException: Unable to find column with logical name: secondID in Base
at org.hibernate.cfg.Ejb3JoinColumn.checkReferencedColumnsType(Ejb3JoinColumn.java:383)
at org.hibernate.cfg.annotations.TableBinder.bindFk(TableBinder.java:210)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:580)
at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:452)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:268)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1283)
at hibernate.test.inheritance.HibernateUtil.<clinit>(HibernateUtil.java:13)
at hibernate.test.inheritance.Main.main(Main.java:8)
Exception in thread "main" java.lang.NullPointerException
at hibernate.test.inheritance.HibernateUtil.getSession(HibernateUtil.java:20)
at hibernate.test.inheritance.Main.main(Main.java:8)
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 16, 2007 9:45 am 
Beginner
Beginner

Joined: Wed Jul 14, 2004 11:51 am
Posts: 43
Did you find an answer to your question? I have a very similar problem:


BASE CLASS
=========
Code:
@javax.persistence.Entity
@javax.persistence.Table(name="ARTICLE2LOCALE")
@javax.persistence.Inheritance(strategy=javax.persistence.InheritanceType.JOINED)
@org.hibernate.annotations.Entity(dynamicUpdate=true)
public class ArticleLocalization implements org.dataisland.primitives.bean.Localization, Serializable {

    public ArticleLocalization(ArticleLocalizationPK localizationPK) {
        this.articleLocalizationPK = localizationPK;
    }
    private ArticleLocalizationPK articleLocalizationPK;
   
    @javax.persistence.Id
    public ArticleLocalizationPK getArticleLocalizationPK() {
        return articleLocalizationPK;
    }
    public void setArticleLocalizationPK(ArticleLocalizationPK articleLocalizationPK) {
        this.articleLocalizationPK = articleLocalizationPK;
    }


    private java.lang.String industry;
    @org.hibernate.annotations.Type(type="string")
    @javax.persistence.Column(name="INDUSTRY"
    )
    public java.lang.String getIndustry() {
        return this.industry;
    }
    public void setIndustry(java.lang.String value) {
        this.industry = value;
    }
}



PKCLASS
=======

Code:
@javax.persistence.Embeddable
public class ArticleLocalizationPK implements Serializable {
    private static final long serialVersionUID = 8843238238156809823L;
   
    public ArticleLocalizationPK(Article article, Locale locale) {
        this.article = article;
        this.locale = locale;
    }
   
    private Locale locale;

    @org.hibernate.annotations.Type(type="locale")
    @javax.persistence.Column(name="LOCALE")
    public Locale getLocale() {
        return locale;
    }
    public void setLocale(Locale locale) {
        this.locale = locale;
    }

    private Article article;
   
    @javax.persistence.ManyToOne()
    @javax.persistence.JoinColumn(name="ELEMENT_FK", insertable=false, updatable=false)
    public Article getArticle() {
        return this.article;
    }
   
    public void setArticle(Article article) {
        this.article = article;
    }
...
}



SUBCLASS
========
Code:
@javax.persistence.Entity
@javax.persistence.Table(name="ITEM_ARTICLE2LOCALE")
@javax.persistence.PrimaryKeyJoinColumns({@javax.persistence.PrimaryKeyJoinColumn(name="LOCALE", referencedColumnName="LOCALE"), @javax.persistence.PrimaryKeyJoinColumn(name="ELEMENT_FK", referencedColumnName="ELEMENT_FK")})
public class ItemArticleLocalization extends ArticleLocalization {


    public ItemArticleLocalization(ArticleLocalizationPK localizationPK) {
        super(localizationPK);
    }
   
    private java.lang.String name;
    @org.hibernate.annotations.Type(type="string")
    @javax.persistence.Column(name="NAME"
    )
    public java.lang.String getName() {
        return this.name;
    }
    public void setName(java.lang.String value) {
        this.name = value;
    }
}


I get the following error with the previous code:

Quote:
org.hibernate.MappingException: Unable to find column with logical name: ELEMENT_FK in ARTICLE2LOCALE


And when I omit the @PrimaryKeyJoinColumns (because the doc says that it would take the same names as the parent class, I get the following exception:

Quote:
org.hibernate.MappingException: Foreign key (FK9DADD222AB4811E6:ITEM_ARTICLE2LOCALE [LOCALE])) must have same number of columns as the referenced primary key (ARTICLE2LOCALE [LOCALE,ELEMENT_FK])



If anybody has a clue, it would be greatly appreciated,

Thanks,

Francois


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 16, 2007 10:16 am 
Newbie

Joined: Fri Dec 01, 2006 5:02 am
Posts: 2
Hi Francois,

I couldn't solve that problem, but decided to use surrogate primary keys instead. It is suggested to use composite keys (making PK classes necessary) only for legacy systems. Furthermore, the usage of surrogates makes it much more easier if the system becomes more complex.

Sorry that I couldn't answer your question, but my advice is to switch to surrogate keys if possible.

cheers,
Confuzius


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 16, 2007 12:11 pm 
Beginner
Beginner

Joined: Wed Jul 14, 2004 11:51 am
Posts: 43
Hi Confusiuz,

Thanks for your prompt reply... I'll give it a try using sorrogate keys then.

Thanks,

François


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.