当前位置: 代码迷 >> J2EE >> EJB3.0 persist 之后,返回自动增长为null的有关问题
  详细解决方案

EJB3.0 persist 之后,返回自动增长为null的有关问题

热度:377   发布时间:2016-04-22 03:17:21.0
EJB3.0 persist 之后,返回自动增长为null的问题
SessionBean:

@Stateless
public class LoginBeanBean implements LoginBeanRemote, LoginBeanLocal {

  @PersistenceContext(unitName = "wuooPU")
  private EntityManager em;

  public Login persist(Login login){
  em.persist(login);
  System.out.println("ejb insert loginid = "+login.getId());
  return login;
  }
}


EntityBean:
@Entity
@Table(name = "login")
public class Login implements Serializable {

  private static final long serialVersionUID = 1L;
  @Id
  @Column(name = "id", nullable = false)
  private Integer id;
  @Column(name = "loginid", nullable = false)
  private String loginid;
  @Column(name = "password", nullable = false)
  private String password;
下面一些get,set就不帖了

id是主键,database table中已设为自动增长列

最后jsp后台javabean中调用persist后,数据已插入数据库,但返回的id是null,请问怎样能得到自动增长的ID

------解决方案--------------------
在ID上添加注解,

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
  相关解决方案