2016-10-25 104 views
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()); 
    } 
} 

Assertion Failure

可能有人請指教一下我做錯了嗎?

+0

您使用的是哪個Roo版本?請描述一下你的環境。 – eruiz

+0

對不起,在STS 3.8.1版上運行的Spring Roo版本1.3.2 – Sotades

+0

另外後端數據庫是windows-x64上的postgresql版本9.6.0-rc1。 – Sotades

回答

1

在persist之前嘗試設置offer.course:在JPA句柄中,雙向關係應該由應用程序完成,而不是由庫實現。見this page [Getter和Setter節])

由於工作的關係是雙向的,以便關係的應用程序更新一側,另一側也應得到更新,並且在同步。 在JPA中,與Java中的一般一樣,應用程序或對象模型負責維護關係。如果您的應用程序添加到關係的一側,那麼它必須添加到另一側。

那麼試試這個:

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

    // ================================== 
    offer.setCourse(course); // XXX set the offer-side relationship part 
    // ================================== 

    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()); 
} 

祝你好運!