例如,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();
用户的userRepository
和emailRepository
是null
,因为User
和Email
有各自的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
的字段不是;UserAggregateRoot
UserRepository
EmailRepository
null
所以我想知道为什么?它是一个错误吗?