I am getting below error: and I am working with Spring Boot AOP Javers. Javers.commit() works fine in another class but in aspect class it gave me below error. i don't know if there is a special way to work with AOP and Javers at the same time.
my code is here : https://github.com/mohamedBenali100/JaversAOPIssue
org.javers.common.exception.JaversException: MANAGED_CLASS_MAPPING_ERROR: given javaClass 'class java.lang.Long' is mapped to PrimitiveType, expected ManagedType
at org.javers.core.metamodel.type.TypeMapper.getJaversManagedType(TypeMapper.java:194) ~[javers-core-5.9.0.jar:na]
My Aspect Class
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.javers.core.Javers;
import org.javers.core.JaversBuilder;
import org.javers.repository.jql.QueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class ContactAspect {
Javers javers = JaversBuilder.javers().registerValueObject(Contact.class).build();
@Autowired
ContactRepository contactRepository;
@Pointcut(value = "execution(* ma.dxc.ContactServiceImpl.save(..))")
public void mySavePointcut(){ }
@Pointcut(value = "execution(* ma.dxc.ContactServiceImpl.update(..)) && args(id,contact,..)")
public void myUpdatePointcut(Long id, Contact contact){ }
@AfterReturning(pointcut = "mySavePointcut()",returning= "result")
public void logAfterReturningUsers(JoinPoint joinPoint, Object result) throws Throwable{
Contact contact = (Contact) result;
System.out.println("contact added : "+contact.toString());
}
@Around("myUpdatePointcut(id,contact)")
public Object applicationLogger (ProceedingJoinPoint proceedingJoinPoint, Long id, Contact contact) throws Throwable {
Contact contactAudited = contactRepository.getOne(id);
javers.commit("hamada",contactAudited);
Object object = proceedingJoinPoint.proceed();
contactAudited = contactRepository.getOne(id);
javers.commit("hamada",contactAudited);
String changes = javers.findChanges( QueryBuilder.byInstance(contactAudited).build()).toString();
System.out.println("contact updated by this changes : "+changes);
return object;
}
}
My Service Class
import java.util.List;
import javax.transaction.Transactional;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ContactServiceImpl{
@Autowired
private ContactRepository contactrepository ;
public List<Contact> findAll() {
return contactrepository.findAll();
}
public Contact save(@Valid Contact contact) {
return contactrepository.save(contact) ;
}
public Contact findOne(long id) {
return contactrepository.getOne(id);
}
@Transactional
public Contact update(@Valid Long id, Contact contact) {
contact.setId(id);
return contactrepository.saveAndFlush(contact);
}}
My Contact Class
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
@Entity
public class Contact implements Serializable {
private static final long serialVersionUID = 1L;
@Id @GeneratedValue
private Long id ;
@NotNull
@Size
private String Fname;
@NotNull
@Size
private String Lname;
public Contact() {
super();
}
public Contact(@NotNull @Size String fname, @NotNull @Size String lname) {
super();
Fname = fname;
Lname = lname;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFname() {
return Fname;
}
public void setFname(String fname) {
Fname = fname;
}
public String getLname() {
return Lname;
}
public void setLname(String lname) {
Lname = lname;
}
@Override
public String toString() {
return "Contact [Fname=" + Fname + ", Lname=" + Lname + "]";
}
}
my repository class
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;
@Repository
public interface ContactRepository extends JpaRepository<Contact, Long>,JpaSpecificationExecutor<Contact> {
}
** my Main Class **
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ContactApplication implements CommandLineRunner {
@Autowired
ContactServiceImpl contactServiceImpl;
public static void main(String[] args) {
SpringApplication.run(ContactApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
Contact contact = new Contact("tom","hunx");
contactServiceImpl.save(contact);
contact.setLname("cruise");
contactServiceImpl.update(Long.valueOf(1), contact);
}}
I found the issue in my code or i shuld say a friend of mine who helped me to find it. So in my case there is 4 mistakes i did :
1_ in pom file i used : javers-core but i shuld use javers-spring as dependency
2_ In my service class i used : javax.transaction.Transactional but i shuld use org.springframework.transaction.annotation.Transactional and i shuld add .orElseThrow(EntityNotFoundException::new) to my functions to evoid exception when your object is not there.
3_ I had to add a configuration calss named JaversConfig with Bean javers.
4_ In my Aspect class i needed to use DI for javers and needed to use service instead of repository for searching for an object.
I don't know if i explainted that well but you will find my working code in this link: https://github.com/mohamedBenali100/JaversAOPIssue