Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Does Hibernate support custom class per row?
PostPosted: Fri Jun 02, 2017 2:41 pm 
Newbie

Joined: Fri May 05, 2017 12:09 pm
Posts: 7
Hi,

Is there a way to override which class Hibernate uses for each row of a table? So a select could return a list of objects of different types. For example, if I have a table like:

Code:
create table things (
   id integer,
   name text,
   java_class text
)


There is a Java class `Thing` (which has properties for `id` and `name`), and the `java_class` column can name a subclass of `Thing`.

Rob


Top
 Profile  
 
 Post subject: Re: Custom class per row?
PostPosted: Fri Jun 02, 2017 2:47 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1229
Sure, it does support such a feature.

Just use SINGLE_TABLE inheritance as explained in this article. The java_class column is the discriminator type which you can customize using the @DiscriminatorColumn.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
 Post subject: Re: Custom class per row?
PostPosted: Fri Jun 02, 2017 3:05 pm 
Newbie

Joined: Fri May 05, 2017 12:09 pm
Posts: 7
vlad wrote:

Just use SINGLE_TABLE inheritance as explained in this article. The java_class column is the discriminator type which you can customize using the @DiscriminatorColumn.


I don't see any mention of inheritance or discriminators in that article. Is that the right link?

Rob


Top
 Profile  
 
 Post subject: Re: Does Hibernate support custom class per row?
PostPosted: Fri Jun 02, 2017 3:33 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1229
By default, the discriminator column is called DTYPE. If you provide a @DiscriminatorColumn annotation, you can override the column name. The JavaDoc is very straightforward about it.

For more details, subscribe to my blog newsletter and I'll write about this topic in the following two weeks.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.