The system was updated from Hibernate 4 to Hibernate 5 recently. After that, I notice that Hibernate is trying to add a null value to the "upload date" column and an error is thrown
@Table(name = "mydocument")
@Entity
public class MyDocument {
private static final long serialVersionUID = 1L;
@Getter
@Setter
@Column(columnDefinition = "TEXT")
private String title;
@Getter
@Setter
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
@Column(columnDefinition = "timestamp default current_timestamp")
private DateTime uploadDate;
}
Error
org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
I solved that by adding @DynamicInsert. But the CompanyDocument table has a relationship with the MyDocument table and when the title column is updated that error appears again:
@Entity
@Table(name = "company_document")
@Data
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
public class CompanyDocument {
private static final long serialVersionUID = 1L;
@ManyToOne
@JoinColumn(name = "my_document_id", nullable = true)
private MyDocument myDocument;
}
How can I solve this and why this is happening after the Hibernate upgrade? I tried to add @DynamicUpdate but the code fetch all before instead of calling session.get(..)
@Query("SELECT companyDoc FROM CompanyDocument companyDoc LEFT JOIN FETCH companyDoc.myDocument WHERE companyDoc.id = :id")
CompanyDocument findOneFetchAll(@Param("id") Long id);
Query in MySQL
DESCRIBE myDocument;
Field, Type, Null, Key, Default, Extra
uploadDate,timestamp,NO,"",CURRENT_TIMESTAMP,DEFAULT_GENERATED