我在一个项目中使用Spring/JPA,我有一个实体在列表上有@onetomany注释,另一个实体有@manytoone注释。尽管当我通过父实体的id检索父实体时,返回的列表(子实体)的大小始终为0
测试的文本版本
@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 = "recommendation")
public class Recommendation extends BusinessEntity {
@ManyToOne
@JoinColumn(name = "employeeid")
private Employee employee;
@OneToMany(mappedBy = "recommendation", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
List<RecommendationImage> recommendationimages;
public Recommendation() {}
public Recommendation(Employee employee, String title, String description, double targetLat, double targetLong,
String street, String city, String country, List<RecommendationImage> images
) {
this.employee = employee;
this.title = title;
this.description = description;
this.targetLat = targetLat;
this.targetLong = targetLong;
this.street = street;
this.city = city;
this.country = country;
this.active = true;
this.recommendationimages = images;
}
@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);
}
}
如有任何帮助,不胜感激,谢谢!
在提供给您的代码中,没有任何地方实际填充reventionimage
的列表。所以我要猜猜。
在处理双向关系时,为了使它们持久化,需要更新关系中没有mappedBy的那一边。我猜您在建议
中添加了reventionimage
,而没有设置reventionimage
的建议
。
通常,您需要这样的方法:
public class Recommendation {
//...
private List<RecommendationImage> images = new ArrayList<>();
public void addRecommendationImage(final RecommendationImage newImage) {
recommendationImages.add(newImage);
newImage.setRecommendation(this);
}
}
public List<RecommendationImage> translateToRecommendationImages(Recommendation recommendation, List<String> rawImages) throws ServiceException {
//try and whatnot
for(String image: rawImages){
recommendation.addRecommendationImage(new RecommendationImage(image));
}
当某个对象具有装箱类型属性时,该属性的getter返回。但是这应该返回,因为boxed type属性的默认值是。这里有什么问题?
查找表示整数为所需的最小位 为什么为值0返回0,难道不应该返回1吗。因为表示0所需的位数是1。 另外,我认为公式中的是一个偏移量
问题内容: 我已经能够验证结果是否为。但是,它不返回列表。为什么? 问题答案: 对列表进行适当排序,即不返回新列表。写吧
而且 不是应该都返回吗?它不是基元变量,在第二个代码中,即使在添加零之后,它也会打印。我知道装箱(对于从-128到127的整数),但是为什么装箱在第二段代码中起作用而不是在第一段代码中起作用呢?
我正在解决以下Leetcode问题:https://leetcode.com/problems/maximum-depth-of-binary-tree/solution/ 返回二叉树的最大深度。 这是我的解决方案: 由于某种原因,输出总是比预期的少一个。看看公认的解决方案,它们看起来和我的非常相似,但我似乎找不到我的解决方案出了什么问题。
我试图使用JPA findAll获取所有记录。如果我在终端中运行相同的查询,结果会得到一些行,但不是通过JPA。我在stackoverflow上尝试了其他答案,但都不起作用。我尝试添加公共getter和setter,尽管我假设这是通过注释完成的。 型号类别: 服务等级: 编辑:添加存储库代码: