当实体具有Spring Data Repository时,此实体的LoadTimeWeaving不起作用

时间:2017-11-13 02:31:53

标签: spring hibernate spring-data-jpa aspectj aspects

例如,User实体:

@Configurable
@Entity
@Data
@EqualsAndHashCode(callSuper = false)
@DynamicInsert
@DynamicUpdate
public class User extends AbstractAggregateRoot<User> implements Serializable {

    private static final long serialVersionUID = 1L;

    @Autowired
    @Transient
    private UserRepository userRepository;

    @Autowired
    @Transient
    private EmailRepository emailRepository;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String nickname;

    @Version
    @Column(columnDefinition = "timestamp")
    @Source(value = SourceType.DB)
    private Timestamp ts;

    public void doSavedComplete() {
        UserSavedEvent event = new UserSavedEvent();
        event.setUser(this);
        event.setTs(new Date());
        andEvent(event);
        userRepository.save(this);
    }

    @Data
    public static class UserSavedEvent {
        private User user;
        private Date ts;
    }
}

及其存储库:

@Repository
public interface UserRepository extends JpaRepository<User, Long> {

}

当我创建一个新的User对象时:

User ob = new User();

用户userRepositoryemailRepositorynull, 因为UserEmail分别有Repository个类,UserRepositoryEmailRepository;

当我使用另一个没有Spring Data JPA Repository的类UserAggregateRoot时,加载时间编织效果很好:

@Configurable
@Entity
public class UserAggregateRoot extends AbstractAggregateRoot<User> implements Serializable {
    @Autowired
    @Transient
    private UserRepository userRepository;

    @Autowired
    @Transient
    private EmailRepository emailRepository;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String nickname;

    @Version
    @Column(columnDefinition = "timestamp")
    @Source(value = SourceType.DB)
    private Timestamp ts;
}

UserAggregateRoot没有Repository,因此当我创建新的UserAggregateRoot对象时,UserAggregateRootUserRepositoryEmailRepository的字段为不是null;

所以我想知道为什么?这是一个错误吗?

0 个答案:

没有答案