79764749

Date: 2025-09-15 04:59:05
Score: 1
Natty:
Report link

You can add @JoinColumn on the @OneToMany side of relation that make it owner-side like this:

@Entity
public class Employee {
    ...

    @OneToMany(mappedBy="associatedEmployee", cascade=CascadeType.ALL)
    @JoinColumn(name="employee_id")
    private Set<Vehicle> vehicles;

    ...

}

@Entity 
public class Vehicle {
    ...

    @ManyToOne
    private Employee associatedEmployee;

    ...
}

that's set parent and corresponding foreign key automatically.

but I prefer to set parent with setter without change owner side of ManyToOne relation.

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @JoinColumn
  • User mentioned (0): @OneToMany
  • Low reputation (1):
Posted by: hosein ahangari