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!