-->
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: Composite Foreign Key Overlap Foreign Key
PostPosted: Tue Jun 16, 2009 11:08 am 
Newbie

Joined: Tue Jun 16, 2009 10:37 am
Posts: 2
Hey all, quick question here.

I have three tables, as follows:
Code:
CREATE TABLE `types` (
  `TYPE_ID` int(10) NOT NULL AUTO_INCREMENT,
  `NAME` varchar(50) NOT NULL,
  PRIMARY KEY (`TYPE_ID`)
);

Code:
CREATE TABLE `ranks` (
  `RANK` int(10) NOT NULL DEFAULT '0',
  `TYPE_ID` int(10) NOT NULL,
  `DESCRIPTION` varchar(100) NOT NULL,
  PRIMARY KEY (`RANK`,`TYPE_ID`),
  KEY `RANKS_TYPE_ID_FK_IDX` (`TYPE_ID`),
  CONSTRAINT `RANKS_TYPE_ID_FK` FOREIGN KEY (`TYPE_ID`) REFERENCES `types` (`TYPE_ID`)
)

Code:
CREATE TABLE `tasks` (
  `TASK_ID` int(10) NOT NULL AUTO_INCREMENT,
  `TYPE_ID` int(10) NOT NULL,
  `RANK` int(10) DEFAULT NULL,
  PRIMARY KEY (`TASK_ID`),
  KEY `TASKS_TYPE_ID_FK_IDX` (`TYPE_ID`),
  KEY `TASKS_RANK_FK_IDX` (`RANK`,`TYPE_ID`),
  CONSTRAINT `TASKS_TYPE_ID_FK` FOREIGN KEY (`TYPE_ID`) REFERENCES `types` (`TYPE_ID`),
  CONSTRAINT `TASKS_RANK_FK` FOREIGN KEY (`RANK`, `TYPE_ID`) REFERENCES `ranks` (`RANK`, `TYPE_ID`)
)


This is how the Task entity is defined:
Code:
@Entity
@Table( name = "tasks" )
public class Task implements java.io.Serializable
{
    private int id;
    private Type type;
    private Rank rank;

    public Task( )
    {

    }

    public Task( int id, Type type, Rank rank )
    {
        this.id = id;
        this.type = type;
        this.rank = rank;
    }

    @Id
    @Column( name = "TASK_ID", unique = true, nullable = false)
    public int getId( )
    {
        return this.id;
    }

    public void setId( int id )
    {
        this.id = id;
    }

    @ManyToOne( fetch = FetchType.LAZY )
    @JoinColumn( name = "TYPE_ID", nullable = false )
    public WorkType getType( )
    {
       return this.type ;
    }

    public void setType( Type type )
    {
       this.type = type ;
    }

    @ManyToOne( fetch = FetchType.LAZY )
    @JoinColumns( {
        @JoinColumn( name = "RANK", referencedColumnName = "RANK" ),
        @JoinColumn( name = "TYPE_ID", referencedColumnName  = "TYPE_ID" ) } )
    public Priority getRank( )
    {
       return this.rank;
    }

    public void setRank( Rank rank)
    {
       this.rank = rank;
    }
}


When creating a Task, it's Type has to be defined at start. It's Rank however, does not need to be defined until later on (all of the Ranks have been created and exist before assigning a Rank, obviously).

With Task defined as it is above, I receive this error:
Quote:
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: Task column: TYPE_ID (should be mapped with insert="false" update="false")

However, following that suggestion (insertable = false, updatable = false) gives me this:
Quote:
org.hibernate.AnnotationException: Mixing insertable and non insertable columns in a property is not allowed: Taskrank

Following that suggestion further (insertable = false, updatable = false for both JoinColumn) "works," but means that one can no longer set the Rank of a Task.

Adding nullable = false to the TYPE_ID JoinColumns' JoinColumn gives the no mixing of nullables. Making both JoinColumns' JoinColumn nullable = false gives rise to the repeated mapping of TYPE_ID, and is not what is wanted, as the foreign key is nullable.

How do I properly annotate my Task class to support this?

Perhaps the database is modeled wrong, and the composite foreign key should only be a regular foreign key?

Thanks for the help!


Top
 Profile  
 
 Post subject: Re: Composite Foreign Key Overlap Foreign Key
PostPosted: Wed Jun 17, 2009 12:49 pm 
Newbie

Joined: Tue Jun 16, 2009 10:37 am
Posts: 2
Heh, so no one else has run into this issue?

It can easily be resolved by using a surrogate key, but what does one do when that's not an option...

I need the ability to mix insert and update in a single JoinColumns:
Code:
    @ManyToOne( fetch = FetchType.LAZY )
    @JoinColumns( {
        @JoinColumn( name = "RANK", referencedColumnName = "RANK", insertable = true, updatable = true ),
        @JoinColumn( name = "TYPE_ID", referencedColumnName  = "TYPE_ID", insertable = false, updatable = false ) } )


Top
 Profile  
 
 Post subject: Re: Composite Foreign Key Overlap Foreign Key
PostPosted: Thu Jul 02, 2009 5:52 pm 
Newbie

Joined: Thu Jul 02, 2009 5:33 pm
Posts: 1
Anybody solved this issue?


Top
 Profile  
 
 Post subject: Re: Composite Foreign Key Overlap Foreign Key
PostPosted: Fri Nov 19, 2010 11:56 pm 
Beginner
Beginner

Joined: Thu May 20, 2010 12:31 pm
Posts: 28
Reviving and old thread, sry folks... I have just run into this issue. It works *peeeerfectly* with EclipseLink. Hibernate is just inferior on mappings when compared to other Java ORMs. I will switch to EclipseLink in the long run, Hibernate just has too many incomprehensible mapping issues.

What use is an ORM that fails when validating? All lights-out caching and whatever ORM features and algorithms are pointless when the mappings don't work...

Karsten


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.