[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-4112) HibernateProxy enhanced POJOs lose method annotations
Maximiliano Carrizo (JIRA)
noreply at atlassian.com
Wed Mar 17 16:31:31 EDT 2010
[ http://opensource.atlassian.com/projects/hibernate/browse/HHH-4112?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=35868#action_35868 ]
Maximiliano Carrizo commented on HHH-4112:
------------------------------------------
I agree with David. Imagine this scenario:
- You have an entity Person, which has several entities Addresses ( One to Many relation ), using cascade ALL.
- My Person entity has several Hibernate validations ( using annotations, of course ) and my Address entity has several hibernate validation annotations too.
- When I validate Person entity I want Hibernate validates my Person entity AND my Addresses entity TOO ! At the same time.
- I want to validate all these entiyies using ClassValidator.
What happens ? If my Addresses collection is a proxy collection, Hibernate ignores Addresses validation ( because Address proxy doesn't have annotations ) and throws an exception when I execute persist()
How can I make a workaround of this ?
I can manually iterate all relations and get class without proxy and validate them, but I think this is not the idea.
> HibernateProxy enhanced POJOs lose method annotations
> -----------------------------------------------------
>
> Key: HHH-4112
> URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-4112
> Project: Hibernate Core
> Issue Type: Bug
> Components: core
> Affects Versions: 3.2.0.ga
> Reporter: David Green
>
> Using FetchType.LAZY on an association can result in POJO entities being enhanced via JavassistProxyFactory or CGLIBProxyFactory. The resulting class overrides all declared accessors and mutators (get/set methods) in the original POJO. Those overridden methods lose any annotations that were specified on the original POJO.
> The result is that unsuspecting code looking for annotations on POJO accessors won't find any annotations.
> For example, if I have an @Entity POJO called Resident, the following JUnit test will fail for both CGLIBProxyFactory and JavassistProxyFactory:
> {code:Java}
> public void testCGLibProxy() throws HibernateException, SecurityException, NoSuchMethodException {
> doTest(new CGLIBProxyFactory());
> }
> public void testJavassistProxy() throws HibernateException, SecurityException, NoSuchMethodException {
> doTest(new JavassistProxyFactory());
> }
> private void doTest(ProxyFactory proxyFactory) throws NoSuchMethodException {
> HashSet proxyInterfaces = new HashSet();
> proxyInterfaces.add( HibernateProxy.class );
> proxyFactory.postInstantiate("Resident", Resident.class, proxyInterfaces, Resident.class.getDeclaredMethod("getId"), Resident.class.getDeclaredMethod("setId",Long.class), null);
> HibernateProxy hibernateProxy = proxyFactory.getProxy(resident.getId(), session);
> assertTrue(hibernateProxy instanceof Resident);
>
> System.out.println("Hibernate proxy name: "+hibernateProxy.getClass().getName());
>
> assertNotNull(Resident.class.getDeclaredMethod("getMaritalStatus").getAnnotation(Required.class));
> assertNotNull(hibernateProxy.getClass().getDeclaredMethod("getMaritalStatus").getAnnotation(Required.class));
> }
> {code}
--
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.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira
More information about the hibernate-issues
mailing list