我正在使用Spring Data JPA存储库运行Spring Boot 1.5.1。我已经在我的用户存储库中添加了一个方法,该方法使用了JPA投影(UserProfile),它非常有用。我现在希望在我的服务层中缓存该方法的结果,该层应该返回类型为Page<的结果。 UserProfile>如图所示
JPA Projection。
public interface UserProfile extends Serializable {
long getId();
@Value("#{target.firstname} #{target.othernames}")
String getFullName();
String getFirstname();
String getOthernames();
String getGender();
String getEnabled();
@Value("#{T(System).currentTimeMillis()-target.birthday.getTime()}")
long getBirthday();
}
用户实体。
@Entity
@Cacheable(true)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class User implements Serializable {
private static final long serialVersionUID = 6756059251848061768L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@Column
private String firstname;
@Column
private String othernames;
@Column
private String gender;
@Column
private String photoname;
@Column
private Date birthday;
@Column
private String username;
@Column
private Boolean enabled;
@Column
private String password;
@ElementCollection
private Map<String,String> phonenumbers = new HashMap<String,String>(0);
@JsonBackReference
@OneToMany(cascade = CascadeType.ALL, orphanRemoval=true)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
private List<Address> addresses = new ArrayList<Address>(0);
//Omitted Getters and Setters
@Override
public int hashCode() {...}
@Override
public boolean equals(Object obj) {...}
}
用户存储库。
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
public Page<UserProfile> findAllUserProfilesBy(Pageable pageable);
}
用户服务实施。
@Service
@Transactional(readOnly=true)
public class UserServiceImpl implements UserService {
@Autowired
UserRepository UserRepository;
@Override
@Cacheable("users")
public Page<UserProfile> findAllUserProfiles(Pageable pageable) {
//simulateSlowService();
return UserRepository.findAllUserProfilesBy(pageable);
}
}
然而,当调用服务方法时,我得到以下异常。
java.lang.RuntimeException: Class org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor does not implement Serializable or externalizable
我应该如何缓存服务方法的结果? 非常感谢任何帮助。