复合主键和外键映射问题

时间:2019-01-21 18:56:41

标签: hibernate jpa

我有两个实体Employee和Phone。在具有相同复合主键的情况下,并且对于电话实体,主键充当外键。当尝试创建OneTONe关系时,我在下面面临的问题得到了解释。建议我尽快解决此问题

        //Employee 
        @Entity(name = "Employee")
        @Table(name = "employee")
        @IdClass(EId.class)
        public class Employee {

            @Id
            @GeneratedValue(strategy = GenerationType.AUTO)
            @Column(name = "c_id")
            private int id;

            @Id
            @GeneratedValue(strategy = GenerationType.AUTO)
            @Column(name = "uuid")
            private int uuid;

            private String name;

            @OneToOne(cascade = CascadeType.ALL, mappedBy = "employee")
            private Phone phone;

//Getters and Setters }
        //Phone
        @Entity(name = "Phone")
        @Table(name = "phone")
        @IdClass(PId.class)
        public class Phone {

            @Id
            @Column(name = "num")
            private int id;

            @Id
            @Column(name = "uuid")
            private int uuid;

            private String name;

            @OneToOne
            @MapsId
            @JoinColumns({ @JoinColumn(name = "num", referencedColumnName = "c_id"),
                    @JoinColumn(name = "uuid", referencedColumnName = "uuid") })
            private Employee employee;
        //Getters and Setters }
        //Repo

        @Repository
        @Transactional
        public class Repo {
            @Autowired
            EntityManager em;

            public void insert() {

                Employee employee = new Employee();
                employee.setName("Azum");

                Phone phone = new Phone();
                phone.setName("Nokia 6.1 plus");

                employee.setPhone(phone);
                phone.setEmployee(employee);

                em.persist(employee);
            }
        }

        //Error
         insert 
            into
                employee
                (name, c_id, uuid) 
            values
                (?, ?, ?)
        2019-01-22 00:08:23.998 TRACE 24724 --- [  restartedMain] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - [Ravi]
        2019-01-22 00:08:23.998 TRACE 24724 --- [  restartedMain] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [INTEGER] - [1]
        2019-01-22 00:08:23.999 TRACE 24724 --- [  restartedMain] o.h.type.descriptor.sql.BasicBinder      : binding parameter [3] as [INTEGER] - [2]
        Hibernate: 
            insert 
            into
                phone
                (name, num, uuid) 
            values
                (?, ?, ?)
        2019-01-22 00:08:24.000 TRACE 24724 --- [  restartedMain] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - [Nokia 6.1 plus]
        2019-01-22 00:08:24.000 TRACE 24724 --- [  restartedMain] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [INTEGER] - [0]
        2019-01-22 00:08:24.000 TRACE 24724 --- [  restartedMain] o.h.type.descriptor.sql.BasicBinder      : binding parameter [3] as [INTEGER] - [0]
        2019-01-22 00:08:24.002  WARN 24724 --- [  restartedMain] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 23506, SQLState: 23506
        2019-01-22 00:08:24.002 ERROR 24724 --- [  restartedMain] o.h.engine.jdbc.spi.SqlExceptionHelper   : Referential integrity constraint violation: "FKMAWCH876NTEFVR66GJWEGJC09: PUBLIC.PHONE FOREIGN KEY(NUM, UUID) REFERENCES PUBLIC.EMPLOYEE(C_ID, UUID) (0, 0)"; SQL statement:
        insert into phone (name, num, uuid) values (?, ?, ?) [23506-197]
        2019-01-22 00:08:24.006 ERROR 24724 --- [  restartedMain] o.h.i.ExceptionMapperStandardImpl        : HHH000346: Error during managed flush [org.hibernate.exception.ConstraintViolationException: could not execute statement]
        2019-01-22 00:08:24.011  INFO 24724 --- [  restartedMain] ConditionEvaluationReportLoggingListener :

我正在共享主键。.当插入数据时,由于此应用程序未共享给Phone的员工主键数据引发Integarty问题

0 个答案:

没有答案