Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Mapping Strategy for Class without a table.
PostPosted: Wed Mar 15, 2017 8:14 am 
Newbie

Joined: Mon Mar 06, 2017 9:15 am
Posts: 18
Hi,

Say i have a class like this :

Code:
public class Article{
    private int Id
    private HeaderList headers;
}   


And HeaderList class
Code:
public class HeaderList {
    private String header1;
    private String header2;
    ...(more headers)
}


Imagine i have a table which looks like this (simplified):

ArticleID headerType content
1 header1 "abc"
1 header2 "xyz"

Because not every Article has all types of header, so i planned to write some @Formula to fetch the Strings i need. I thought of mapping the class HeaderList as @Embeddable but not sure its the best strategy, because the HeaderList is not entirely a value object (it still contains attributes that fetched from table).

Anyway i need the Article ID for my @Formula so i need to pass it from Article Entity, is there any good way to do this ?

Any help appreciated.


Top
 Profile  
 
 Post subject: Re: Mapping Strategy for Class without a table.
PostPosted: Wed Mar 15, 2017 3:01 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1386
Making HeaderList an @Embeddable is probably the best way to do it.

_________________
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: Mapping Strategy for Class without a table.
PostPosted: Thu Mar 16, 2017 6:51 am 
Newbie

Joined: Mon Mar 06, 2017 9:15 am
Posts: 18
vlad wrote:
Making HeaderList an @Embeddable is probably the best way to do it.


i thought so as well, but i still need the ID from Parent to map with Formular, is there any way to pass the ID to embeddable classs without making a new attribute in embeddable class ? Thank you.


Top
 Profile  
 
 Post subject: Re: Mapping Strategy for Class without a table.
PostPosted: Thu Mar 16, 2017 8:56 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1386
I don't understand hat you are trying to map. Exotic mappings are usually a code smell, so maybe you can simplify your requirements.

_________________
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: Mapping Strategy for Class without a table.
PostPosted: Thu Mar 16, 2017 9:58 am 
Newbie

Joined: Mon Mar 06, 2017 9:15 am
Posts: 18
vlad wrote:
I don't understand hat you are trying to map. Exotic mappings are usually a code smell, so maybe you can simplify your requirements.


hi, for the Strings atributtes in Embeddable class (HeaderList) i planned to use some SQL Formula to map it like
"SELECT ...FROM HeaderListTable WHERE article_id = .." for this i need the ID of the instance, in which this Headerlist instance is embedded. I hope you understand what i mean :)


Top
 Profile  
 
 Post subject: Re: Mapping Strategy for Class without a table.
PostPosted: Thu Mar 16, 2017 10:21 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1386
The @Formula is for a single attribute, not for an Embeddable. The Embeddable is meant just to groups several columns which already exist on the same table with the owning entity.

What you need to do is write a custom Hibernate type. Make sure you implement CompositeUserType instead of UserType.

_________________
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  [ 6 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.