-
1. Re: Help for the org.hibernate.LazyInitializationException
zeeman Mar 9, 2012 12:58 AM (in response to yuanqixun)Have you configured Seam persistence like mentioned in the documentation? Let seam manage the entity manager for you. If you inject with @Inject the entityManager you should not have this issue.
-
2. Re: Help for the org.hibernate.LazyInitializationException
yuanqixun Mar 9, 2012 1:42 AM (in response to zeeman)you can see my configure:
1. I have one produce class
@ConversationScoped public class PersistenceManager implements Serializable{ @Produces @CRM @PersistenceUnit(unitName="crm") EntityManagerFactory crmEMFactory; @Produces @Dependent @CRM @PersistenceContext(unitName="crm",type=PersistenceContextType.EXTENDED) EntityManager crmEntityManager; }
2. in my service bean, I inject the em:
@Named("crm.room.roomSVC") @ConversationScoped public interface RoomSVC { ...... } public class RoomSVCImpl implements Serializable, RoomSVC { @Inject @CRM EntityManager em; ....... do some database access..... }
3. in my action bean used by xhtml page
@ConversationScoped @Named("crm.room.roomZiLiaoDetailAction") public class RoomZiLiaoDetailAction implements Serializable { @Inject RoomSVC roomSVC; private Room room;//getter and setter omit @PostConstruct public void afterCreate() { roomUuid = roomUuidReq.get(); if (StringUtils.isNotEmpty(roomUuid)) { log.infov("The room uuid:{0}", roomUuid); room = roomSVC.findRoomById(roomUuid); } else { // TODO 需要新增吗,否则应该抛出异常? } } }
4. The room entity have Many2One attribute
@Entity @Table(name = "CRM_ROOM") public class Room { .... @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "HXBH", referencedColumnName = "BH") private HouseType houseType; }
5. two page call the RoomZiLiaoDetailAction bean
page1.xhtml
<h:outputText value="room:#{crm.room.roomZiLiaoDetailAction.room.name}" />
page2.xhtml
<h:outputText value="houseType:#{crm.room.roomZiLiaoDetailAction.room.houseType.name}" />
over, and you will see the exception.
-
3. Re: Help for the org.hibernate.LazyInitializationException
zeeman Mar 10, 2012 2:14 AM (in response to yuanqixun)1 of 1 people found this helpfulyou're missing few things.
This is how I produce my EMF. I don't produce EM as Seam knows how.
//Don't put any scope here
public class PersistenceConfiguration {
@Produces
@ExtensionManaged
@ConversationScoped
@PersistenceUnit
private EntityManagerFactory conversationEMF;
}
On
RoomZiLiaoDetailAction
, be sure you're starting and end the conversation correctly. -
4. Re: Help for the org.hibernate.LazyInitializationException
yuanqixun Mar 12, 2012 3:12 AM (in response to zeeman)I don't produce EM as Seam knows how.
But,how to inject the entityManager in your action bean? please give a example, thanks.
-
5. Re: Help for the org.hibernate.LazyInitializationException
yuanqixun Mar 12, 2012 4:38 AM (in response to zeeman)Yes,you are right, I followed your guide, and no this exception!