Spring数据JPA只有一个组合键是自动递增的问题 [英] Spring data JPA only one composite key is auto incremented issue

查看:80
本文介绍了Spring数据JPA只有一个组合键是自动递增的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MySQL数据库.

I am using MySQL database.

在我的表中,我有两个四个主键,其中一个是自动递增的.

In my table i there are two four primary keys, out of which one is auto incremented.

@Embeddable
    public class EmployeeId implements Serializable {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        @Column(name = "id", nullable = false)// 
         This is just Pk  in mysql table
        **private int id;**
        // I have  tried and @GeneratedValue(strategy = GenerationType.IDENTITY),
        @GeneratedValue(strategy = GenerationType.IDENTITY) 
        //and @GeneratedValue(strategy = GenerationType.TABLE)
        //@GeneratedValue(strategy = GenerationType.AUTO, generator = "id") @SequenceGenerator(name = "id", sequenceName = "id")
     **this is auto incremented and pk in mysql table**
        @Column(name = "gender_key", nullable = false)
        private int gender_key;

        }



        @Entity
        @Table(name = "employee")
        public class employee {
        @EmbeddedId
        private EmployeeId employeeId;

        private String emp_name;
        private String mobile_no;

        employee() {
        }}

        public interface employeeRepository extends
            JpaRepository<employee, EmployeeId> {
        }

在我的控制器中,我想要在employeeRepository.save(bean)之后的ID;方法,因为我想将该ID保存在其他数据库中.

In My Controller I want id after employeeRepository.save(bean); method because i want to save that id in different db .

logger.info("gender_key是--->" + gender_key);

logger.info("gender_key is --- > "+gender_key);

但是我总是得到0的sex_key值.

But I am getting always 0 value of gender_key.

我尝试过的是:

bean = employeeRepository.save(bean)
int gender_key= bean.getGender_key();
logger.info("gender_keyis --- > "+gender_key);

但是,gender_key的值仍为0(零). 或我必须在存储库中写的任何查询.

But still the value for gender_key is 0(Zero). Or any Query which I have to write in repository .

如何获取插入MySQL表中的sex_key的自动递增值?

How I can get the auto incremented value of gender_key which is inserted into MySQL table?

请帮助.

谢谢.

推荐答案

您的JPA @Id不需要与数据库PK列匹配.只要它是唯一的,那就很重要.

Your JPA @Id does not need to match the database PK column(s). So long as it is unique then that is all that matters.

来自 https://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing:

JPA ID不一定总是与数据库表主数据库匹配 键约束,也不需要主键或唯一约束.

The JPA Id does not always have to match the database table primary key constraint, nor is a primary key or a unique constraint required.

由于您要确保自动递增的列是唯一的,因此只需将sex_key用作您的@ID,并将地图ID用作普通列.

As your an auto-increment column is guaranteed to be unique then just use gender_key as your @ID and map id as a normal column.

@Entity
@Table(name = "employee")
public class employee {

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY) 
   private int genderKey;

   @Column
   private int id;

}

说实话,我会发现您的架构令人困惑.

To be honest I would find your schema confusing however.

我还建议您阅读以下内容:

I would also suggest reading the following:

https://www.javatpoint.com/java-naming-conventions

这篇关于Spring数据JPA只有一个组合键是自动递增的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆