[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-6911) Write DiscriminatorValue to DiscriminatorColumn when combined with InheritanceType#JOINED

Roberto Ruiz (JIRA) noreply at atlassian.com
Sat Mar 3 15:35:48 EST 2012


    [ https://hibernate.onjira.com/browse/HHH-6911?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=45814#comment-45814 ] 

Roberto Ruiz commented on HHH-6911:
-----------------------------------

There is situation when @DiscriminatorColumn in JOINED inheritance is quite useful. It happens when the discriminator column is also a foreign key to another table (which may contain the description of the subtype)

For example:

@Entity
@Table(name="CONTRACT")
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name="ID_CONTRACT_TYPE", discriminatorType=DiscriminatorType.INTEGER)
public class ContratoJoin {
    
	@Id
	@Column(name="ID_CONTRACT")
	private Long id;
	
	@Column(name="DESCRIPTION")
	private String description;
	
	@ManyToOne
	@JoinColumn(name="ID_CONTRACT_TYPE", insertable=false, updatable=false)
	private ContractType contractType;

	//getters, setters
}

... subclasses with @DiscriminatorValue ...

@Entity
@Table(name="CONTRACT_TYPE")
public class ContractType {
	
	@Id
	@Column(name="ID_CONTRACT_TYPE")
	private Long id;
	
	@Column(name="DESCRIPTION")
	private String description;

	//getters, setters
}

In this case, the value of the column ID_CONTRACT_TYPE should be inserted automatically depending on the subclass, so that the description is retrieved on query.

Currently, in order to do that I must set the ContractType manually. It's not much work, nut it is error prone.

There is also an advantage when using discriminator column. I may look directly into the CONTRACT table, and know the type.

> Write DiscriminatorValue to DiscriminatorColumn when combined with InheritanceType#JOINED
> -----------------------------------------------------------------------------------------
>
>                 Key: HHH-6911
>                 URL: https://hibernate.onjira.com/browse/HHH-6911
>             Project: Hibernate ORM
>          Issue Type: Improvement
>          Components: annotations, metamodel
>            Reporter: Steve Ebersole
>            Priority: Minor
>              Labels: jpa2
>
> The JPA spec states that {{DiscriminatorColumn}} can be used in conjunction with JOINED inheritence.  The only section that seems to deal with this combination is _11.1.10 DiscriminatorColumn Annotation_ where it says
> {quote}
> For the SINGLE_TABLE mapping strategy, and typically also for the JOINED strategy, the persistence
> provider will use a type discriminator column. The DiscriminatorColumn annotation is used to
> define the discriminator column for the SINGLE_TABLE and JOINED inheritance mapping strategies.
> {quote}
> Apparently this is meant to imply that even if providers do not need/use the discriminator for concrete class resolution (reading) they are still expected to write it out to the database.  Dunno, thats just what we have been told :)
> Anyway, like I said, Hibernate itself does not need the discriminator column/value to interpret the concrete classes.  However, in the interest of portability we certainly *could* write it out.  
> Longer term we could *possibly* even use the DiscriminatorColumn/DiscriminatorValue (if present) instead of the SQL CASE statement thing we do now.
> See HHH-4358 for the origins of this request...

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list