I try to make a simple Java EE application in java with JSF and Java EE.
I couldn't deploy the following customer entity:
package ch.uufstellend.onlineshop.model;
import java.io.Serializable;
import javax.ejb.Stateful;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import lombok.Data;
@Entity
@Data
@Table(name = "CUSTOMER")
@NamedQuery(
name = "Customer.findAll",
query = "SELECT c FROM Customer c")
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Long id;
private String email;
private String password;
public Customer() {
}
@Override
public String toString() {
return id + "-" + email + "-" + password;
}
}
Because of:
Severe: Exception while deploying the app [uuf-onlineshop-ear] : Invalid ejb jar [uuf-onlineshop-ejb-1.0-SNAPSHOT.jar]: it contains zero ejb. Note:
A valid ejb jar requires at least one session, entity (1.x/2.x style), or message-driven bean.
EJB3+ entity beans (@Entity) are POJOs and please package them as library jar.
If the jar file contains valid EJBs which are annotated with EJB component level annotations (@Stateless, @Stateful, @MessageDriven, @Singleton), please check server.log to see whether the annotations were processed properly.
If I add a @Stateful annotation to the entity I'm able to deploy the app. But when I then access the RegisterController the following exception is thrown while persisting the Customer:
exception:> javax.servlet.ServletException: java.lang.IllegalArgumentException: Object: ch.uufstellend.onlineshop.model.__EJB31_Generated__Customer__Intf___302872188 is not a known entity type. root cause
javax.faces.el.EvaluationException: java.lang.IllegalArgumentException: Object: ch.uufstellend.onlineshop.model.__EJB31_Generated__Customer__Intf___302872188 is not a known entity type.
Controller:
package ch.uufstellend.onlineshop;
import ch.uufstellend.onlineshop.model.Customer;
import java.io.Serializable;
import javax.annotation.Resource;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.NotSupportedException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;
import lombok.Getter;
import lombok.Setter;
@Named
@RequestScoped
public class RegisterController implements Serializable {
private static final long serialVersionUID = 1L;
@PersistenceUnit
private EntityManagerFactory emf;
@Resource
private UserTransaction ut;
@Inject
@Getter
@Setter
private Customer customer;
public String persist() {
try {
ut.begin();
EntityManager entityManager = emf.createEntityManager();
entityManager.persist(customer); // HERE the error is thrown
ut.commit();
FacesMessage m = new FacesMessage("Succesfully registered!", "Your email was saved under id " + customer.getId());
FacesContext.getCurrentInstance().addMessage("registerForm", m);
} catch (NotSupportedException | SystemException | RollbackException | HeuristicMixedException | HeuristicRollbackException | SecurityException | IllegalStateException e) {
e.printStackTrace();
FacesMessage m = new FacesMessage(FacesMessage.SEVERITY_WARN, e.getMessage(), e.getCause().getMessage());
FacesContext.getCurrentInstance().addMessage("registerForm", m);
}
return "/register.jsf";
}
}
Any idea where the problem could be?
Your @Entity
entity bean is all fine. Don't modify it. Making it an EJB would indeed cause the described exception because the EJB container creates a proxy around the class which in turn isn't recognized as a JPA entity.
Your @Named
managed bean is wrong. It is tight coupled with EJB responsibilities (persistence and transaction management). Split off EJB responsibilities into a real @Stateless
session bean and have the managed bean invoke it.
@Stateless
public class RegisterService {
@PersistenceContext
private EntityManager entityManager;
public void persist(Customer customer) {
entityManager.persist(customer);
}
}
@Named
@RequestScoped
public class RegisterController {
@Inject
private Customer customer;
@EJB
private RegisterService registerService;
public String submit() {
FacesMessage m;
try {
registerService.persist(customer);
m = new FacesMessage("Succesfully registered!", "Your email was saved under id " + customer.getId());
} catch (Exception e) {
m = new FacesMessage(FacesMessage.SEVERITY_WARN, e.getMessage(), e.getCause().getMessage());
}
FacesContext.getCurrentInstance().addMessage("registerForm", m);
return "/register.jsf";
}
// ...
}
Note that you do not need to manually fiddle with user transactions in the real EJB. Only the @Inject
on Customer
is kind of weird. I'm not sure what the @Data
does, but if it has the same effect as CDI's @Model
annotation, then that's OK. Otherwise head off to second link below for concrete examples and more links.