admin管理员组

文章数量:1415414

I have the following entity:

@Entity
@Data
@DoAudit
@Table(name = "product")
@EntityListeners(AuditListener.class)
public class Product implements Serializable {

    @Id
    @Column(name = "code", length = 50, nullable = false, unique = true, insertable = false, updatable = false)
    private String code; // Primary Key: code (VARCHAR(50))

    private String name;
    private Double price;
}
CREATE TABLE product (
    code VARCHAR(50) NOT NULL UNIQUE,
    name VARCHAR(255),
    price DOUBLE,
    PRIMARY KEY (code)
);

Whenever I try to save this class, there is no issue.

However, when I try to save the following version:

@Entity
@Data
@DoAudit
@Table(name = "product")
@EntityListeners(AuditListener.class)
public class Product implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private String name;
    private Double price;
}
CREATE TABLE product (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255),
    price DOUBLE
);

I encounter the following error in this class in logChange method issue is with all DB I am using.

@Component
public class AuditListener implements PostUpdateEventListener, PostInsertEventListener, PostDeleteEventListener {

    @Autowired
    private AuditRepository auditRepository;

    @Transactional(Transactional.TxType.REQUIRES_NEW)
    private void logChanges(Object entity, String operation, Object[] oldState, Object[] newState, String[] propertyNames) {
        if (entity instanceof AuditLog) {
            return;
        }

        try {
            AuditLog auditLog = new AuditLog();
            auditLog.setTableName(entity.getClass().getSimpleName());
            auditLog.setOperationType(operation);
            auditLog.setChangedBy("Ankit");
            auditLog.setTimestamp(LocalDateTime.now());
            auditLog.setEntityId("UNKNOWN");
            auditLog.setTraceId("traceId");
            auditLog.setChanges("Changes as per logic");

            auditRepository.save(auditLog);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public void onPostInsert(PostInsertEvent event) {
        System.out.println("

本文标签: