When an entity has a Spring Data Repository, the LoadTimeWeaving for this entity doesnt work

169 views Asked by At

For example, the User entity :

@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;
    }
}

and it's repository :

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

}

When I create a new User object:

User ob = new User();

the user's userRepository and emailRepository are null, because User and Email have respective Repository classes, UserRepository and EmailRepository;

When I use another class UserAggregateRoot which has no Spring Data JPA Repository, the load time weave works great:

@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 has no Repository, so when I create a new UserAggregateRoot object, the fields of UserAggregateRoot, UserRepository and EmailRepository are not null;

So I would like to know why? Is it a bug?

0

There are 0 answers