-->
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.  [ 9 posts ] 
Author Message
 Post subject: Field-name prefixes
PostPosted: Mon May 30, 2005 8:39 pm 
Newbie

Joined: Mon May 30, 2005 8:24 pm
Posts: 5
Hi,

Does Hibernate 3 allow arbitrary prefixes for field names?

I have an existing business model in which the POJO instance variables all start with an underscore (e.g. private String _aField) and it would be good if I could configure the mappings to handle this, such as by direct specification of the prefix string or of the accessor method-names.

I've experiemented, and searched the documentation and forums, without success, and was wondering if I missed something.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 30, 2005 11:26 pm 
Regular
Regular

Joined: Tue May 24, 2005 10:19 am
Posts: 65
NamingStrategy
http://www.hibernate.org/hib_docs/reference/en/html_single/#configuration-namingstrategy


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 31, 2005 12:42 am 
Newbie

Joined: Mon May 30, 2005 8:24 pm
Posts: 5
NamingStrategy appears to be for database-object names only. Is that correct?

I'm looking for something that will allow me to have an instance variable begin with an underscore (_aField) while using standard getters and setters (getAField, rather than get_aField) to access it. Currently, Hibernate chokes because it's looking for get_aField, which doesn't exist.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 31, 2005 2:04 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
are you using hbm2java or ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 31, 2005 6:34 am 
Newbie

Joined: Mon May 30, 2005 8:24 pm
Posts: 5
No. I've just begun playing around with Hibernate, writing both the POJOs and mapping files by hand, and testing basic DB interaction.

Actually, I've just realised I can write a custom implementation of org.hibernate.property.PropertyAccessor and specify it in the mapping file using the 'default-access' attribute of 'hibernate-mapping'.

Problem solved.


Top
 Profile  
 
 Post subject: Same problem
PostPosted: Fri Jun 03, 2005 12:00 pm 
Beginner
Beginner

Joined: Wed Feb 23, 2005 11:29 am
Posts: 26
I'm seeing the same thing. Hibernate 2.1.7c, Firebird 1.5.2

What version of Hibernate are you using? I was curious if 3.0 fixed this.

Originally, someone was working through our code to fix things that didn't adhere to our coding standards. They prepended the '_' to all the member variable names but didn't touch the .hbm.xml files, and then didn't run any of our integration tests, so a week or so later when we started getting "foreign key constraint violation" all over the place we didn't know why. So I started backing out changes until it worked and voila it was the underscores.

Today I tried adding the underscores in the .hbm.xml files and got:

Code:
net.sf.hibernate.PropertyNotFoundException: Could not find a setter for property _id in class com.lexmark.workflow.framework.domain.printer.ApplicationsFeature


So far I've tried:

- Change the getter/setter names to get_id()/set_id(). It can't be get_Id(), because apparenly the 'upcase the first letter' applies to the underscore rather than the 'i'. Ugly and the changes ripple, but it seemed to be working to a point. I didn't pursue it because of the inherent ugliness. It sounds like you have a large code base and you don't want to be going through it all.

- Specify access="field" for all of the properties in the mapping flies, so that Hibernate doesn't use the getters/setters at all. I suppose there could be cases where some synthesis or validation of the field should be happening and you would lose that. I tried it anyway and it broke on a joined-subclass - it said it couldn't find the _id in the subclass. I guess somehow when it calls the getter it picks up the base-class getter, but in the case of field access its not.

- Page 80 of _Hibernate in Action_ says you can write your own custom PropertyAccessor, but I haven't tried that one. Not sure what package it would have to go in, etc...

- I think for us the simplest thing is to change our coding standards, since we're early in our project. Before I go there I'm going to try creating a PropertyAccessor, though.

-Jeff


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 03, 2005 7:15 pm 
Newbie

Joined: Mon May 30, 2005 8:24 pm
Posts: 5
I'm using Hibernate 3.0

I also rejected the possible solutions you mentioned, for mostly the same reasons. Although, I like using the underscore prefix for instance variables: it makes it obvious which variables belong to the method and which to the object. It also means I can use logical names for method parameters, instead of creating new names which don't conflict, or of using 'this'.

Creating a PropertyAccessor is straightforward and works well. The easiest way is to simply copy org.hibernate.property.BasicPropertyAccessor and add a couple of lines to 'getterMethod' and 'setterMethod' to handle the prefix.


Top
 Profile  
 
 Post subject: Where to put it...
PostPosted: Fri Jun 03, 2005 7:49 pm 
Beginner
Beginner

Joined: Wed Feb 23, 2005 11:29 am
Posts: 26
Thanks for the reply. How does Hibernate know about a custom PropertyAccessor? Would the PropertyAccessor I create have to remain in the net.sf.hibernate.... package (still on version 2 for now), or is there some way to 'register' the class with Hibernate?

-Jeff


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 03, 2005 8:22 pm 
Newbie

Joined: Mon May 30, 2005 8:24 pm
Posts: 5
You tell Hibernate about the accessor by specifying the accessor's class name via the mapping file/s, using the 'default-access' attribute of the 'hibernate-mapping' element. For example:
Quote:
<hibernate-mapping default-access="com.whatever.hibernate.UnderscorePrefixedPropertyAccessor">
...
</hibernate-mapping>


You can put the new accessor class anywhere, as long as it ends up in the classpath (from Hibernate's point of view). I put mine with the rest of my project's code, in a 'hibernate' package with all the other Hibernate-specific stuff.


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