[EJB 3.0] - Re: LazyInitializationException in many-to-many relation tha
by jaikiran
anonymous wrote : I am getting a LazyInitializationException: failed to lazily initialize a collection of role : eco.ejb3.entity.Subject.usersThatSubscribedToMe, no session or session was closed
A LazyInitializationException occurs when the entity or its associated collection is being accessed when the session is no longer available. In your case, i guess, your JUnit test first called a method on the bean to retrieve the entity. When this is done, a session is opened to retrieve that persistent object. The object is then returned to the client (your JUnit test case). At the same time the session is closed (since the bean method transaction completed).
Now at some later point in time, in your testcase you invoke the addPublicSubject method on the bean passing it the entity which was loaded as part of a session which no longer exists. Now when you try to access the entities collection, it throws this LazyInitializationException.
To avoid this exception, you either have to access that collection when the session is open or use the merge method on the entitymanager to attach the entity to a session. Like this:
@TransactionAttribute(TransactionAttributeType.REQUIRED)
| public void addPublicSubject(Subject s, User u) {
|
| //merge the subject. Remember that the merge operation, returns a merged object.
| // So use this returned object, henceforth in the code.
| Subject mergedSubject = entityManager.merge(s);
|
| //Let's also merge the User since it too was detached
| User mergedUser = entityManager.merge(u);
|
| mergedSubject.getUsersThatSubscribedToMe().add(mergedUser);
|
| entityManager.persist(mergedSubject);
|
| }
This is just a pseudo-code and you might have to do some additional changes to get this working. If this doesn't work, then please post back with the exception logs and the code changes that you did.
While posting the logs or code or xml content, remember to wrap it in a code block using the Code button in the message editor window and please hit the Preview button to make sure your post is correctly formatted
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4145885#4145885
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4145885
18 years
[Installation, Configuration & DEPLOYMENT] - Re: Can not get connection to server. Problem establishing s
by alexsbe
Here is the answer of your questions
1) Is the client that you are running on a different system?
The OS of the client is Windows for the moment. It is a tomcat server (I tried with a java application for the same result).
2) Is the client running on Tomcat and the EJB deployed on JBoss?
Exact
3) Does it work if both Tomcat (client) and JBoss(EJB) are on the same machine? (I guess, that's what you meant in your comment, above).
Yes it is working if there are in the same machine.
4) Where have you placed the jndi.properties file? Are you sure it is being picked up? Can you try hard-coding the properties in the client code while doing the lookup and see if it works?
Yes it is picked up, and correct.
Here is the code of my method getcontext. I checked if the values I have setted are correct.
private static Context getContext() throws NamingException {
|
| ResourceBundle bundle = ResourceBundle.getBundle("jndi");
| Properties props = new Properties();
|
| props.put(Context.INITIAL_CONTEXT_FACTORY,bundle.getString(Context.INITIAL_CONTEXT_FACTORY));
| props.put(Context.URL_PKG_PREFIXES,bundle.getString(Context.URL_PKG_PREFIXES));
| props.put(Context.PROVIDER_URL,bundle.getString(Context.PROVIDER_URL));
|
| return new InitialContext(props);
|
| }
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4145866#4145866
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4145866
18 years