EntityManager identifier generation exception?

294 views Asked by At

I ran into this problem when I try to create a record in DB through entitybean and entitymanager. I have two methods in the UserManagerImpl.java, the odd thing is the findalluser method works well and create one throws the following exception in Jboss-7 console: the code are given below

   identifier generation excepton: ids for this class must be manully assigned before calling save(): gmartinc.swe645.common.entitybeans.user 

The sql statement I used to create the table in MySql is

create table users(
userid varchar(100) primary key,
firstname varchar(100),
lastname varchar(100),
group varchar(100),
team varchar(100)
);

and the Entitybean to map this table is 

    import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;


@Entity
@Table(name = "users")
public class user implements Serializable {
  private String userid;
  private String firstname;
  private String lastname;
  private String password;
  private String team;


  @Id
  @Column(name="userid")
  public String getUserId() {
    return userid;
  }
  public void setUserId(String id) {
    this.userid = id;
  }

  @Column(name="firstname")
  public String getFirstName() {
    return firstname;
  }
  public void setFirstName(String firstName) {
    this.firstname = firstName;
  }

  @Column(name = "lastname")
  public String getLastName() {
    return lastname;
  }
  public void setLastName(String lastName) {
    this.lastname = lastName;
  }

  @Column(name = "password")
  public String getPassWord() {
    return password;
  }
  public void setPassWord(String password) {
    this.password = password;
  }

  @Column(name = "team")
  public String getTeam() {
    return team;
  }
  public void setTeam(String team) {
    this.team = team;
  }


}

and the UserManagerImpl.java is 
import java.util.List;

import javax.ejb.Stateless;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;


@Stateless
public class UserManagerImpl implements UserManagerRemote {
  @PersistenceContext(unitName = "Swe645University")
  private EntityManager entityManager;

  public void createUser(user us) {
    entityManager.persist(us);
  }

  public List<user> findUser() {
        Query q = entityManager.createQuery(" from user p ");
        return q.getResultList();
      }
}

Really don't know what's wrong and Thanks in advance!

1

There are 1 answers

1
dcernahoschi On

You need to specify a generator for your id.

If your underlying persistence manager is hibernate take a look at hibernate generators.