@Entity
@Table(name="t_user")
public class TUser implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column
private String userName;
public TUser() {
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
这是我的实体类
@Test
public void testConnection() {
TUser user = new TUser();
EntityManagerFactory entityManagerFactory = (EntityManagerFactory) applicationContext
.getBean("entityManagerFactory");
EntityManager em = entityManagerFactory.createEntityManager();
user.setId(1);
user.setUserName("ccccc");
em.persist(user);
em.getTransaction().commit();
}
这样测试的话 回报一个错误Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: com.gtercn.model.user.TUser
把这行@GeneratedValue(strategy = GenerationType.AUTO)
去掉就能插入了 为什么啊 ?请高手解答
------解决方案--------------------
奇怪了,
jpa中,在指定主键时,如果不指定主键生成策略,默认其實也为AUTO。
你把private int id;換成private Integer identifier;試試
------解决方案--------------------
看看你的id是不是自动增长啊,如果是的话,那就要加上去,不是的话就不要了,我刚刚也是遇到这个问题了呵呵,,,
------解决方案--------------------
楼主这个有挑战性,,我还不会,,,