-->
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.  [ 3 posts ] 
Author Message
 Post subject: DiscriminatorColumn with discriminatorType to a class
PostPosted: Tue May 30, 2006 1:40 pm 
Newbie

Joined: Fri May 12, 2006 9:48 am
Posts: 14
Location: Toulouse
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


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 31, 2006 12:01 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Make the many to one read only
Code:
@ManyToOne
@JoinColumn (name="filetype_id", insertable=false, updatable=false)
private FileType fileType;
.....
}

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 31, 2006 4:03 am 
Newbie

Joined: Fri May 12, 2006 9:48 am
Posts: 14
Location: Toulouse
I thought about that (as the error message suggests), but will an insert of an object of type RawDataFile work ?

In other words, since the attribute fileType is set to read-only, will Hibernate be able to fill in the column filetype_id in table FILE ?

What about the class property in class DiscriminatorType ?
Does it work and if yes: how ?


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