extened CMPC vs SMPC
bernix.bernix.ning.gmail.com Mar 17, 2008 6:53 PMIn my application, I want to hold some informations of the login user, so I have a loginInformation
component in the session.
I set the user into the loginInformation
component when user login, but the problem is, when I tried to retrieve the children object of user in other backing bean, an error said:
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
I am using seam-managed hibernate session
1. Authenticator.java
@Name("authenticator") public class Authenticator { @In private Session mysession; @In(creat=true) private LoginInformation loginInformation; public boolean authenticate() { // find the user from database // authentication logic loginInformation.set(user); return true; } }
2. LoginInformation.java
@Name("loginInformation") @Scope(SESSION) public class LoginInformation { private User user; // other login information fields // getters and setters }
3. User.java
@Entity public class User implements Serializable { private Integer id; private String name; private String password; private Children child; // getters and setters }
4. MyBackingBean.java
@Name("uiBackingBean") @Scope(CONVERSATION) @Restrict("#{identity.loggedIn}") public class MyBackingBean { @In private LoginInformation loginInformation; @Create @Begin public void init () { // Error: could not initialize proxy - no Session String childName = loginInformation.getUser().getChild().getName(); ... } ... }
If I change the Authenticator to use extended CMPC, everything works fine...
@Name("authenticator") public class Authenticator { @PersistenceContext(type=EXTENDED) private EntityManager entityManager; ... }
But we may move the database access into the DAO layer in the future, so the extened CMPC is acceptable...
Any comments will be greatful.