[英]Hibernate does not save Object when using @GeneratedValue(strategy=GenerationType.AUTO)
I am learning Hibernate
with MySql
data base, while making small project i come up with strange behave of hibernate. 我正在用MySql
学习Hibernate
,而在做一个小项目时,我想到了Hibernate
奇怪行为。 I am trying to save one entity (code is below), but entity not getting persist into DB. 我正在尝试保存一个实体(下面的代码),但是实体无法持久保存到数据库中。 i am using session.save(e)
method only. 我只使用session.save(e)
方法。 It is working if i am using @GeneratedValue(strategy=GenerationType.IDENTITY)
and not if i am using @GeneratedValue(strategy=GenerationType.AUTO)
. 如果我正在使用@GeneratedValue(strategy=GenerationType.IDENTITY)
而不是如果我正在使用@GeneratedValue(strategy=GenerationType.AUTO)
。
Again if i use session transaction ( begin and commit
) it is working in both case (AUTO and IDENTITY). 同样,如果我使用会话事务( begin and commit
),则在两种情况下(AUTO和IDENTITY)都可以工作。
Again if i use two different entity one with AUTO and another with IDENTITY it is working. 同样,如果我使用两个不同的实体,一个与AUTO一起使用,另一个与IDENTITY一起使用,则它正在工作。
First Entity 第一实体
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "EMPLOYEE")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "EMP_ID")
private int empId;
@Column(name ="name")
private String name;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Second Entity 第二实体
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "SUPEMPLOYEE")
public class SupperEmploye {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "EMP_ID")
private int empId;
@Column(name ="name")
private String name;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Code for save entity 保存实体代码
Session s= HibernateConfig.getSessionFactory().openSession();
//s.getTransaction().begin(); //working if add
Employee e= new Employee();
e.setName("Employee");
s.save(e);
SupperEmploye se = new SupperEmploye();
se.setName("Super Employee");
s.save(se);
s.close();
//s.getTransaction().commit(); // Working if add
HibernateConfig.getSessionFactory().close();
NOTE: Using hibernate 5, MySQL 8, JAVA 8 注意:使用休眠5,MySQL 8,JAVA 8
please comment if need more info 如果需要更多信息,请发表评论
尝试这个
@GeneratedValue(strategy=GenerationType.IDENTITY)
You should use 你应该用
@GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
instead of 代替
@GeneratedValue(strategy = GenerationType.AUTO)
Read below this nice article on this. 在下面阅读这篇不错的文章。
https://vladmihalcea.com/why-should-not-use-the-auto-jpa-generationtype-with-mysql-and-hibernate/ https://vladmihalcea.com/why-should-not-use-the-auto-jpa-generationtype-with-mysql-and-hibernate/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.