Hibernate @DiscriminatorColumn and @DiscriminatorValue issue
I have this parent class:
@Entity
@Inheritance(strategy = Inher开发者_如何学JAVAitanceType.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!
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
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.
精彩评论