简体   繁体   English

Spring-Roo JPA创建的实体未更新

[英]Spring-Roo JPA created entity not updating

I'm working on code from the book 'Spring Roo in Action', and I'm running into a problem. 我正在研究“ Spring Roo in Action”一书中的代码,但遇到了问题。 As per the book, I am generating two JPA entities, Offering and Course; 根据这本书,我正在生成两个JPA实体,即Offer和Course。 course to offering is a one-to-many dependency. 提供的过程是一对多的依赖。

Using the Roo command shell, I have generated the relationship between the two entities thus: 使用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

This has generated the class code Offering with annotations: 这样就生成了带有注释的类代码Offer:

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

} }

And class code for 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;
}

Now, when I run the integration test , it fails, it seems that the Course is persisted, but the Offering isn't. 现在,当我运行集成测试时,它失败了,看来该课程仍然存在,但产品没有。 One Offer should have been persisted and then retrieved from the database, but none are returned: 一个要约应该已经保存,然后从数据库中检索,但是没有返回:

    @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 断言失败

Could somebody please advise what I am doing wrong here? 有人可以告诉我我在做什么错吗?

Try to set offer.course before persist: In JPA handle bidirectional relations should be done by application, not by library implementation. 尝试在持久之前设置offer.course :在JPA处理中,双向关系应由应用程序而不是库实现来完成。 See this page [ Getters and Setters section] ) 参见此页 [ Getters and Setters部分])

As the relationship is bi-directional so as the application updates one side of the relationship, the other side should also get updated, and be in sync. 由于关系是双向的,因此当应用程序更新关系的一侧时,另一侧也应更新并保持同步。 In JPA , as in Java in general it is the responsibility of the application, or the object model to maintain relationships . 在JPA中 ,就像在Java中一样,通常由应用程序或对象模型负责维护关系 If your application adds to one side of a relationship, then it must add to the other side. 如果您的应用程序添加到关系的一侧,则它必须添加到关系的另一侧。

So try this: 所以试试这个:

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

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM