Hibernate version: hibernate-3.2 ; hibernate-annotations-3.2.0.CR1 ; hibernate-entitymanager-3.2.0.CR1
Hi,
Here is my problem.
I created a class named File (abstract). I have 2 derived classes (RawDataFile and TrashFile).
To separate the 2 types, I created a separate class named FileType which has 2 columns (id and name). This table has 2 values: (T, 'Trash') and (R, 'RawData').
Code:
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="filetype_id", discriminatorType=DiscriminatorType.STRING)
public class File {
...
}
@Entity
@DiscriminatorValue("R")
@SecondaryTable(name="rawdatafile")
public class RawDataFile extends File {
...
}
This works.
But now there is no relation (FK) with the table FileType.
So I see 3 possible ways to solve this:
1. Add the property to class File:
(getters/setters removed to simplify)
Code:
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="filetype_id", discriminatorType=DiscriminatorType.STRING)
public class File {
...
@ManyToOne
@JoinColumn (name="filetype_id")
private FileType fileType;
.....
}
Now Hibernate sais:
Code:
Exception in thread "main" org.hibernate.MappingException: Repeated column in mapping for entity:<path>.RawDataFile column: filetype_id (should be mapped with insert="false" update="false")
This sounds pretty logical... First I tell him the dicriminator column is a String and next I make him refer to a class.
2.
So I can try to change the discriminatorType to the class property.
The problem there is that I can't make the code to compile. I don't find the right syntax to do so (all examples are using DiscriminatorType.STRING or DiscriminatorType.INTEGER)
My inline code helper in Eclipse shows me the class property so this might be possible....but how?
Code:
@DiscriminatorColumn(name="filetype_id", discriminatorType=DiscriminatorType.class )
3.
Not using the FileType class and FILETYPE table. But then I need to use a Check constraint for filetype_id in the table FILE that only value R and T are allowed. Personally, i don't like this way of working. If I want to use a third type of File, I need to recreate the constraint. With the use of the FILETYPE table, I just need to add a line to the table.
Is there a way by the way of adding a check constraint with annotations (like there is one for unique constraints) ?
Can anyone help me further (or point me to some doc I overlooked) ?
TIA,
Whiteman