0

Whenever I am trying to update the entity, the auditable column created by doesn't get updated.

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'CREATED_BY' cannot be null

The following are my java classes and persistence.xml

Controller

@RequestMapping(value = "/insertContents", method = RequestMethod.POST)
    @ResponseBody
    public void insertContents(@RequestBody InsertContentsRequest insertContentsRequest) {
        contentsDataService.insertContents(insertContentsRequest);
    }

Service

public void insertContents(InsertContentsRequest insertContentsRequest) {
        Contents contents = new Contents();

        String contentsFromRequest = insertContentsRequest.getContents();   
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");

        java.sql.Date contentDate = null;

        if(null == insertContentsRequest.getContentDate()) {
            contentDate = new Date(System.currentTimeMillis());
        }
        else {
            try {
                contentDate  = (Date) sdf.parse(insertContentsRequest.getContentDate());
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        contents.setContent(contentsFromRequest);
        contents.setContentDate(contentDate);
        contentsRepository.save(contents);
    }

Entity class

@Entity
public class Contents extends Model {
    @Column(name = "USER_NAME")
    String userName;

    @Column(name = "CONTENT")
    String content;

    @Column(name = "CONTENT_DATE")
    Date contentDate;   
}

Model class

@MappedSuperclass
@SuppressWarnings("serial")
public abstract class Model extends AuditableModel {           
    private static final long serialVersionUID = 1L;
    public static final int ID_LENGTH = 36;
    public static final int ENUM_LENGTH = 256;

    @Autowired(required = true)
    @Id
    @Column(name = "ID", updatable = false)
    @Size(max = ID_LENGTH)
    @GeneratedValue(generator = "UuidOrAssignedGenerator")
    @GenericGenerator(name = "UuidOrAssignedGenerator", strategy = "com.dragonfly.timemachine.util.jpa.UuidOrAssignedGenerator", parameters = { @Parameter(name = "strategy", value = "uuid2") })
    private String id;                
}

Auditable Model

@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class AuditableModel {              
    @Column(name = "CREATED_BY", updatable = false, nullable = false)
    @Size(min = 1, max = 255)
    @CreatedBy
    private String createdBy;

    @Column(name = "CREATED_DATE", updatable = false)
    @CreatedDate
    private DateTime createdDate;

    @Column(name = "UPDATED_BY")
    @Size(min = 1, max = 255)
    @LastModifiedBy
    private String lastModifiedBy;

    @Column(name = "UPDATED_DATE")
    @LastModifiedDate
    private DateTime lastModifiedDate;

     }        
}

Persistence.xml

<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="timeMachine">
        <class>com.dragonfly.timemachine.domain.Contents</class>
        <class>com.dragonfly.timemachine.domain.Credentials</class>
        <class>com.dragonfly.timemachine.domain.Model</class>
        <class>com.dragonfly.timemachine.domain.AuditableModel</class>
    </persistence-unit>
</persistence>

What am I missing here?

Arun
  • 1,176
  • 5
  • 20
  • 48
  • Did you try the jpa as in this [thread](http://stackoverflow.com/questions/6450689/how-to-use-auditing-in-jpa-spring-data-jpa?rq=1) – randominstanceOfLivingThing Apr 26 '16 at 03:20
  • 1
    It seems your Contents is not set and is null. The error here "'CREATED_BY' cannot be null". I don't see a place that you set it. Can you double it?! – Kenny Tai Huynh Apr 26 '16 at 04:43
  • 1
    Actually AuditColumns are supposed to be autogenerated and set explicitly. "Created by" comes from AudiAwareImpl where I have given what to set. – Arun Apr 26 '16 at 14:46
  • @Arun - Were you able to solve this issue ? I'm also facing the same issue created here: https://stackoverflow.com/questions/56823730/spring-data-jpa-auditing-not-working-for-the-jparepository-update-method-with-m. – PAA Jun 30 '19 at 09:27

0 Answers0