我遇到以下错误:我正在使用Spring Boot AOP Javers。
Javers.commit()在另一个类中工作正常,但是在方面类中却给了我下面的错误。
我不知道是否有一种特殊的方式可以同时使用AOP和Javers。
我的代码在这里: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]
我的方面课程
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;
}
}
我的服务等级
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);
}}
我的联系方式
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 + "]";
}
}
我的存储库类
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> {
}
**我的主班**
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);
}}
最佳答案
我在代码中找到了问题,或者我应该说我的一个朋友帮助我找到了这个问题。因此,就我而言,我犯了4个错误:
我使用的pom文件中的1_:javers-core,但我应该使用javers-spring作为依赖项
2_在我的服务类中,我使用了:javax.transaction.Transactional,但是我应该使用org.springframework.transaction.annotation.Transactional
并且我应该将.orElseThrow(EntityNotFoundException :: new)添加到我的函数中,以在对象不存在时避免异常。
3_我必须在Bean javers中添加一个名为JaversConfig的配置脚本。
4_在我的Aspect类中,我需要使用DI进行运输,并且需要使用服务而不是存储库来搜索对象。
我不知道我是否解释得很好,但是您可以在此链接中找到我的工作代码:
https://github.com/mohamedBenali100/JaversAOPIssue
关于java - Javers.commit()无法正常运作MANAGED_CLASS_MAPPING_ERROR,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61867519/