[JBoss Seam] - Problems with list page, persistence, and detached entities
by Oberiko
Hello.
I seem to be doing something wrong with my Stateful Session Bean in that I can't seem to have it persist properly.
I have a list page where I want to delete previously entered users. I've put the @DataModel into the page scope, as I want it to be kept available until I leave the page.
| package org.domain.myProject.session;
|
| import static javax.persistence.PersistenceContextType.EXTENDED;
|
| import java.util.List;
|
| import javax.ejb.Remove;
| import javax.ejb.Stateful;
| import javax.persistence.EntityManager;
| import javax.persistence.PersistenceContext;
|
| import org.domain.myProject.entity.User;
| import org.domain.myProject.session.local.UserEditLocal;
| import org.jboss.seam.ScopeType;
| import org.jboss.seam.annotations.Destroy;
| import org.jboss.seam.annotations.Factory;
| import org.jboss.seam.annotations.In;
| import org.jboss.seam.annotations.Logger;
| import org.jboss.seam.annotations.Name;
| import org.jboss.seam.annotations.Out;
| import org.jboss.seam.annotations.datamodel.DataModel;
| import org.jboss.seam.annotations.datamodel.DataModelSelection;
| import org.jboss.seam.faces.FacesMessages;
| import org.jboss.seam.log.Log;
|
| @Stateful
| @Name("userEdit")
| public class UserEditForm implements UserEditLocal {
|
| @Logger
| private Log log;
|
| @In
| FacesMessages facesMessages;
|
| @PersistenceContext(type = EXTENDED)
| private EntityManager em;
|
| @DataModelSelection
| @Out(required=false)
| private User user;
|
| @DataModel(scope=ScopeType.PAGE)
| private List<User> users;
|
| @SuppressWarnings("unchecked")
| @Factory("users")
| public void findUsers(){
| log.info("********************************************* Finding the users");
| if (users == null)
| log.info("users is null");
| else if (users.size() == 0)
| log.info("users has a size of zero");
| else
| log.info("Strange, because users has " +users.size() + " entries");
| users = em.createQuery("from User u").getResultList();
| }
|
| public void remove(){
| log.info("Deleting user " +user.getName());
| em.remove(user);
| users.remove(user);
| }
|
| public void update() {
| //TODO write the update method
| }
|
| @Destroy @Remove
| public void destroy() {}
| }
|
The following is my console log:
| 19:48:04,917 INFO [STDOUT] Hibernate:
| insert
| into
| User
| (id, name)
| values
| (null, ?)
| 19:48:04,917 INFO [STDOUT] Hibernate:
| call identity()
| 19:48:04,917 INFO [SaveUserAction] John was saved.
| 19:48:08,401 INFO [UserEditForm] ********************************************* Finding the users
| 19:48:08,401 INFO [UserEditForm] users is null
| 19:48:08,417 INFO [STDOUT] Hibernate:
| select
| user0_.id as id0_,
| user0_.name as name0_
| from
| User user0_
| 19:48:11,323 INFO [UserEditForm] ********************************************* Finding the users
| 19:48:11,323 INFO [UserEditForm] users is null
| 19:48:11,323 INFO [STDOUT] Hibernate:
| select
| user0_.id as id0_,
| user0_.name as name0_
| from
| User user0_
| 19:48:11,323 INFO [UserEditForm] Deleting user Vader
| 19:48:11,339 INFO [STDOUT] Hibernate:
| delete
| from
| User
| where
| id=?
| 19:48:26,495 INFO [UserEditForm] ********************************************* Finding the users
| 19:48:26,495 INFO [UserEditForm] users is null
| 19:48:26,495 INFO [STDOUT] Hibernate:
| select
| user0_.id as id0_,
| user0_.name as name0_
| from
| User user0_
| 19:48:26,495 INFO [UserEditForm] Deleting user Skywalker
| 19:48:26,511 INFO [STDOUT] Hibernate:
| delete
| from
| User
| where
| id=?
|
And this is the page I'm calling it from:
| <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
| "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
| <html xmlns="http://www.w3.org/1999/xhtml"
| xmlns:s="http://jboss.com/products/seam/taglib"
| xmlns:f="http://java.sun.com/jsf/core"
| xmlns:h="http://java.sun.com/jsf/html">
|
| <f:view>
| <h:messages />
| <h:form>
| <h:dataTable value="#{users}" var="user">
| <h:column>
| <f:facet name="header">
| <h:outputText value="Id" />
| </f:facet>
| <h:outputText value="#{user.id}" />
| </h:column>
| <h:column>
| <f:facet name="header">
| <h:outputText value="Name" />
| </f:facet>
| <h:outputText value="#{user.name}" />
| </h:column>
| <h:column>
| <f:facet name="header">
| <h:outputText value="Actions" />
| </f:facet>
| <s:button action="#{userEdit.remove()}" value="S. Delete"/>
| <h:commandButton action="#{userEdit.remove()}" value="A. Delete" />
| </h:column>
| </h:dataTable>
| </h:form>
| <s:button view="/user.xhtml" value="Return"/>
| </f:view>
|
| </html>
|
|
I think I understand that the s:button, since it's not passing the form back, is basically treating everything as a new page, which is why it's not being persisted (that's what I used in the above logs). When I try the <h:commandButton> though, I get the following:
Exception during request processing:
| Caused by javax.servlet.ServletException with message: "#{userEdit.remove()}: javax.ejb.EJBTransactionRolledbackException: Removing a detached instance org.domain.myProject.entity.User#7"
Is there a way, barring putting the page in the session scope, to keep the entities from being detached?
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4121145#4121145
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4121145
18 years, 5 months
[JBoss Seam] - Need definitive answers on unit testing with Seam
by grettke_spdr
Hi folks,
We are going forward with Seam as the foundation of our application architecture for "the big project". So, I need some definitive answers on unit testing with Seam.
There is conflicting information regarding how one should go about unit testing components. One camp says to use SeamTest, the other camp says that you should not use it.
See in particular: here and here.
Or search for "unit test could not find datasource" with "include all items" set to true in the Seam forum.
Q1. What is the right way to unit test Seam components?
Q2. How does one test without SeamTest?
The same problem that everyone seems to run into is that when they make the call:
| Persistence.createEntityManagerFactory("my-data-source")
|
The testing errors out since the datasource can not be found. The suggestion was made to register the datasource in JNDI, but no one (including myself hence this post) talks about how to do so without relying on SeamTest to do so.
What are all the steps that one needs to take to perform unit testing against a datasource without using the embedded container?
Ref: here.
Q2. How does one test with SeamTest?
Everyone seems to be getting the "cannot find datasource" error because the test gets run before the embedded container is actually initialized. The generally agreed upon workaround seem to be to initialize the datasource before every test is run:
See: here.
This is how we did it on our first Seam project. The code is ugly and hacking; bless the programmer that took care of it for the team.
Is this really the "right" way to do it. I feel like I am doing something wrong and this is a workaround. What is the official way to deal with this? Is this normal behavior or are all of users doing something wrong?
What is the official way to deal with the issue? Is it really as simple as "configure everything exactly the same way as the sample projects"? Remember everyone is encountering this error using seam-gen'd projects.
This is really confusing. Gavin, Pete, we need your magic on this, you are the ones who can shed the light onto our ignorance. This may sound like craziness to you, but it is really frustrating.
Looking forward to your reply! :)
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4121143#4121143
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4121143
18 years, 5 months