例如,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
;
当我使用另一个没有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
对象时,UserAggregateRoot
,UserRepository
和EmailRepository
的字段为不是null
;
所以我想知道为什么?这是一个错误吗?