Restriction error in relationship Onetomany Hibernate

Asked

Viewed 259 times

0

I’m getting error in the Onetomany relationship when I try to persist an entity.

Error:

org.postgresql.util.PSQLException: ERROR: null value in column "book_url_id" violates not-null constraint Detalhe: Failing row contains (2, name, pattern, temp, type, null).

Bookurl.java:

@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "book_urls")
public class BookUrlEntity implements Serializable {

    private static final long serialVersionUID = 1216319313385073856L;

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

    @NotNull
    private String url;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "bookUrl")
    private Set<BookUrlParameterEntity> parameters;
}

Bookurlparameterentity.java

@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "book_url_parameters")
public class BookUrlParameterEntity implements Serializable{

    private static final long serialVersionUID = -1388929732076588240L;

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

    @NotNull
    @Column(name = "variable_name")
    private String variableName;

    @NotNull
    @Column(name = "variable_type")
    private String variableType;

    @NotNull
    @Column(name = "variable_pattern")
    private String variablePattern;

    @Column(name = "variable_temp_value")
    private String variableTempValue;

    @ManyToOne
    @JoinColumn(nullable = false, updatable = false, insertable = true)
    private BookUrlEntity bookUrl;
}

I’m trying to persist that way:

BookUrlEntity book = BookUrlEntity.builder().url("www.com.br").build();
BookUrlParameterEntity p1 = BookUrlParameterEntity.builder()
        .variableName("asd")
        .variablePattern("asd")
        .variableType("asda")
        .build();
BookUrlParameterEntity p2 = BookUrlParameterEntity.builder()
        .variableName("asd")
        .variablePattern("asd")
        .variableType("asdw")
        .build();
BookUrlParameterEntity p3 = BookUrlParameterEntity.builder()
        .variableName("asd")
        .variablePattern("asd")
        .variableType("asdq")
        .build();
Set<BookUrlParameterEntity> params = new HashSet<BookUrlParameterEntity>();
params.add(p1);
params.add(p2);
params.add(p3);
book.setParameters(params);

bookUrlRepository.save(book);

I’ve looked elsewhere about relationships, but it’s still a little confusing to me, which I’m starting with Spring, Hibernate.

  • The error is very clear, by persisting BookUrlParameterEntity the field bookUrl is null and violates the constraint that a foreign key cannot be null. You need to debug and see why this object is going null

  • 1

    Exactly in this part that I am not succeeding. I do not understand why the ID is not assigned in the table of parameters.

1 answer

1


In the code you added we can see the problem, the objects P1, P2 and P3 are in the field bookUrl null. Add the object bookvariables P1, P2 and P3. would be something like:

BookUrlParameterEntity p1 = BookUrlParameterEntity.builder()
        .variableName("asd")
        .variablePattern("asd")
        .variableType("asda")
        .bookUrl(book)
        .build();

The same with P2 and P3.

As you are using Lombok, and have a bidirectional relationship you need to delete the field bookUrl in class Bookurlparameterentity with this annotation:

@EqualsAndHashCode(exclude="bookUrl")

For more information: Documentation @Equalsandhashcode

  • 1

    When I do that, it makes a mistake java.lang.StackOverflowError at com.teste.manager.models.entities.BookUrlEntity.hashCode(BookUrlEntity.java:30) at com.teste.manager.models.entities.BookUrlParameterEntity.hashCode(BookUrlParameterEntity.java:24) at java.util.AbstractSet.hashCode(AbstractSet.java:126). It looks like he goes into some kind of loop

  • It works if I save Bookurl first. But that would be the right approach?

  • 1

    I’m not familiar with Lombok but you have a bidirectional dependency between BookUrlEntity and BookUrlParameterEntity. Error happens in Equals and Hashcode com.teste.manager.models.entities.BookUrlParameterEntity.hashCode by what I researched you can add this annotation @EqualsAndHashCode(exclude="bookUrl") in class BookUrlParameterEntity. doc @Equalsandhashcode explains why stackoverflow error

  • 1

    @Diegohenrique How you wrote down the field parameters with CascadeType.ALL, correct approach is to save the same bookurl.

  • 1

    I added the Annotation Equalsandhashcode and fixed the problem. Could you update the answer so I can accept it? Thank you so much for your help

  • @Diegohenrique For nothing, updated !

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.