0

I want to display a list of users in an XHTML page. I'm sending a request from my managedBean to the Business through an EJB (3.0) then another request to the DAO still through an EJB (3.0). I'm using JPA 2 and a MySQL database with an entity manager.

I'm sending the following request to my database

@Remote(IDaoUser.class)
@Stateless
Public class DaoUser implements IDaoUser

@PersitenceContext(unitName = "persistence_unit")
Private EntityManager em;

@Override
Public List<User> getAll() {
    Query query = em.createQuery("SELECT u FROM User u");
    List<User> users = query.getResultList();
    return users;
}

At that point everything's fine and I get all my users in my list with all attributes especially id (primary key). My user class has one attribute (birthdate) and inherits from a super class Parent (name, forename, mail, phone...) which inherits again from another class called Who which has just one attribute called id (the primary key). Then I return my List (users) to the business through an EJB and once in the business I realise all the id attributes are 0. I kept all the others attributes (name, forename...) except ids and I can't figure out why I'm losing these values. There are well stored in my List in the DAO but all changed to 0 once in the business.

Here is my business class which is very simple

@Remote(IBusinessUser.class)
@Stateless
Public class BusinessUser implements IBusinessUser

@EJB
private IDaoUser proxy;

@Override
Public List<User> getAll() {
    List<User> users = proxy.getAll();
    return users;
}
ydaetskcoR
  • 53,225
  • 8
  • 158
  • 177

2 Answers2

0

Given the description of the problem, I would ask some questions

  1. Are the parent classes entities themselves, i.e. are they annotated with @Entity.
  2. You need to ensure that @Id is on your primary key. Do you have the @Id annotation on the primary key.

Experience has taught me to always have the @Id attribute in class or at least in a parent class tagged with the @MappedSuperclass. And some people still have problems with retrieving their id fields from the mapped super class.

So see the JEE documentation for using the MappedSuperclass annotation. This may require changing your object inheritance model but that shouldn't be a problem given your requirement.

okmich
  • 700
  • 5
  • 11
0

Thanks for your help. Actually both parent classes are themselves entities. My super class "Who" is the only one having an id attribute (primary key) with the annotation @Id. I can't tag my super class with the @MappedSuperclass since it is associated to another class.