Foreign Key Constraint Issue for One-to-Many One-Way Relationships

I have Employee(parent) and Emp_Contacts(child). Only the class Employeehas unidirectional mapping.

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.Digits;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.NotEmpty;

@Entity
@Table(name="EMPLOYEE")
public class Employee {

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

  @Size(min=3, max=50)
  @Column(name = "NAME", nullable = false)
  private String name;

...

  @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
  @JoinColumn(name = "emp_id")
  private Set<Emp_Contacts> contacts;

...getters and setters...

My is Emp_Contactsas follows:

@Entity
@Table(name = "Emp_Contacts")
public class Emp_Contacts implements Serializable {

  private static final long serialVersionUID = 1L;
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private int emp_contact_id;

  @NotNull
  @Column(name = "emp_id")
  private long emp_id;

....

Table DB does not have a zero FK limit for table Emp_Contactson emp_id.

  • If I delete the above restriction, the persist(employee)employee and the corresponding emp_contacts will be saved.
  • With FK limitation, I get below error:

    MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails

I searched the Internet and found this link https://forum.hibernate.org/viewtopic.php?f=1&t=995514

But if I set the value nullable to Employee:

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "emp_id", nullable = false)
private Set<Emp_Contacts> contacts

my server does not even start, I get below the error:

Repeated column in mapping for entity: com.cynosure.model.Emp_Contacts
column: emp_id (should be mapped with insert="false" update="false")

What am I doing wrong?

+4
3

Employee , ( ). , Emp_Contacts, .

- :

public class Employee {
  @OneToMany(mappedBy = "employee")
  private Set<Emp_Contacts> contacts;
}

public class Emp_Contacts {
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "emp_id", nullable = false)
  private Employee employee;
}

, , , Emp_Contacts.

, Emp_Contacts ( ). , , ​​ ( ), Hibernate -, . . .

, , ( HQL/JPQL).

( ), emp_id insertable = false, updatable = false, Hibernate Employee:

@Column(name = "emp_id, insertable = false, updatable = false")
private long emp_id;
+3

,

@NotNull
@Column(name = "emp_id")
private long emp_id;

Emp_Contacts. , Hibernate emp_id, Employee.

0
  • Hibernate , . , Emp_Contacts. JoinColumn updatable = false. Hibernate .
  • orphanRemoval = true OneToMany. , Hibernate defaults orphanRemoval true, :( . , .
0

All Articles