I have a relation between tow entities:
UserProfile
@Getter
@Setter
@NoArgsConstructor
@Entity
@Table(name="userprofile")
public class UserProfile implements Identifiable, Displayable {
@Id
@GeneratedValue(generator = "uuid2", strategy = GenerationType.SEQUENCE)
@GenericGenerator(name = "uuid2", strategy = "uuid2", parameters = { @org.hibernate.annotations.Parameter(name = "uuid_gen_strategy_class", value = "org.hibernate.id.uuid.CustomVersionOneStrategy") })
private UUID id;
@Column(length = 100)
@NotNull
private String name;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "userProfile", cascade = {CascadeType.ALL}, orphanRemoval = true)
private List<UserPermission> permissions;
@Override
public String getDisplayString() {
return name;
}
}
UserPermission
@Getter
@Setter
@NoArgsConstructor
@Entity
@Table(name="userpermission")
public class UserPermission implements Identifiable {
@Id
@GeneratedValue(generator = "uuid2", strategy = GenerationType.SEQUENCE)
@GenericGenerator(name = "uuid2", strategy = "uuid2", parameters = { @org.hibernate.annotations.Parameter(name = "uuid_gen_strategy_class", value = "org.hibernate.id.uuid.CustomVersionOneStrategy") })
private UUID id;
@NotNull
@Column(length = 100)
private String permission;
@ManyToOne
@JoinColumn(name = "profile_id")
private UserProfile userProfile;
private boolean canChange;
}
I have a REST service that exposes the CRUD services for those entities.
The information is received in a JSON like this:
{
name: 'Profile Name',
permissions: [
{
permission: 'PERMISSION_1',
canChange: true
},
{
permission: 'PERMISSION_2',
canChange: false
}
]
}
Jackson is converting this information and creating my DTOs. After that, using dozer, I map those data to an instance of UserProfile entity.
The problem is, when saving, Hibernate is not setting automatically the userProfile attribute inside UserPermission instances, what gives me that exception:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'profile_id' cannot be null
Is Hibernate suppose to set them automatically? How?
I would like to avoid to have to set manually the userProfile instance in the UserPermission instances. Is there any way of doing this?
Thank you.