Java Spring/JPA@OneToMany关系返回一个大小为0的列表,而它应该返回更多。为什么?
我在一个项目中使用spring/jpa,我有一个实体在列表上有@OneToMany注释,另一个实体有@ManyToOne注释。虽然当我通过父实体的id检索父实体时,返回的列表(子实体)的大小始终为0 测试的文本版本Java Spring/JPA@OneToMany关系返回一个大小为0的列表,而它应该返回更多。为什么?,java,spring,jpa,Java,Spring,Jpa,我在一个项目中使用spring/jpa,我有一个实体在列表上有@OneToMany注释,另一个实体有@ManyToOne注释。虽然当我通过父实体的id检索父实体时,返回的列表(子实体)的大小始终为0 测试的文本版本 @Test public void createReccomendation() throws ServiceException, FileNotFoundException, UserDoesNotExistException, UserNameIsNotUniqueExcepti
@Test
public void createReccomendation() throws ServiceException, FileNotFoundException, UserDoesNotExistException, UserNameIsNotUniqueException, IllegalUserNameException {
String uid = UUID.randomUUID().toString();
Employee employee = employeeService.createEmployee("hi", uid+"@me.com", uid, "secret password", 23.234, 23.23, "image", "23dAD", "seattle", "usa");
List<String> images = new ArrayList<>();
String image1 = "image1/url";
String image2 = "image2/url";
String image3 = "image3/url";
images.add(image1);
images.add(image2);
images.add(image3);
Employee e = employeeService.getEmployeeById(employee.getId());
Recommendation rec = recommendationService.createRecommendation(e.getId(), "title", "Description", 23.23, 23.23, "persikogatan", "stockholm", "Sweden", images);
Recommendation rec2 = recommendationService.getRecommendationById(rec.getId());
Assert.assertEquals(rec.getTitle(), "title");
Assert.assertEquals(rec.getRecommendationimages().get(0).getPath(), image1);
Assert.assertEquals(3, rec2.getRecommendationimages().size());
}
子实体的超类
@Entity
@Table(name = "recommendationimage")
public class RecommendationImage extends ImageEntity{
@ManyToOne
private Recommendation recommendation;
@MappedSuperclass
public abstract class ImageEntity extends BaseEntity{
@Column(name="path", length=700)
String path;
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}
推荐服务类
@Service
public class RecommendationService{
private static final Logger log = LogManager.getLogger(DealService.class);
@Autowired
RecommendationRepository recommendationRepository;
public Recommendation getRecommendationById(Long id){
return recommendationRepository.findOne(id);
}
}
public List<RecommendationImage> translateToRecommendationImages(List<String> rawImages) throws ServiceException {
try{
List<RecommendationImage> recommendationImages = new ArrayList<>();
for(String image: rawImages){
RecommendationImage newImage = new RecommendationImage(image);
recommendationImages.add(newImage);
}
return recommendationImages;
}catch(Exception e){
log.warn("** SERVICE EXCEPTION ** FOR METHOD: translateRecommendationImages()");
throw new ServiceException("Could not translate raw images to RecommendationImage", e);
}
}
public Recommendation createRecommendation(Long employeeId, String title, String description, double targetLat,
double targetLong, String street, String city, String country,
List<String> rawImages) throws ServiceException {
log.info("createRecommendation(): employeeId: "+employeeId+" recommendationTitle: "+title);
Employee employee=null;
Recommendation rec=null;
String imagepath=null;
try {
List<RecommendationImage> images = translateToRecommendationImages(rawImages);
employee = employeeRepository.getEmployeeByid(employeeId);
rec = new Recommendation(employee, title, description, targetLat, targetLong, street, city, country, images);
// Recommendation rec2 = CheckProximity.getRecommendationProximity(rec, employee);
return recommendationRepository.save(rec);
}catch(Exception e){
log.warn("** SERVICE EXCEPTION ** FOR METHOD: createRecommendation(): employeeId: "+employeeId);
e.printStackTrace();
throw new ServiceException("Could not create recommendation at this time: "+rec.toString(), e);
}
}
@服务
公共类推荐服务{
私有静态最终记录器log=LogManager.getLogger(DealService.class);
@自动连线
推荐存储库推荐存储库;
公共推荐getRecommendationById(长id){
返回recommendationRepository.findOne(id);
}
}
公共列表TranslateRecommendationImages(列表rawImages)引发ServiceException{
试一试{
List recommendationImages=new ArrayList();
用于(字符串图像:原始图像){
RecommendationImage newImage=新的RecommendationImage(图像);
推荐图片。添加(新图片);
}
返回推荐图片;
}捕获(例外e){
log.warn(“**服务异常**针对方法:translateRecommissionImages()”;
抛出新的ServiceException(“无法将原始图像转换为推荐图像”,e);
}
}
公共推荐CreateRecommension(长employeeId、字符串标题、字符串描述、双targetLat、,
双目标长,字符串街,字符串城市,字符串国家,
列表(图像)引发ServiceException{
log.info(“CreateRecommension():employeeId:+employeeId+”recommendationTitle:+title”);
Employee=null;
建议rec=null;
字符串imagepath=null;
试一试{
列表图像=翻译推荐图像(原始图像);
employee=employeeRepository.getEmployeeByid(employeeId);
rec=新推荐(员工、职位、描述、目标地、目标地、街道、城市、国家、图像);
//RecommendationRec2=CheckProximity.GetRecommendationProximition(rec,employee);
返回recommendationRepository.save(rec);
}捕获(例外e){
log.warn(“**服务异常**针对方法:CreateRecommension():employeeId:”+employeeId);
e、 printStackTrace();
抛出新的ServiceException(“此时无法创建建议:+rec.toString(),e);
}
}
任何帮助都将不胜感激,谢谢 提供给您的代码中没有任何地方实际填充了
RecommendationImage
s的列表。所以我猜
在处理双向关系时,为了持久化它们,需要更新不带mappedBy的关系的一侧。我猜您在推荐
中添加了推荐图像
s,而没有设置推荐图像
的推荐
通常,您需要这样一种方法:
public class Recommendation {
//...
private List<RecommendationImage> images = new ArrayList<>();
public void addRecommendationImage(final RecommendationImage newImage) {
recommendationImages.add(newImage);
newImage.setRecommendation(this);
}
}
提供给您的代码中没有任何地方实际填充了
RecommendationImage
s的列表。所以我猜
在处理双向关系时,为了持久化它们,需要更新不带mappedBy的关系的一侧。我猜您在推荐
中添加了推荐图像
s,而没有设置推荐图像
的推荐
通常,您需要这样一种方法:
public class Recommendation {
//...
private List<RecommendationImage> images = new ArrayList<>();
public void addRecommendationImage(final RecommendationImage newImage) {
recommendationImages.add(newImage);
newImage.setRecommendation(this);
}
}
若测试用例也是文本,而不是图像,那个么事情会变得容易一些。这些员工和推荐服务是Spring生成的还是您自己制作的(sry,询问原因不太熟悉Spring Stuff)?@Dan,你能用一个所有代码都可见的屏幕截图来替换屏幕截图吗?@Eugene我已经用测试的文本版本替换了图像。@pirho我用文本替换了图像,并显示了推荐服务,使内容更易于阅读。它只调用一个findOne方法,该方法的实现由JPACan提供。我们可以看到BusinessEntity类,以及Employee实体及其超类(如果有的话)。它可能与ID有关。如果测试用例也是文本,而不是图像,这将使事情变得更容易。这些员工和推荐服务是Spring生成的还是您自己制作的(sry,询问原因不太熟悉Spring Stuff)?@Dan,你能用一个所有代码都可见的屏幕截图来替换屏幕截图吗?@Eugene我已经用测试的文本版本替换了图像。@pirho我用文本替换了图像,并显示了推荐服务,使内容更易于阅读。它只调用一个findOne方法,该方法的实现由JPACan提供。我们可以看到BusinessEntity类,以及Employee实体及其超类(如果有的话)。这可能与ID有关。谢谢,我添加了缺失的代码。我要做的是在建议的构造函数中设置列表。所以应该是这样设置的。等等,看起来你真的将推荐(父对象)设置为RecommendationImage(子对象),我没有这样做。我将列表设置为推荐,但是您的示例建议我应该做相反的事情,我将尝试一下。实际上,“parent”是没有“mappedBy”属性的一方。这一侧的填充内容决定了存储的内容。不过,整个父子关系变得有点奇怪,因为它只是支持mappedBy的OneToMany注释。谢谢你做到了这一点。我没有意识到我必须在每个RecommendationImage中实际设置员工,并将列表设置为员工。我以为级联会解决前者。但这为我解决了问题。谢谢谢谢,我已经添加了丢失的代码。什么
public List<RecommendationImage> translateToRecommendationImages(Recommendation recommendation, List<String> rawImages) throws ServiceException {
//try and whatnot
for(String image: rawImages){
recommendation.addRecommendationImage(new RecommendationImage(image));
}