gpt4 book ai didi

java - 如何使用 JpaRepository 获取嵌套对象列表?

转载 作者:行者123 更新时间:2023-11-29 08:36:25 25 4
gpt4 key购买 nike

我无法使用 JpaRepository 获取嵌套对象的列表。我将尝试使用以下代码解释我想要的内容:

AutoService实体:

@Entity
public class AutoService {
@Id
private long id;

@Column(name = "serviceName", nullable = false)
private String serviceName;
}

服务实体:

@Entity
public class Service {
@Id
private long serviceId;

@Column(name = "serviceName", nullable = false)
private String serviceName;

@Column(name = "category", nullable = false)
private String category;

@ManyToOne
@JoinColumn(name = "autoServiceId", nullable = false)
private AutoService autoService;
}

ServiceRepository接口(interface):

public interface ServiceRepository extends JpaRepository<Service, Long> {
List<Service> findByServiceNameAndCategory(String autoServiceName, String categoryName);
}

业务逻辑:

@org.springframework.stereotype.Service
public class ServiceServiceImpl implements ServiceService {
@Autowired
private ServiceRepository serviceRepository;

@Override
public List<Service> findByAutoServiceAndCategory(String autoServiceName, String serviceCategory) {
return serviceRepository.findByServiceNameAndCategory(autoServiceName, serviceCategory);
}
}

如我所料,上面的代码无法提供与提供的类别和 AutoService 名称相匹配的所需 Service 列表。

有人可以就如何使用我的存储库获取嵌套服务列表提供建议吗:autoServiceNameserviceCategory 请问?

编辑:

现在我正在使用自定义查询。

我现在使用的是 autoServiceId 而不是服务名称。

但由于某种原因,我得到的对象列表是空的。

这是我的 JPA repo 协议(protocol)。

public interface ServiceRepository extends JpaRepository<Service, Long> {
@Query("SELECT s from Service s where s.autoService.id = :autoServiceId and s.category = :categoryName")
List<Service> findByServiceNameAndCategory(@Param("autoServiceId") Long autoServiceId, @Param("categoryName") String categoryName);
}

有什么建议吗?

我想我知道答案了。问题在我的类别中,发送到服务器。我用俄语写的。并在服务器端对类别的破损值进行编码。 enter image description here

最佳答案

1- 在您的实体对象上相应地使用 @Embedded@Embeddable 注释,然后您的方法将获取嵌套对象。

2- @Query 注释用于编写自定义查询,请引用此链接 custom query reference

关于java - 如何使用 JpaRepository 获取嵌套对象列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43841608/

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