You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@myfaces.apache.org by "Kristian Jörg (JIRA)" <de...@myfaces.apache.org> on 2011/09/14 15:01:09 UTC

[jira] [Created] (MYFACES-3306) + JPA with Hibernate creates Hibernate PersistentCollection where it should not. Causes

<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


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

       

[jira] [Commented] (MYFACES-3306) + JPA with Hibernate creates Hibernate PersistentCollection where it should not. Causes

Posted by "Kristian Jörg (JIRA)" <de...@myfaces.apache.org>.
    [ https://issues.apache.org/jira/browse/MYFACES-3306?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13104808#comment-13104808 ] 

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

Yes, that does indeed work. Is this documented as the recommended way to handle persistent objects? Or is this still a bug that should be fixed?
I'll use this as a workaround for now though...

Thanx for the info!

> <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

       

[jira] [Commented] (MYFACES-3306) + JPA with Hibernate creates Hibernate PersistentCollection where it should not. Causes

Posted by "Kristian Jörg (JIRA)" <de...@myfaces.apache.org>.
    [ 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

       

[jira] [Commented] (MYFACES-3306) + JPA with Hibernate creates Hibernate PersistentCollection where it should not. Causes

Posted by "Michael Kurz (JIRA)" <de...@myfaces.apache.org>.
    [ https://issues.apache.org/jira/browse/MYFACES-3306?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13105122#comment-13105122 ] 

Michael Kurz commented on MYFACES-3306:
---------------------------------------

So, I quickly checked it again. You can also add it directly as attribute collectionType="java.util.HashSet" to h:selectManyCheckBox. I think this was added in JSF 2.0.

IMO this is the recommended way to do it. If there is no objection from the community I would say this issue can be closed.

> <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

       

[jira] [Resolved] (MYFACES-3306) + JPA with Hibernate creates Hibernate PersistentCollection where it should not. Causes

Posted by "Jakob Korherr (JIRA)" <de...@myfaces.apache.org>.
     [ https://issues.apache.org/jira/browse/MYFACES-3306?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Jakob Korherr resolved MYFACES-3306.
------------------------------------

    Resolution: Not A Problem

> <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

       

[jira] [Commented] (MYFACES-3306) + JPA with Hibernate creates Hibernate PersistentCollection where it should not. Causes

Posted by "Michael Kurz (JIRA)" <de...@myfaces.apache.org>.
    [ https://issues.apache.org/jira/browse/MYFACES-3306?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13104566#comment-13104566 ] 

Michael Kurz commented on MYFACES-3306:
---------------------------------------

There might be a simpler solution. Try to add

<f:attribute name="collectionType" value="java.util.HashSet"/>

as a child to <h:selectManyCheckBox>.

> <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