简体   繁体   English

使用@GeneratedValue(strategy = GenerationType.AUTO)时,Hibernate不保存对象

[英]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.

相关问题 Hibernate @GeneratedValue(strategy = GenerationType.AUTO) - Hibernate @GeneratedValue(strategy = GenerationType.AUTO) oracle上的Hibernate序列,@ GeneratedValue(strategy = GenerationType.AUTO) - Hibernate sequence on oracle, @GeneratedValue(strategy = GenerationType.AUTO) JPA @GeneratedValue(strategy=GenerationType.AUTO) 在 MySQL 上不起作用 - JPA @GeneratedValue(strategy=GenerationType.AUTO) does not work on MySQL 休眠:@GeneratedValue(strategy = GenerationType - Hibernate: @GeneratedValue(strategy = GenerationType 休眠Junit hsqldb-(strategy = GenerationType.AUTO)不起作用 - Hibernate Junit hsqldb - (strategy = GenerationType.AUTO) not working 休眠:在以下情况下插入具有自定义ID的实体:strategy = GenerationType.AUTO - Hibernate: inserting an entity with custom id in the case: strategy = GenerationType.AUTO @GeneratedValue(strategy = GenerationType.AUTO): MySQL 和表之间共享的生成ID? - @GeneratedValue(strategy = GenerationType.AUTO): MySQL and generated IDs shared between tables? hibernate GenerationType.AUTO如何在Oracle中运行? - How does the hibernate GenerationType.AUTO work in Oracle? EclipseLink和H2策略= GenerationType.AUTO - EclipseLink and H2 strategy = GenerationType.AUTO GenerationType.AUTO没有选择适当的策略 - GenerationType.AUTO not picking appropriate strategy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM