Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » JPA spec: persisting discriminator values not mandatory?
JPA spec: persisting discriminator values not mandatory? [message #768128] Mon, 19 December 2011 16:11 Go to next message
Karsten Wutzke is currently offline Karsten WutzkeFriend
Messages: 112
Registered: July 2009
Senior Member
Hello all,

there's currently an issue in Hibernate 4 which I consider a bug. It is here:

https://hibernate.onjira.com/browse/HHH-4358

You don't necessarily have to read everything up front. The short story is that Hibernate doesn't persist discriminator values of entities involved in inheritance with an @DiscriminatorColumn.

I have a webapp to manage a number of documents (upload, delete, ...), which got mapped as this base class:

@Entity
@Table(name = "Documents")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "discriminator", discriminatorType = DiscriminatorType.STRING)
public abstract class Document implements Serializable
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column
    protected Integer id;

    @Column(name = "file_name")
    protected String fileName;

    ...
}

Here's a concrete sub class:

@Entity
@Table(name = "ExternalDocuments")
@DiscriminatorValue(value = "external")
public class ExternalDocument extends SystemDocument
{
    ... // no additional fields
}

Any new ExternalDocument instance gets the column's default string value in the database, here 'official' (or the first value in the list of a MySQL ENUM('official', 'reference', 'curriculumvitae', 'individual', 'external')) - which is incorrect. (AFAIK EclipseLink doesn't do this!)

I cannot find anything in the JPA 2 spec about whether discriminator values have to be persisted or not. Ultimately the person I discuss the issue with in the bug report might be correct that this Hibernate JPA issue doesn't need to be fixed.

Is this true or is it just missing information in the JPA spec? If it is present, where can I find it?

Thanks
Karsten

[Updated on: Tue, 20 December 2011 12:51]

Report message to a moderator

Re: JPA spec: persisting discriminator values not mandatory? [message #768267 is a reply to message #768128] Mon, 19 December 2011 20:23 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

Can't see how inheritance can possibly work if you don't persist the DiscriminatorValue. I assume the Spec covers it.

I think Hibernate just doesn't support this with JOINED inheritance, and it does seem like a bug, and a spec violation.


James : Wiki : Book : Blog : Twitter
Re: JPA spec: persisting discriminator values not mandatory? [message #768271 is a reply to message #768128] Mon, 19 December 2011 20:23 Go to previous messageGo to next message
James is currently offline JamesFriend
Messages: 272
Registered: July 2009
Senior Member
Can't see how inheritance can possibly work if you don't persist the DiscriminatorValue. I assume the Spec covers it.

I think Hibernate just doesn't support this with JOINED inheritance, and it does seem like a bug, and a spec violation.

--
James : http://wiki.eclipse.org/EclipseLink : http://en.wikibooks.org/wiki/Java_Persistence : http://java-persistence-performance.blogspot.com/
Re: JPA spec: persisting discriminator values not mandatory? [message #768617 is a reply to message #768271] Tue, 20 December 2011 13:22 Go to previous messageGo to next message
Karsten Wutzke is currently offline Karsten WutzkeFriend
Messages: 112
Registered: July 2009
Senior Member
Here's what I received from a member of the JPA expert group:

Quote:
The spec does not require an implementation to use discriminator columns to implement JOINED inheritance, however, the assumption is that if @DiscriminatorColumn is specified then it would be used, i.e. the values would be written out. We do not explicitly state that if a @DiscriminatorColumn is specified in the code it must be used, just like we don't explicitly state that if a @Column or @JoinColumn is specified the values must be stored in the table, but there is only so much that we can or should specify. At the lowest level, certain laws of physics and reason are just assumed.

Astonishingly Hibernate still works without persisting discriminator values. There has been a Hibernate bug report for this before, but it was rejected:

https://hibernate.onjira.com/browse/ANN-140

Note the first comment (rant) by Gavin King there:

Quote:
EJB3 does NOT require use of discriminators with JOINED mapping strategies. It is allowed for inferior implementations of the JOINED mapping strategy which require a discriminator. Hibernate does not need a discriminator because Hibernate is better than these other inferior implementations.

I fail to understand why not persisting discriminators isn't considered a bug and hasn't been fixed until today (Hibernate 4 Final) honestly because it leads to inconsistent data in the DB (yuck).

Note: This is surely getting OT in this forum, but JBoss AS 7 users are still kinda stuck using Hibernate as (I think!) there's still no 100% support for EclipseLink for that server.

I'd be glad if anyone could comment on this.

Thanks
Karsten
Re: JPA spec: persisting discriminator values not mandatory? [message #769125 is a reply to message #768617] Wed, 21 December 2011 12:54 Go to previous messageGo to next message
Karsten Wutzke is currently offline Karsten WutzkeFriend
Messages: 112
Registered: July 2009
Senior Member
The answer is here:

http://stackoverflow.com/questions/7988756/hibernate-4-persisting-inheritancetype-joined-discriminator-column-values

The RFE is here:

https://hibernate.onjira.com/browse/HHH-6911

Thanks for helping on this
Karsten
Re: JPA spec: persisting discriminator values not mandatory? [message #769126 is a reply to message #768617] Wed, 21 December 2011 12:54 Go to previous messageGo to next message
Karsten Wutzke is currently offline Karsten WutzkeFriend
Messages: 112
Registered: July 2009
Senior Member
The answer is here:

http://stackoverflow.com/questions/7988756/hibernate-4-persisting-inheritancetype-joined-discriminator-column-values

The RFE is here:

https://hibernate.onjira.com/browse/HHH-6911

Thanks for helping on this
Karsten
Re: JPA spec: persisting discriminator values not mandatory? [message #769754 is a reply to message #768617] Thu, 22 December 2011 16:29 Go to previous message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

EclipseLink should work in any Java server (perhaps without weaving, you can use static weaving).

Have you had issues with using EclipseLink on JBoss?

If you cannot get Hibernate to work perhaps switch to EclipseLink, or maybe use Glassfish if you are having JBoss issues.


James : Wiki : Book : Blog : Twitter
Re: JPA spec: persisting discriminator values not mandatory? [message #769757 is a reply to message #768617] Thu, 22 December 2011 16:29 Go to previous message
James is currently offline JamesFriend
Messages: 272
Registered: July 2009
Senior Member
EclipseLink should work in any Java server (perhaps without weaving, you can use static weaving).

Have you had issues with using EclipseLink on JBoss?

If you cannot get Hibernate to work perhaps switch to EclipseLink, or maybe use Glassfish if you are having JBoss issues.

--
James : http://wiki.eclipse.org/EclipseLink : http://en.wikibooks.org/wiki/Java_Persistence : http://java-persistence-performance.blogspot.com/
Previous Topic:Eclipselink force update
Next Topic:Where to define schema?
Goto Forum:
  


Current Time: Sat Dec 20 05:56:10 GMT 2014

Powered by FUDForum. Page generated in 0.02137 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software