Good morning everyone,
The problem I have is the following:
I have my Spring CRUD to do persistence on my Oracle sql database.
Below I detail:
Class Listener that will control transactions in BBDD:
import java.sql.Timestamp;
import javax.faces.bean.ViewScoped;
import javax.inject.Named;
import javax.persistence.PostPersist;
import javax.persistence.PostRemove;
import javax.persistence.PostUpdate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.support.TransactionSynchronizationAdapter;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import com.minhap.Util.AutowireHelper;
import com.minhap.model.ReportAudit;
import com.minhap.model.User;
import com.minhap.service.ReportAuditService;
@Named
@ViewScoped
public class ReportAuditCtrl {
@Autowired
private ReportAuditService reportAuditService;
@PostUpdate
public void onUpdate(Object object) {
if(object instanceof User) {
User user = (User)object;
AutowireHelper.autowire(this);
System.out.println("Update User :" + user.getName());
ReportAudit reportAudit = new ReportAudit(new Timestamp(System.currentTimeMillis()), "INFO", "Admin", "update in User", "OK", "OK");
reportAuditService.addReportAudit(reportAudit);
}
}
@PostPersist
public void onPersist(Object object) {
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
@Override
public void afterCommit() {
if(object instanceof User) {
User user = (User)object;
AutowireHelper.autowire(this);
System.out.println("Insert User :" + user.getName());
ReportAudit reportAudit = new ReportAudit(new Timestamp(System.currentTimeMillis()), "INFO", "Admin", "update in User", "OK", "OK");
reportAuditService.addReportAudit(reportAudit);
}
}
});
}
@PostRemove
public void onRemove(Object object) {
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
@Override
public void afterCommit() {
if(object instanceof User) {
User user = (User)object;
System.out.println("Remove User :" + user.getName());
}
}
});
}
public ReportAuditService getReportAuditService() {
return reportAuditService;
}
public void setReportAuditService(ReportAuditService reportAuditService) {
this.reportAuditService = reportAuditService;
}
}
Service Interface
import java.util.List;
import com.minhap.model.ReportAudit;
public interface ReportAuditService {
ReportAudit addReportAudit(final ReportAudit reportAudit);
List<ReportAudit>getAllReportAudit();
ReportAudit getReportAuditById(final Long id);
}
Service that implements the interface
import java.util.List;
import javax.inject.Inject;
import org.springframework.stereotype.Service;
import com.minhap.dao.ReportAuditRepository;
import com.minhap.model.ReportAudit;
@Service
public class ReportAuditServiceImpl implements ReportAuditService {
@Inject
private ReportAuditRepository reportAuditRepository;
@Override
public ReportAudit addReportAudit(ReportAudit reportAudit) {
return reportAuditRepository.save(reportAudit);
}
public ReportAuditRepository getReportAuditRepository() {
return reportAuditRepository;
}
public void setReportAuditRepository(ReportAuditRepository reportAuditRepository) {
this.reportAuditRepository = reportAuditRepository;
}
@Override
public List<ReportAudit> getAllReportAudit() {
// TODO Auto-generated method stub
return null;
}
@Override
public ReportAudit getReportAuditById(Long id) {
// TODO Auto-generated method stub
return null;
}
}
Repository
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.minhap.model.ReportAudit;
@Repository("reportAuditRepository")
public interface ReportAuditRepository extends CrudRepository<ReportAudit, Long> {
}
The Entity class
import java.io.Serializable;
import java.sql.Timestamp;
import javax.inject.Named;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
@Entity
@Table(name = "REPORT_AUDIT")
public class ReportAudit implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "REPORT_AUDIT_SEQ")
@SequenceGenerator(sequenceName = "REPORT_AUDIT_SEQ", allocationSize = 20, name = "REPORT_AUDIT_SEQ")
private Long id;
private Timestamp fx_event;
private String tipo;
private String actor;
private String description;
private String event_result;
private String extrainfo;
public ReportAudit() {
super();
}
public ReportAudit(Timestamp fx_event, String tipo, String actor, String description, String event_result,
String extrainfo) {
this.fx_event = fx_event;
this.tipo = tipo;
this.actor = actor;
this.description = description;
this.event_result = event_result;
this.extrainfo = extrainfo;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Timestamp getFx_event() {
return fx_event;
}
public void setFx_event(Timestamp fx_event) {
this.fx_event = fx_event;
}
public String getTipo() {
return tipo;
}
public void setTipo(String tipo) {
this.tipo = tipo;
}
public String getActor() {
return actor;
}
public void setActor(String actor) {
this.actor = actor;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getEvent_result() {
return event_result;
}
public void setEvent_result(String event_result) {
this.event_result = event_result;
}
public String getExtrainfo() {
return extrainfo;
}
public void setExtrainfo(String extrainfo) {
this.extrainfo = extrainfo;
}
}
In the Listener class I use the Service to call the method that the insert in BBDD will do to me. As you can see the primary key of the entity is self-generated with a sequence that previously created in my DB. The table is created equal. Next I attach the scripts of these objects:
CREATE TABLE REPORT_AUDIT (
id INTEGER NOT NULL,
fx_event TIMESTAMP WITH TIME ZONE,
tipo VARCHAR2(20),
actor VARCHAR2(500),
description VARCHAR2(500),
event_result VARCHAR2(500),
extrainfo VARCHAR2(500),
CONSTRAINT reports_audit_pk PRIMARY KEY (id)
);
CREATE SEQUENCE REPORT_AUDIT_SEQ START WITH 1;
The problem is that it does not insert me into the table, but it does not give me any errors in the server console either. (I have enabled traces of logs)
I hope you can help me. To understand a bit about the listener class I have attached another post that I created two days ago: java.lang.NullPointerException when calling Service from Listener class