EntityManager标识符生成异常?

时间:2011-11-05 07:42:55

标签: java entity-bean

当我尝试通过entitybean和entitymanager在DB中创建记录时遇到了这个问题。我在UserManagerImpl.java中有两个方法,奇怪的是findalluser方法运行良好并创建一个在Jboss-7控制台中引发以下异常:    代码如下:

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

我用于在MySql中创建表的sql语句是

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();
      }
}

真的不知道出了什么问题,并提前致谢!

1 个答案:

答案 0 :(得分:0)

您需要为您的ID指定生成器。

如果您的底层持久性管理器处于休眠状态,请查看hibernate生成器。