0

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

Daniela Morais
  • 2,125
  • 7
  • 28
  • 49
  • You need to be able to read back the value that was inserted into the column, so that it is reflected in the MyDocument. Either flush and refresh the entity, or look for a hibernate equivalent of EclipseLink's @ReturnInsert https://www.eclipse.org/eclipselink/documentation/2.4/jpa/extensions/a_returninsert.htm feature. Why not just set the timestamp in your entity and use that instead of force the database to do it? – Chris Dec 09 '21 at 20:15
  • "update date" = `uploadDate`? [Related question](https://stackoverflow.com/q/54064625/6413377) – pirho Dec 10 '21 at 06:27
  • @pirho It was a typo, I've fixed it – Daniela Morais Dec 10 '21 at 13:19
  • I don't understand what the expected behaviour is. Don't want `uploadDate` to be updatable? Just use `@Column(updatable = false, ...)`. If you also want the `current_timestamp` to always take effect, add `insertable = false`, too – crizzis Dec 12 '21 at 20:45

0 Answers0