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 fieldbookUrl
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– Viktor Hugo
Exactly in this part that I am not succeeding. I do not understand why the ID is not assigned in the table of parameters.
– Diego Henrique