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("
本文标签:
版权声明:本文标题:java - issue in saving PostUpdateEventListener,PostInsertEventListener, PostDeleteEventListener, getting duplicate error in case 内容由网友自发贡献,该文观点仅代表作者本人,
转载请联系作者并注明出处:http://www.betaflare.com/web/1745237034a2649099.html,
本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论