Spring JPA data only one composite key automatically increases

I am using a MySQL database.

My table is an employee in which there are two primary keys, of which one automatically increases.

My code is:

@Embeddable
    public class EmployeeId implements Serializable {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        @Column(name = "id", nullable = false)// this i want to increment 
        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")

        @Column(name = "gender_key", nullable = false)
        private id 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> {
        }

In My Controller, I want id after employeeRepository.save (bean); because I want to save this identifier in another table.

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

But I always get 0 id value.

How to get incremental id value that is inserted into MySQL table?

Please, help.

Thanks in advance.

+4
source share
1 answer

save? , :

employeeRepository.save(bean)
int id = bean.getId();
logger.info("id is --- > "+id);

, . , , :

bean = employeeRepository.save(bean)
int id = bean.getId();
logger.info("id is --- > "+id);
0

Source: https://habr.com/ru/post/1685794/


All Articles