Nimchip
Nimchip

Reputation: 1735

entity inheritance and discriminatorColumns

I am trying to test some DAOs.

I have Entity (and table) named Nomination, it has some properties:

@Entity(name = "Nomination")
@Table(name = "NOMINATION")
@DiscriminatorColumn(name="CATEGORY_CODE", discriminatorType = DiscriminatorType.STRING, length = 1)

public class Nomination extends AuditableEntity {

@Id
@Column(name = "NOM_ID", insertable = true, updatable = true,
        nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;

@Transient
protected NominationType type = null;

@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name = "PERIOD_ID", referencedColumnName = "PERIOD_ID")
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE,
        org.hibernate.annotations.CascadeType.MERGE})
private NomPeriod period = null;

@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name = "CATEGORY_CODE", referencedColumnName = "CATEGORY_CODE")
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE,
        org.hibernate.annotations.CascadeType.MERGE})
private Category category = null;

@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name = "NOMINATOR_ID", referencedColumnName = "EMP_ID")
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE,
        org.hibernate.annotations.CascadeType.MERGE})
private Employee nominator = null;

@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name = "STATUS_ID", referencedColumnName = "STATUS_ID")
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE,
        org.hibernate.annotations.CascadeType.MERGE})
private Status status = null;

//more after this line

Now I have 4 different types of Nomination (idea, team, success, and coworker nominations) and 4 different tables(nomination, team_nom, idea_nom, success_nom). One (NOMINATION) has the common columns. This coincides with my first type of Nomination called CoworkerNom. There is no separate table for this one, since it only needs what's listed in the NOMINATION table. The other 3 Nominations are: TeamNom, SuccessNom, and IdeaNom. These have their own tables as they have additional data, and so they also have their own Entities. For example:

@Entity(name = "IdeaNom")
@Table(name = "IDEA_NOM")
@DiscriminatorValue("I")

public class IdeaNom extends Nomination {

@Column(name = "PURPOSE_INC", insertable = true, updatable = true,
        nullable = true)
private Boolean purposeIncrease;

@Column(name = "PURPOSE_SIMPLIFY", insertable = true, updatable = true,
        nullable = true)
private Boolean purposeSimplify;

//more after this line

This structure applies to SuccessNom and TeamNom.

I am having problems understanding how to do this, but for now I have done some DAOs and am trying to test them out. However I am getting the following error:

Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: 

com.dev.test.data.entity.SuccessNom column: CATEGORY_CODE (should be mapped with insert="false" update="false")
    at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:676)
    at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:698)
    at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:720)
    at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:474)
    at org.hibernate.mapping.SingleTableSubclass.validate(SingleTableSubclass.java:65)
    at org.hibernate.cfg.Configuration.validate(Configuration.java:1362)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1865)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:860)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:779)
    at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1479)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
    ... 54 more

First of all, CATEGORY_CODE is the discriminator... but it should be able to be writable. Hopefully you guys will be able to help me out once more.

Edit: this is my schema

Schema

Upvotes: 3

Views: 5638

Answers (1)

Bozho
Bozho

Reputation: 597342

If you need to map the discriminator column, map it with insert="false" update="false". Hibernate considers it declared once, and it is not writable - it can only be managed by hibernate. Also, it's a bit odd to join on that column.

Upvotes: 8

Related Questions