1

例如,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类,UserRepository并且EmailRepository;

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

@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的字段不是;UserAggregateRootUserRepositoryEmailRepositorynull

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

4

0 回答 0