[ 
https://issues.apache.org/jira/browse/MYFACES-3306?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13105907#comment-13105907
 ] 

Kristian Jörg commented on MYFACES-3306:
----------------------------------------

Agreed, this seems to be the correct way to do it and collectionType is a 
standard attribute in JSF 2.0. 

However if a better way to create error messages could be found it would be 
very useful for other developers. Getting an unexplainable 
LazyInitializationException from deep within MyFaces is VERY confusing and I 
spent a day trying to find the bug in MY code. It is only when I downloaded the 
MyFaces source and debugged it that the problem was found.  And even then I did 
not know how to fix it, hence the bug report. Better error messages would be 
appreciated if possible. 
Maybe catch the exception as SharedRendererUtils tries to populate the 
collection and create an exception message hinting about the collectionType 
attribute? That would certainly have pointed me in the right direction.

> <h:selectManyCheckBox> + JPA with Hibernate creates Hibernate 
> PersistentCollection where it should not. Causes 
> ---------------------------------------------------------------------------------------------------------------
>
>                 Key: MYFACES-3306
>                 URL: https://issues.apache.org/jira/browse/MYFACES-3306
>             Project: MyFaces Core
>          Issue Type: Bug
>          Components: General
>    Affects Versions: 2.1.2
>         Environment: JPA with Hibernate + Spring
>            Reporter: Kristian Jörg
>   Original Estimate: 24h
>  Remaining Estimate: 24h
>
> I have a case where I use a JPA domain object (Mission) that has a standard 
> @ManyToMany relationship to another domain object Departments. The fetch type 
> is set to EAGER on both ends.
> The web page has a <h:selectManyCheckBox> with all possible Departments. The 
> selection is then mapped to the mission object's department set via a custom 
> converter. This all works well.
> When I submit my form I get an org.hibernate.LazyInitializationException in 
> (I believe)  the validation phase. The problem is that MyFaces tries to 
> create a new instance of the specific hibernate class PersistentSet (which 
> should NOT be used outside Hibernate). It then populates this Set with 
> objects and that is when LazyInitializationException hits!
> I have pinpointed the exact location to 
> org.apache.myfaces.shared_impl.renderkit.SharedRendererUtils.java, Line 255:
>                            // try to create the (concrete) collection from 
> modelType 
>                             // or with the class object of componentValue (if 
> any)
>                             try
>                             {
>                                targetForConvertedValues = (componentValue != 
> null
>                                          ? componentValue.getClass()
>                                          : modelType).newInstance();
>                             }
> With I add a check so we are not instanciating Hibernate collections 
> (AbstractPersistentCollection) the program then goes on to create a standard 
> HashSet and all is well. My program works perfectly with this patch:
>                            // try to create the (concrete) collection from 
> modelType 
>                             // or with the class object of componentValue (if 
> any)
>                             try
>                             {
>                                targetForConvertedValues = (componentValue != 
> null && !(componentValue instanceof 
> org.hibernate.collection.AbstractPersistentCollection)
>                                          ? componentValue.getClass()
>                                          : modelType).newInstance();
>                             }
> Of course, adding a dependency on Hibernate in Myfaces is not the correct 
> solution, so another solution has to be invented. 
> My program is solid in itself with regards to the dreaded LIE exception, it 
> is only when I use persisted objects with OneToMany or ManyToMany collections 
> that this problem occurs. 
> MyFaces should never try to instanciate Hibernate collections without having 
> an entity manager session, which you do not.
> I can attach some code examples if needed, but the problem lies not in the 
> rather standard JSF code, but in this specific point in code listed above. 
> Let me know if more examples are needed.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira


Reply via email to