Spring JPA审核不起作用

时间:2016-04-26 03:04:26

标签: java mysql spring hibernate jpa

每当我尝试更新实体时,由myn创建的可审核列都会更新。

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

以下是我的java类和persistence.xml

控制器

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

服务

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
public class Contents extends Model {
    @Column(name = "USER_NAME")
    String userName;

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

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

模型类

@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;                
}

可审核模式

@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>

我在这里缺少什么?

0 个答案:

没有答案