У меня есть две организации: Сотрудник и Телефон.Как с одним и тем же составным первичным ключом, так и для телефонного объекта первичный ключ действует как внешний ключ.при попытке создать отношения 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 :
У меня общие первичные ключи .. при вставке данных Данные первичного ключа сотрудника, не переданные в телефон из-за этого приложения, вызывают проблему Integarty