tags:

views:

243

answers:

3

I have this parent class:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "BSEntity")
@DiscriminatorColumn(name = "resourceType", discriminatorType = DiscriminatorType.STRING, length = 32)
public abstract class BaseEntity {

and the subclass

@Entity
@Table(name = "BSCategory")
@DiscriminatorValue("Category")
public class CategoryEntity extends BaseEntity {

But when I run the program, I get the following error:

2010-06-03 10:13:54,222 [main] WARN  (org.hibernate.util.JDBCExceptionReporter:100) - SQL Error: 1364, SQLState: HY000
2010-06-03 10:13:54,222 [main] ERROR (org.hibernate.util.JDBCExceptionReporter:101) - Field 'resourceType' doesn't have a default value

Any thoughts?

Updated: Database is MySQL. I have also changed inheritance strategy to JOINED instead of SINGLE_TABLE. Help

Another Update: I saw the following posting somewhere and it looks very interesting: http://opensource.atlassian.com/projects/hibernate/browse/ANN-140

New Update: If I were to use SecondaryTable approach, how would I proceed then?

Final Update: turns out the @Discriminator thing does not work well with hibernate. I used the @SecondaryTable approach and that took care of this issue for me. Thanks everyone for helping me out!

A: 

Make your database column have e default value.

If this isn't suitable, use hbm2ddl.auto (set to update) so that hibernate creates (updates) the structure it requires.

UPDATE: Go to your table and manually set a value to all existing rows in this column. Then restart your application.

Bozho
well, what if I really don't want to have this column default to a certain value? the value should really change.
but i don't want to use hbm2ddl.auto=create. It will completely drop the table (that already contains data in there) and re-create a new table with different structure. This code works fine with HSQLDB, but not for MySQL. why?
no, it won't drop it. It will only update it.
Bozho
When I set it to update, I get the same error I posted at the very begining of this post. I actually had it set initially with hbm2ddl.auto=update.
see my update..
Bozho
I can manually add a value in the column named resourceType. But I have thousands of records!!! and each row can have different value for this column :(
well, you must decide what your _existing_ records should hold. Hibernate can't decide for you.
Bozho
A: 

I used a different inheritance strategy such as TABLE_PER_CLASS and still not working for me.

add this as update to the original question, not as an anwer.
Bozho
A: 

I'm not a hibernate guru but I'm wondering if you need the @DiscriminatorColumn and @DiscriminatorValue at all in your example. You already have the base class in it's own table and I assume all of the subclasses are also in their own tables?

If you check out the following link, you can see that InheritanceType.JOINED is not used with the discriminator stuff. The discriminator annotations are used when you are using InheritanceType.SINGLE_TABLE.

http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#d0e1168

Gray
But this strategy worked with HSQLDB. why not for MySQL? When I changed the attribute to default to something, then it worked fine. But I don't want that. I want the value to be dynamically populated for this field.
I still don't understand _why_ you need the Discriminator stuff at all. That it worked with HSQLDB and not MySQL shows to me that you are doing something wrong. Try removing the `@DiscriminatorColumn` and `Value` altogether. Does that work?
Gray
The reason why I'm using Discriminator is to help me automatically fill in data into the resourceType field (which, by the way, is located in the BaseEntity table) whenever I insert data into the CategoryEntity table. Dicriminator is the only way I can come up with. And it seems to do what I want when I used HSQLDB. However, I'm trying to achieve the same thing using MySQL. How?
I removed @DiscriminatorColumn and Value altogether, and that didn't work either.
You probably need to add a `@PrimaryKeyJoinColumn(name="xxx")` which tells hibernate which field in the subclass `CategoryEntity` corresponds to the id field in `BaseEntity`.
Gray