Java 如何使用JpaRepository获取嵌套对象列表?
我无法使用Java 如何使用JpaRepository获取嵌套对象列表?,java,spring-data-jpa,Java,Spring Data Jpa,我无法使用JpaRepository获取嵌套对象的列表。我将尝试使用以下代码解释我想要什么: 自动服务实体: @Entity public class AutoService { @Id private long id; @Column(name = "serviceName", nullable = false) private String serviceName; } @Entity public class Service { @Id
JpaRepository
获取嵌套对象的列表。我将尝试使用以下代码解释我想要什么:
自动服务实体:
@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;
}
服务实体:
@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界面:
public interface ServiceRepository extends JpaRepository<Service, Long> {
List<Service> findByServiceNameAndCategory(String autoServiceName, String categoryName);
}
有什么建议吗
我想我知道答案。“我的类别”中存在问题,已发送到服务器。我用俄语写的。并在服务器端对类别的断开值进行编码。
1-在实体对象上相应地使用
@Embedded
和@Embedded
注释,然后方法将获取嵌套对象
或
2-
@Query
注释用于编写自定义查询请参考此链接您可能需要在服务存储库中编写类似的查询
public interface ServiceRepository extends JpaRepository<Service, Long> {
@Query("SELECT s from Service s where s.autoService.serviceName = :autoServiceName and s.category = :categoryName")
Set<Round> getRoundsBySessionQuestionId(@Param("autoServiceName") String autoServiceName, @Param("categoryName") String categoryName);
}
public interface servicepository扩展了JpaRepository{
@查询(“从服务s中选择s,其中s.autoService.serviceName=:autoServiceName和s.category=:categoryName”)
设置getRoundsBySessionQuestionId(@Param(“autoServiceName”)字符串autoServiceName,@Param(“categoryName”)字符串categoryName);
}
希望这有帮助。快乐编码 由于在AutoService
和Service
实体中都有serviceName
属性,ServiceRepository.findbyservicenameandcography
相当于以下SQL查询:
SELECT
*
FROM
Service
WHERE
serviceName = ?
AND category = ?
如图所示,此查询根本没有命中AutoService
实体,这就是结果不符合预期的原因
正确的存储库方法是:
public interface ServiceRepository extends JpaRepository<Service, Long> {
List<Service> findByCategoryAndAutoServiceServiceName(String category, String autoServiceName);
}
public interface servicepository扩展了JpaRepository{
列出findByCategoryAndAutoServiceServiceName(字符串类别,字符串autoServiceName);
}
此方法将按其serviceName
搜索嵌套的AutoService
对象
Github上提供了一个用于显示此操作的列表。不幸的是,我现在得到的列表是空的。我现在使用的是autoServiceId而不是名称。但由于某些原因,我从数据库中获取了空的数据列表。您是否能够解决此问题。上面的查询实际上可以解决这个问题。如果这有效,请将其标记为正确答案,因为它有助于将来搜索相同答案的人。否则请告诉我当前的问题。给我,如果你发送的类别,它应该工作的英文字符。谢谢你的好答案,我正在使用自定义查询现在,我从数据库中得到空列表。你能检查我的编辑吗?@Andrew,你不需要自定义查询,这就是我的答案。您可以简单地使用查找bycategoryandautoserviceid
或类似的内容。如果这不适用于您,请发布实际代码和一些我们可以测试的示例数据。