Error when using @SessionScoped and @Entity Annotation
janey Jan 8, 2010 2:45 PMHey all,
i am trying to implement a simple WELD example with a jsf page to add a new account to the dateabase. My Problem is that as soon as I use a scope-annotation e.g. @SessionScoped and a @Entity Annotation I get following error when I try to perist the Account Object:
20:47:15,700 ERROR Faces Servlet Servlet.service() for servlet Faces Servlet threw exception java.lang.IllegalArgumentException: Unknown entity: bankgroupServer.Account_$$_javassist_23 at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:311) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
The head of my Account.java looks like this:
@SessionScoped @Named @Entity @Table(name = "tbl_Account") public class Account implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.AUTO) private int accountNumber; private float amount = 0; [...] }
My jsf-page looks like:
<h:form id="account" styleClass="edit"> <rich:panel> <f:facet name="header">#{account.managed ? 'Edit' : 'Add'} Account</f:facet> Amount: <h:inputText id="amountId" required="true" value="#{account.amount}" /> </rich:panel> <div class="actionButtons"> <h:commandButton id="save" value="Save" action="#{accountManager.createAccount}" rendered="#{!account.managed}"/> </div> </h:form>
My AccountBean.java looks like:
@Named("accountManager") @SessionScoped @Stateful public class AccountBean implements Serializable, IAccountBeanLocal { private static final long serialVersionUID = 1L; @Inject Account account; @Inject @BankDatabase EntityManager database; /** * Default constructor. */ public AccountBean() { // TODO Auto-generated constructor stub } /** * */ public void createAccount() { System.out.println(account.getAmount()); // --> Test if injection was correct database.persist(account); // --> Error occurs } @Remove public void destroy() { } }
If I leave out the @SessionSyoped annotation in Account.java, I am able to persist the Account object without errors, but the account object I filled with the amount-value from the jsf page won't be injected but a new one will be created.
Am I doing something principally wrong? Am I not able to use @Entity's with a scope??? Why can't the Entity be found if I define a scope?
I guess if I use two diffenrent classes, one for the entity to persist and one for the bean that takes the values from the jsf page. I would need to copy the values from the one class to the entity class and then call persist. I guess that would work but do I really need 2 classes for one purpose????
If you need the persistent.xml or anything else please just ask me to and I will post it.
I can't get rid of the error above an I cant guess of working solution. I need your help :)