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.  [ 10 posts ] 
Author Message
 Post subject: Unable to access property
PostPosted: Tue Dec 28, 2004 10:57 pm 
Beginner
Beginner

Joined: Thu Oct 14, 2004 10:53 pm
Posts: 45
In my PartialDate class, I have a field called precision, which is of type PrecisionCode.

It has the usual get/set methods, as below
public PrecisionCode getPrecision() {
return precision;
}

public void setPrecision(PrecisionCode precision) {
this.precision = precision;
}

I have two get/set methods called getPrecisionChar/setPrecisionChar as well, implemented below

protected char getPrecisionChar() {
return precision.getPrecision();
}

protected void setPrecisionChar(char precision) {
this.precision = PrecisionCode.valueOf(precision);
}

I don't have a precisionChar field (e.g. private variable), but I want to persist the PrecisionChar into the database. The mapping file is below. However I get a PropertyNotFound exception also shown below. It seems to be complaining that the field precisionChar cannot be found, which is fair because I never declared it. But getPrecisionChar and setPrecisionChar work from the precisionCode field. In hibernate, how to I get precisionChar to be persisted to the database?

[b]Hibernate version: 2.1.7[/b]

[b]Mapping documents:[/b]

<hibernate-mapping package="test" >
<class name="PersistableDate" table="PersistableDate">
<id name="id" type="int">
<generator class="increment"/>
</id>
<property name="name" />

<component name="testDate" class="PartialDate">
<property name="precisionChar" column="testDatePrec"/>
</component>

</class>
</hibernate-mapping>


[b]Code between sessionFactory.openSession() and session.close():[/b]

[b]Full stack trace of any exception that occurs:[/b]
[ERROR] (hibernate.cfg.Configuration ) - Could not compile the mapping document
net.sf.hibernate.MappingException: Problem trying to set property type by reflection
at net.sf.hibernate.mapping.SimpleValue.setTypeByReflection(SimpleValue.java:183)
at net.sf.hibernate.cfg.Binder.createProperty(Binder.java:1069)
at net.sf.hibernate.cfg.Binder.bindComponent(Binder.java:870)
at net.sf.hibernate.cfg.Binder.propertiesFromXML(Binder.java:1053)
at net.sf.hibernate.cfg.Binder.bindRootClass(Binder.java:363)
at net.sf.hibernate.cfg.Binder.bindRoot(Binder.java:1257)
at net.sf.hibernate.cfg.Configuration.add(Configuration.java:252)
at net.sf.hibernate.cfg.Configuration.addInputStream(Configuration.java:288)
at net.sf.hibernate.cfg.Configuration.addResource(Configuration.java:336)
at net.sf.hibernate.cfg.Configuration.doConfigure(Configuration.java:1018)
at net.sf.hibernate.cfg.Configuration.doConfigure(Configuration.java:974)
at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:902)
at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:888)
at test.TestPersister.main(TestPersister.java:35)
Caused by:
net.sf.hibernate.PropertyNotFoundException: field not found: precisionChar
at net.sf.hibernate.property.DirectPropertyAccessor.getField(DirectPropertyAccessor.java:74)
at net.sf.hibernate.property.DirectPropertyAccessor.getField(DirectPropertyAccessor.java:80)
at net.sf.hibernate.property.DirectPropertyAccessor.getField(DirectPropertyAccessor.java:80)
at net.sf.hibernate.property.DirectPropertyAccessor.getGetter(DirectPropertyAccessor.java:88)
at net.sf.hibernate.util.ReflectHelper.getter(ReflectHelper.java:81)
at net.sf.hibernate.util.ReflectHelper.reflectedPropertyType(ReflectHelper.java:86)
at net.sf.hibernate.mapping.SimpleValue.setTypeByReflection(SimpleValue.java:172)
... 13 more
net.sf.hibernate.MappingException: Error reading resource: PersistableDate.hbm.xml
at net.sf.hibernate.cfg.Configuration.addResource(Configuration.java:339)
at net.sf.hibernate.cfg.Configuration.doConfigure(Configuration.java:1018)
at net.sf.hibernate.cfg.Configuration.doConfigure(Configuration.java:974)
at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:902)
at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:888)
at test.TestPersister.main(TestPersister.java:35)
Caused by: net.sf.hibernate.MappingException: Problem trying to set property type by reflection
at net.sf.hibernate.mapping.SimpleValue.setTypeByReflection(SimpleValue.java:183)
at net.sf.hibernate.cfg.Binder.createProperty(Binder.java:1069)
at net.sf.hibernate.cfg.Binder.bindComponent(Binder.java:870)
at net.sf.hibernate.cfg.Binder.propertiesFromXML(Binder.java:1053)
at net.sf.hibernate.cfg.Binder.bindRootClass(Binder.java:363)
at net.sf.hibernate.cfg.Binder.bindRoot(Binder.java:1257)
at net.sf.hibernate.cfg.Configuration.add(Configuration.java:252)
at net.sf.hibernate.cfg.Configuration.addInputStream(Configuration.java:288)
at net.sf.hibernate.cfg.Configuration.addResource(Configuration.java:336)
... 5 more
Caused by: net.sf.hibernate.PropertyNotFoundException: field not found: precisionChar
at net.sf.hibernate.property.DirectPropertyAccessor.getField(DirectPropertyAccessor.java:74)
at net.sf.hibernate.property.DirectPropertyAccessor.getField(DirectPropertyAccessor.java:80)
at net.sf.hibernate.property.DirectPropertyAccessor.getField(DirectPropertyAccessor.java:80)
at net.sf.hibernate.property.DirectPropertyAccessor.getGetter(DirectPropertyAccessor.java:88)
at net.sf.hibernate.util.ReflectHelper.getter(ReflectHelper.java:81)
at net.sf.hibernate.util.ReflectHelper.reflectedPropertyType(ReflectHelper.java:86)
at net.sf.hibernate.mapping.SimpleValue.setTypeByReflection(SimpleValue.java:172)
... 13 more

[b]Name and version of the database you are using:[/b]
SQL Server 2000


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 29, 2004 3:58 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
according to the stacktrace you have activated direct field access (access="field") and thus it will check for a field instead of a property (methods).

Don't set access to something and it should work.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 29, 2004 5:49 am 
Beginner
Beginner

Joined: Thu Oct 14, 2004 10:53 pm
Posts: 45
Thanks for your answer. However I have not set "access" to equal anything here. The default is property and so it uses the getter/setter methods to access fields, but still does not work for some reason.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 29, 2004 6:20 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
what is the type of id in your class int or Integer?

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 29, 2004 6:33 am 
Beginner
Beginner

Joined: Thu Oct 14, 2004 10:53 pm
Posts: 45
It is type Integer. I'm sorry, I cannot see how this relates to the original question. I must be doing something so stupid so any direction is greatly appreciated


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 29, 2004 6:36 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
looking at the hibernate code (which you also can do), I see that if it fails to look up the property accessor it retries with field access (actually a surprise to me too ,)

Do you run under special security permissions that might prevent hibernate from getting to the protected property ? What happens if you make the get-sets public does it work then ?

try to step in with a debugger and see what exception that is thrown at line 81 of ReflectHelper since it tries to use field access.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 29, 2004 6:37 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
change
Code:
<id name="id" type="int">
<generator class="increment"/>
</id>


to
Code:
<id name="id">
<generator class="increment"/>
</id>

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 29, 2004 6:43 am 
Beginner
Beginner

Joined: Thu Oct 14, 2004 10:53 pm
Posts: 45
[quote="max"]looking at the hibernate code (which you also can do), I see that if it fails to look up the property accessor it retries with field access (actually a surprise to me too ,)

Do you run under special security permissions that might prevent hibernate from getting to the protected property ? What happens if you make the get-sets public does it work then ?

try to step in with a debugger and see what exception that is thrown at line 81 of ReflectHelper since it tries to use field access.[/quote]

Changing to public seems to work, however I'd prefer the methods to be protected as they are only accessed in the subclass. I have tested that Hibernate functions perfectly with protected methods, but I am baffled that it is not the case here. Is this something to report to JIRA? I will step through with the debugger later and report back my findings.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 29, 2004 6:47 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
step in with the debugger before reporting this to jira - this stuff does work (as you have seen in other cases too ,)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 29, 2004 5:43 pm 
Beginner
Beginner

Joined: Thu Oct 14, 2004 10:53 pm
Posts: 45
We've found the problem.

Apparently the code obfuscator that we used changed the method names so that hibernate could not find them. When the method name is public, the obfuscator didn't touch them, however when they were protected they were modified, so it confused hibernate.


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