[
http://opensource.atlassian.com/projects/hibernate/browse/HHH-6215?page=c...
]
Bogdan Butnaru edited comment on HHH-6215 at 6/5/11 10:49 AM:
--------------------------------------------------------------
Sanne, I don’t want them to be mutable from within Java code, I just want Hibernate to be
able to set the fields for the objects when I restore them from the database. It’s just
simpler to access the properties directly rather than having to add a getter. I find that
this way is a bit more self-documenting: an property with a getter but no setter suggests
that _you_ can’t modify its value (though something inside the class might), while a
{{public final}} field makes it clear that it shouldn’t _ever_ change its value.
However, note that it’s not a *new* behavior I’m asking for: Hibernate can already set
{{final}} fields, as long as they’re not declared {{public}}. Also, various documentation
I’ve encountered mentions that such fields can be set, it just seems that nobody tried it
with public fields yet.
was (Author: bogdanb):
Sanne, I don’t want them to be mutable from within Java code, I just want Hibernate to
be able to set the fields for the objects when I restore them from the database. It’s just
simpler to access the properties directly rather than having to add a getter.
However, note that it’s not a *new* behavior I’m asking for: Hibernate can already set
{{final}} fields, as long as they’re not declared {{public}}. Also, various documentation
I’ve encountered mentions that such fields can be set, it just seems that nobody tried it
with public fields yet.
final fields can't be set if they're public
-------------------------------------------
Key: HHH-6215
URL:
http://opensource.atlassian.com/projects/hibernate/browse/HHH-6215
Project: Hibernate Core
Issue Type: Bug
Components: core
Affects Versions: 3.6.4
Environment: Hibernate 3.6.4
Reporter: Bogdan Butnaru
As far as I can tell, Hibernate 3.6.4 can set a {{final}} field, _except_ if both the
field and the class it belongs to are declared {{public}}. In other words,
{noformat}
public class Test{
@Column(name = "field")
private final String field = null;
}
{noformat}
will work, as will making the class non-{{public}} and the field {{public}}, but
{noformat}
public class Test{
@Column(name = "field")
public final String field = null;
}
{noformat}
will not. The latter causes an {{org.hibernate.PropertyAccessException}} to be thrown
when a {{Test}} object is initialized from the database (caused by a
{{java.lang.IllegalAccessException}}).
(The message I get is _"could not set a field value by reflection setter of
one.of.my.Classes.field at
org.hibernate.property.DirectPropertyAccessor$DirectSetter.set(DirectPropertyAccessor.java:151)"_)
I looked a bit around the sources for 3.6.4 (which I downloaded earlier today) and did a
little debugging, and as far as I can tell the problem is in
{{DirectPropertyAccessor.getField(Class, String)}}; Specifically, line 176 reads
{noformat}
if ( !ReflectHelper.isPublic(clazz, field) ) field.setAccessible(true);
{noformat}
that is, {{setAccessible}} is not called for {{public}} fields even if they are
{{final}}. I don't know the code enough to be sure, but I believe the test should
check for {{final}} too.
There is an identical line in the next method (same name but with two {{Class}}
arguments, line 191); I suspect that causes the same problem, but I didn't hit that
code in my case (AFAIK because I don't have inherited fields).
I'm sorry for not uploading a test case, I don't quite get how to check this, but
I hope I pointed close enough to the problem.
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
http://opensource.atlassian.com/projects/hibernate/secure/Administrators....
-
For more information on JIRA, see:
http://www.atlassian.com/software/jira