-->
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.  [ 7 posts ] 
Author Message
 Post subject: Seam 3 integration with Hibernate
PostPosted: Wed May 08, 2013 5:01 pm 
Newbie

Joined: Mon May 17, 2010 10:24 am
Posts: 5
Hi,
We are working on migrating our project from Seam 2, Hibernate 3, and JSf 1.2 running on JBoss 5 to Seam 3 (or deltaspike once its ready), Hibernate 4, and JSF 2 running on jboss 7. We have run into an issue we cannot solve related to LIE (LazyInitializationException) with hibernate session.

Stack trace is below. We have our persistence.xml under our core/ejb project ~/src/main/resources/meta-inf, also shown below. We have tried with CDI/Seam 3 pojo and EJBs. We have also tried with @Inject or @PersistentContext **with extended option) with no luck. What we do not have is some CDI producer for EntityManager or EntityManagerFactory. It seems if we try to add something like that like the seam 3 documentation suggests we get common WELD duplicate producer exceptions on app server startup
http://docs.jboss.org/seam/3/latest/reference/en-US/html_single/#d0e4926

What we do know...
We have a transaction and do a log with entityMangaer.find and render this entity on page. We do a call to update it and before calling saveOrUpdate we get the LIE working with the entity to recompute something that uses a lazy fetched one to one entity. So customer has a one to one with credit worksheet..

@NotAudited
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@org.hibernate.annotations.LazyToOne(LazyToOneOption.PROXY)
@Cascade({ org.hibernate.annotations.CascadeType.SAVE_UPDATE,
org.hibernate.annotations.CascadeType.MERGE })
@JoinColumn(name = "worksheet_fk")
@Fetch(FetchMode.SELECT)
public CustomerWorksheet getWorksheet() {


@Named("customerFile")
@ConversationScoped
@Transactional
public class CustomerFileBean

load works..
@Transactional
public void load() {
cust = entityManager.find(Customer.class, new Long(1));

update blows up (note entitytManager.contains will return false)
boolean contains = entityManager.contains(cust);
if(contains == false)
entityManager.merge(cust);

contains = entityManager.contains(cust);
cust.compute(true);

with or without the merge it doesn't matter. Compute blows up.

Comparing the entity manager to string and hashcode they match between requests, however if I get the hibernate session the toString and hashcode do NOT match. I would expect same hibernate session for seam extended persistence context.

Any ideas??



<?xml version="1.0" encoding="UTF-8"?>
<!-- Persistence deployment descriptor for dev profile -->
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

<persistence-unit name="oflows">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/datasources/oFlowsDS</jta-data-source>

<properties>
<property name="hibernate.hbm2ddl.auto" value="validate" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.cache.use_second_level_cache" value="true" />
<property name="hibernate.cache.use_query_cache" value="true" />

<property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.EhCacheProvider" />

<property name="jboss.entity.manager.factory.jndi.name"
value="java:/oflowsEntityManagerFactory" />
<property name="hibernate.ejb.naming_strategy"
value="com.oflows.util.hibernate.OflowsNamingStrategy" />
<property name="hibernate.dialect" value="com.oflows.seam.entity.util.CustomMysql5Dialect" />
<property name="hibernate.jdbc.batch_size" value="20" />
<property name="hibernate.connection.max_allowed_packet"
value="33,554,432" />

Caused by: org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:149) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:195) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
at com.oflows.seam.entity.CustomerWorksheet_$$_javassist_174.getCredit(CustomerWorksheet_$$_javassist_174.java) [:]
at com.oflows.seam.entity.Customer.compute(Customer.java:441) [:]
at com.oflows.seam.session.oconsole.CustomerFileBean.updateInformation(CustomerFileBean.java:115) [:]
at com.oflows.seam.session.oconsole.CustomerFileBean$Proxy$_$$_WeldSubclass.updateInformation(CustomerFileBean$Proxy$_$$_WeldSubclass.java) [:]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0]
at java.lang.reflect.Method.invoke(Method.java:601) [rt.jar:1.7.0]
at org.jboss.interceptor.proxy.SimpleInterceptionChain.invokeNextInterceptor(SimpleInterceptionChain.java:114) [jboss-interceptor-core-2.0.0.Final.jar:2.0.0.Final]
at org.jboss.interceptor.proxy.InterceptorInvocationContext.proceed(InterceptorInvocationContext.java:143) [jboss-interceptor-core-2.0.0.Final.jar:2.0.0.Final]
at org.jboss.seam.transaction.TransactionInterceptor$1.work(TransactionInterceptor.java:152) [seam-transaction-3.1.0.Final.jar:3.1.0.Final]


Top
 Profile  
 
 Post subject: Re: Seam 3 integration with Hibernate
PostPosted: Wed May 08, 2013 5:32 pm 
Beginner
Beginner

Joined: Sat May 21, 2011 7:40 am
Posts: 22
What does your compute method do? Is the EntityManager still open when you are within that method?
Basically you have two options, one is to have EntityManager open during interactions with the lazy initialized relations. The other option is that you pre-fetch the relations which are needed for further actions by using FetchType.EAGER or JOIN FETCH when having queries.

I am not sure but I also think that you have to merge/persist/save/saveOrUpdate the CustomerWorksheet element before you can merge/persist/save/saveOrUpdate the Customer.

Hope that helps you. Maybe also look at the following article i just found that seems to be related to how you annotated you entity and the implications you expect from it: http://www.mkyong.com/hibernate/cascade-jpa-hibernate-annotation-common-mistake/


Top
 Profile  
 
 Post subject: Re: Seam 3 integration with Hibernate
PostPosted: Wed May 08, 2013 5:37 pm 
Newbie

Joined: Mon May 17, 2010 10:24 am
Posts: 5
Thank you for the quick response. I should note we have seen this issue with other mappings that are different, one to many, many to many also. Furthermore these mappings were working fine in the previous stack (seam 2, jsf 1, jboss 5).

We also see if we use hibernate.initialize on load method or change fetch to eager problem goes away. So it does seem to be related to the extended persistent context. The entity manager is open, it just seems weird that the entity manager object is the same but hibernate session is not (delegate).


Top
 Profile  
 
 Post subject: Re: Seam 3 integration with Hibernate
PostPosted: Wed May 08, 2013 6:19 pm 
Beginner
Beginner

Joined: Sat May 21, 2011 7:40 am
Posts: 22
I don't know what the scope of your entity manager is so I can't give you an accurate hint, but this obviously looks like if the entity manager is closed somewhere before accessing the credit of your CustomerWorksheet. How does your producer for the entity manager look like? If you are using seam stuff you should maybe ask on seam forums or so.


Top
 Profile  
 
 Post subject: Re: Seam 3 integration with Hibernate
PostPosted: Thu May 09, 2013 2:05 pm 
Newbie

Joined: Mon May 17, 2010 10:24 am
Posts: 5
Ok we have figured out the issue. When we tried to turn on our EntityManager producer class we were producing entity manager and entity manager factory. That caused weld duplicate producer exception, so we had it off thinking seam included this. It turns out we should only produce entity manager factory not entity manager, which makes sense. This is working...though it seems weird we need to add this at application level and its not build into some seam jar.

@Named
@ApplicationScoped
public class EntityManagerHandler {

@SuppressWarnings("unused")
@ExtensionManaged
@Produces
@PersistenceUnit(unitName="pu1")
@Dependent
private EntityManagerFactory entityManagerFactory;

public void setupEntityManager(
@Observes SeamManagedPersistenceContextCreated event) {

Session session = (Session) event.getEntityManager().getDelegate();
LogManager.info(
EntityManagerHandler.class,
"Seam Managed Persistence COntext created for entity manager:"
+ event.getEntityManager() + "\nsession:"
+ session.toString());

}


Top
 Profile  
 
 Post subject: Re: Seam 3 integration with Hibernate
PostPosted: Thu May 09, 2013 6:34 pm 
Beginner
Beginner

Joined: Sat May 21, 2011 7:40 am
Posts: 22
How could seam possibly know what Persistence Unit you want? ;)


Top
 Profile  
 
 Post subject: Re: Seam 3 integration with Hibernate
PostPosted: Fri May 10, 2013 5:17 pm 
Newbie

Joined: Mon May 17, 2010 10:24 am
Posts: 5
Hi, FYI note seam reference docs say use @ConversationScoped for this entity manager factory but we saw this cause exception on startup when we use entity manager in non conversation/web context. We left at @Dependent but on injecting anywhere entity manager must be marked @Transient as beans are marked serialized and Entity manager is not serialized. I'm not sure why just changing this and only then compile issue came up, but it does make sense. I wouldn't expect entityManager to be serializable.


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