2

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.

  • 3
    Your model coming in from JSON has a null UserPermission.userProfile reference, so that is what is inserted into the foreign key. Hibernate/JPA will not set the back references for you on bidirectional relationships, so you'll have to fix this reference yourself, or not map it as a bidirectional OneToMany. You could just make it unidirectional and leave off the UserPermission.userProfile reference and mapping. see https://stackoverflow.com/questions/12038380/how-to-define-unidirectional-onetomany-relationship-in-jpa – Chris Oct 03 '17 at 18:36
  • If I want it bidirectional, is there any way of doing this 'injection' automatically? – Mauricio Santos Oct 03 '17 at 18:40
  • you'll have to check with your JSON->object tool options to see if it can do it for you, or manually make the changes in your code before passing the model on. – Chris Oct 04 '17 at 15:46
  • Either a mandatory peice of information (user profile) is missing, thus you need to check with the producer of this JSON, else, -and I think more logic- It's a unidirectional many to many relation between user profile and permission ... I say this is more logic because usually permission is something independent , but it all depends upon your domain. ... this is very similar to what @chris pointed out – osama yaccoub Oct 04 '17 at 16:10
  • You are right. In my domain no need a bidirectional relation. Other point is that after some search (and your response) I conclude that Hibernate it is not suppose to inject userProfile in userPermission on a bidirectional relation. If I maintain this as a bidirecional, I will have to set it manually. Thank you guys! – Mauricio Santos Oct 04 '17 at 20:01

0 Answers0