-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Mapping constant and application generated columns
PostPosted: Thu Nov 23, 2006 3:28 pm 
Newbie

Joined: Mon Aug 02, 2004 1:21 pm
Posts: 9
Hibernate version: 3.2

Hi. I have an invoice table and an invoice-detail table. I have 2 problems:

1) The invoice table has a field called total that is simply the sum of the total of each detail. In my application objects I don't want to have this value, since I can calculate it and avoid inconsistency problems. However, when I insert a new invoice, I have to write this value to the database. I have a getTotal method, but hibernate does not consider it a valid property since it does not have the corresponding setTotal method. Is there a way to map the column and make hibernate get the total from the getTotal method creating this method on the object?

2) There are some other columns in the database I don't want to map. However this columns are declared not null, so I would like to simply fill them with '0'. Is there a way to make hibernate insert a constant to this columns without creating a property for them in the object?

Thanks

_________________
Camilo A.


Top
 Profile  
 
 Post subject: Re: Mapping constant and application generated columns
PostPosted: Thu Nov 23, 2006 7:22 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
camilosaurio wrote:
1) The invoice table has a field called total that is simply the sum of the total of each detail. In my application objects I don't want to have this value, since I can calculate it and avoid inconsistency problems. However, when I insert a new invoice, I have to write this value to the database. I have a getTotal method, but hibernate does not consider it a valid property since it does not have the corresponding setTotal method. Is there a way to map the column and make hibernate get the total from the getTotal method creating this method on the object?


I think you could have a look at the formula tag in the mapping. If hibernate is requesting the setTotal but you do not want to provide it publicly, you could define it with protected visibility, Hibernate will still be able to use it. In general, put protected everything you can that's specific to hibernate, but that you would prefer not exposing as a public API.

camilosaurio wrote:
2) There are some other columns in the database I don't want to map. However this columns are declared not null, so I would like to simply fill them with '0'. Is there a way to make hibernate insert a constant to this columns without creating a property for them in the object?


I guess a formula could do the trick here.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 24, 2006 10:19 am 
Newbie

Joined: Mon Aug 02, 2004 1:21 pm
Posts: 9
Thanks for your reply.

At first I thought that formula attribute could help me, but I understand that it must be put on a <property> tag and therefore, that requires a name attribute. This means that there must be necessarily a property in the object that maps to the column, and that is what I was trying to avoid.

So far, I solved the problem this way:

For problem 1: I declared a private set that has an empty body. This makes hibernate to recognize the javabean property.

For problem 2: I declared the unused fields as private fields with the constant value and mapped them with field access.

However, I think that this is not an elegant solution. I was hoping that I could solve the issue without touching the objects.

Regards,

_________________
Camilo A.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.