gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:19:53 24 4
gpt4 key购买 nike

我正在编写“Spring Roo in Action”一书中的代码,但遇到了问题。根据这本书,我正在生成两个 JPA 实体:Offering 和 Course;当然提供是一对多的依赖关系。

使用 Roo 命令 shell,我生成了两个实体之间的关系:

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;

}

以及类(class)的类代码:

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

现在,当我运行集成测试时,它失败了,类(class)似乎被保留,但产品却没有。应该保存一个 Offer,然后从数据库中检索,但没有返回:

    @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

有人可以告诉我我在这里做错了什么吗?

最佳答案

尝试在持久化之前设置offer.course:在JPA中处理双向关系应该由应用程序完成,而不是由库实现完成。请参阅this page [ getter 和 setter 部分])

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. If your application adds to one side of a relationship, then it must add to the other side.

所以试试这个:

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

祝你好运!

关于java - Spring-Roo JPA 创建的实体未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40235298/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com