0
我正在研究「Spring Roo in Action」一書中的代碼,我遇到了一個問題。根據這本書,我產生了兩個JPA實體,Offering和Course;提供的過程是一對多的依賴關係。Spring-Roo JPA創建的實體不更新
使用Roo的命令殼,我已經產生因而在兩個實體之間的關係:
focus --class ~.model.Course
field set --fieldName offerings --type ~.model.Offering --cardinality ONE_TO_MANY --mappedBy "course"
focus --class ~.model.Offering
field reference --fieldName course --type ~.model.Course --cardinality MANY_TO_ONE
這已生成的類代碼註釋提供:
@RooJavaBean
@RooToString
@RooJpaActiveRecord
public class Offering {
/**
*/
@NotNull
@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(style = "M-")
private Date offerDate;
/**
*/
@NotNull
@Size(min = 1, max = 80)
private String locationName;
/**
*/
@ManyToOne
private Course course;
}
課程編號:
@RooJavaBean
@RooToString
@RooJpaActiveRecord
public class Course {
/**
*/
private String name;
/**
*/
private BigDecimal listPrice;
/**
*/
private String description;
/**
*/
private Integer maximumCapacity;
/**
*/
@Temporal(TemporalType.DATE)
@DateTimeFormat(style = "S-")
private Date runDate;
/**
*/
@OneToMany(cascade = CascadeType.ALL, mappedBy = "course")
private Set<Offering> offerings = new HashSet<Offering>();
/**
*/
@ManyToOne
private TrainingProgram trainingProgram;
}
現在,當我運行集成測試時,它失敗了,看起來課程仍然存在,但發售並非如此。一個提議應該被堅持,然後從數據庫中檢索,但沒有返回:
@Test
public void addCourseAndOffering(){
CourseDataOnDemand courseDod = new CourseDataOnDemand();
Course course = courseDod.getNewTransientCourse(0);
course.setListPrice(new BigDecimal("100.00"));
OfferingDataOnDemand offerDod = new OfferingDataOnDemand();
Offering offer = offerDod.getNewTransientOffering(0);
course.getOfferings().add(offer);
course.persist();
course.flush();
course.clear();
Course persistedCourse = Course.findCourse(course.getId());
Assert.assertNotNull(persistedCourse.getId());
Assert.assertEquals(course.getListPrice(), persistedCourse.getListPrice());
Set<Offering> offers = persistedCourse.getOfferings();
int size = offers.size();
Assert.assertEquals(1, persistedCourse.getOfferings().size());
}
}
可能有人請指教一下我做錯了嗎?
您使用的是哪個Roo版本?請描述一下你的環境。 – eruiz
對不起,在STS 3.8.1版上運行的Spring Roo版本1.3.2 – Sotades
另外後端數據庫是windows-x64上的postgresql版本9.6.0-rc1。 – Sotades