[英]Customization of Spring Roo DataOnDemand getNewTransientObject method?
[英]Spring Roo 1.2.4: Roo_DataOnDemand aspect don't populate reference entities
我使用roo shell创建了几个带有测试的实体:
roo> entity jpa --class ~.model.Entity1 --testAutomatically --activeRecord
roo> entity jpa --class ~.model.Entity2 --testAutomatically --activeRecord
...
roo> entity jpa --class ~.model.EntityN --testAutomatically --activeRecord
然后,我开始使用JPA批注(@NotNull @ OneToOne,@ OneToMany等)在我的IDE(IntelliJ想法)中创建它们之间的关系。 在此过程中,我一直在运行集成测试,以验证数据库体系结构。 突然,我的测试之一由于java.lang.NullPointerException而失败。 我发现,roo某种程度上改变了先前生成的Roo_DataOnDemand方面,因此所有引用的实体都被初始化为“ null”。
我可以进行Push-In重构,并手动初始化所有引用实体,但这太慢了。 Roo Aspect Generator发生了什么? 如何解决此问题,以便roo生成器正确初始化所有引用?
看起来Roo的“对导入语句进行分组”有问题,例如:
import javax.persistence.*
为了解决此问题,只需在实体内部展开星号即可。 示例如下。
“ Buggy”实体:
...
import javax.persistence.*;
...
@RooJavaBean
@RooToString
@RooJpaActiveRecord
@RooEquals
@ValidAdvocateCount
public class Formation {
@NotNull
@ManyToOne
private Acol registrationAcol;
/**
*/
@NotNull
@OneToOne
private Contacts contacts;
/**
*/
@OneToMany(cascade = CascadeType.ALL, mappedBy = "formation")
private Set<Advocate> advocates = new HashSet<Advocate>();
}
该实体的自动生成的方面无效(已删除无关的详细信息):
privileged aspect FormationDataOnDemand_Roo_DataOnDemand {
declare @type: FormationDataOnDemand: @Component;
private List<Formation> FormationDataOnDemand.data;
public Formation FormationDataOnDemand.getNewTransientFormation(int index) {
Formation obj = new Formation();
setContacts(obj, index);
setForm(obj, index);
setName(obj, index);
setRate(obj, index);
setRegistrationAcol(obj, index);
setResume(obj, index);
setReviewDate(obj, index);
setViews(obj, index);
return obj;
}
public void FormationDataOnDemand.setContacts(Formation obj, int index) {
Contacts contacts = null;
obj.setContacts(contacts);
}
public void FormationDataOnDemand.setRegistrationAcol(Formation obj, int index) {
Acol registrationAcol = null;
obj.setRegistrationAcol(registrationAcol);
}
}
固定实体导入示例:
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.OneToOne;
import javax.persistence.OneToMany;
import javax.persistence.ManyToOne;
import javax.persistence.CascadeType;
import javax.validation.constraints.NotNull;
修复之后,这是一个有效的自动生成的方面:
privileged aspect FormationDataOnDemand_Roo_DataOnDemand {
declare @type: FormationDataOnDemand: @Component;
private List<Formation> FormationDataOnDemand.data;
@Autowired
ContactsDataOnDemand FormationDataOnDemand.contactsDataOnDemand;
@Autowired
AcolDataOnDemand FormationDataOnDemand.acolDataOnDemand;
public Formation FormationDataOnDemand.getNewTransientFormation(int index) {
Formation obj = new Formation();
setContacts(obj, index);
setForm(obj, index);
setName(obj, index);
setRate(obj, index);
setRegistrationAcol(obj, index);
setResume(obj, index);
setReviewDate(obj, index);
setViews(obj, index);
return obj;
}
public void FormationDataOnDemand.setContacts(Formation obj, int index) {
Contacts contacts = contactsDataOnDemand.getSpecificContacts(index);
obj.setContacts(contacts);
}
public void FormationDataOnDemand.setRegistrationAcol(Formation obj, int index) {
Acol registrationAcol = acolDataOnDemand.getRandomAcol();
obj.setRegistrationAcol(registrationAcol);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.