使用 JPARepository 查询列表
Query a list with JPARepository
我需要获取与我的查询匹配的确切值,但对于我的查询,它有 return 多个值(也包含我的列表)和 return 甚至是同一行的两个值:
我做的查询是这样的:
List<Archive> findAllByIdentifierAndChannelsChannelNameIn(String identifier, List<String> channel);
我的模型class是这样的:
public class Archive {
@Id
@Column(name = "ARCHIVE_ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Generated(GenerationTime.ALWAYS)
private Long archiveId;
@Column(name = "IDENTIFIER")
private String identifier;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "archive")
@JsonManagedReference
private Set<Channel> channels;
}
public class Channel{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Generated(GenerationTime.ALWAYS)
private Long channelId;
@ManyToOne
@JoinColumn(name = "archive_id")
@JsonBackReference
private Archive archive;
@Column(name = "Channel_Name")
private String channelName;
}
问题是当我传递这些正文时:
{
"identifier": "NGLCRS97D12G866L",
"channels": ["Sky news","Rai 4"]
}
它还给我这个:
"archiveId": 24,
"identifier": "NGLCRS97D12G866L",
"channels": [
{
"channelId": 20,
"channelName": "Sky news"
}
]
},
{
"archiveId": 9,
"identifier": "NGLCRS97D12G866L",
"channels": [
{
"channelId": 2,
"channelName": "Rai 4"
},
{
"channelId": 40,
"channelName": "Sky news"
}
]
},
{
"archiveId": 9,
"identifier": "NGLCRS97D12G866L",
"channels": [
{
"channelId": 2,
"channelName": "Rai 4"
},
{
"channelId": 40,
"channelName": "Sky news"
}
]
},
{
"archiveId": 25,
"identifier": "NGLCRS97D12G866L",
"channels": [
{
"channelId": 41,
"channelName": "Sky news"
},
{
"channelId": 1,
"channelName": "Boing"
}
]
},
{
"archiveId": 8,
"identifier": "NGLCRS97D12G866L",
"portal": "PORTALE_TITOLARI",
"channels": [
{
"channelId": 39,
"channelName": "Sky news"
}
]
}
如您所见,它会返回同一行的 2 个值 (archiveId: 9)
但是当我通过更多频道以匹配我想要的内容时,我需要获得确切的值,因为我需要在删除中使用。谢谢大家。
即使我要使用本机查询,我也不知道如何编写正确的查询来获取我需要的值
对于更复杂的查询,我建议使用 @Query
而不是 JPA 存储库。
您可以通过多种方式编写它。第一个选项是编写本机查询。
@Query(value = "select * from table where something = :variableName", nativeQuery = true);
public List<MyClass> myQuery(@Param("variableName") String
variable);
第二个选项是编写一个简单的非本机查询并使用您的 Entiry class 名称和字段。为此,您可以使用 javax.persistence.EntityManager
或 @Query(value = "", nativeQuery = false)
。默认 @Query
是非原生查询,你不必写 nativeQuery = false
//@Autowired Constructor dependency injection is more preferred instead
@Autowired
private EntityManager entityManager;
List<MyClass> query = entityManager.createQuery("SELECT new
MyClass(m.id, m.name) from MyClass m where m.name = :variableName", ValidDomain.class).getResultList();
您不必使用 EntityManager
您也可以使用我上面提到的 @Query
注释编写非本机查询。
通过将此添加到我的查询中解决:
@Query(value = "Select * from tbl_archive A where a.archive_id IN (select A.ARCHIVE_ID FROM tbl_archive A JOIN tbl_channel C ON(C.ARCHIVE_ID =A.ARCHIVE_ID)" +
"WHERE a.identifier= :Identifier AND c.channel_name IN :channels group by A.ARCHIVE_ID" +
" having count(c.ARCHIVE_ID) = :channelSize)", nativeQuery = true)
如果有人有更好的解决方案请告诉我
我需要获取与我的查询匹配的确切值,但对于我的查询,它有 return 多个值(也包含我的列表)和 return 甚至是同一行的两个值: 我做的查询是这样的:
List<Archive> findAllByIdentifierAndChannelsChannelNameIn(String identifier, List<String> channel);
我的模型class是这样的:
public class Archive {
@Id
@Column(name = "ARCHIVE_ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Generated(GenerationTime.ALWAYS)
private Long archiveId;
@Column(name = "IDENTIFIER")
private String identifier;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "archive")
@JsonManagedReference
private Set<Channel> channels;
}
public class Channel{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Generated(GenerationTime.ALWAYS)
private Long channelId;
@ManyToOne
@JoinColumn(name = "archive_id")
@JsonBackReference
private Archive archive;
@Column(name = "Channel_Name")
private String channelName;
}
问题是当我传递这些正文时:
{
"identifier": "NGLCRS97D12G866L",
"channels": ["Sky news","Rai 4"]
}
它还给我这个:
"archiveId": 24,
"identifier": "NGLCRS97D12G866L",
"channels": [
{
"channelId": 20,
"channelName": "Sky news"
}
]
},
{
"archiveId": 9,
"identifier": "NGLCRS97D12G866L",
"channels": [
{
"channelId": 2,
"channelName": "Rai 4"
},
{
"channelId": 40,
"channelName": "Sky news"
}
]
},
{
"archiveId": 9,
"identifier": "NGLCRS97D12G866L",
"channels": [
{
"channelId": 2,
"channelName": "Rai 4"
},
{
"channelId": 40,
"channelName": "Sky news"
}
]
},
{
"archiveId": 25,
"identifier": "NGLCRS97D12G866L",
"channels": [
{
"channelId": 41,
"channelName": "Sky news"
},
{
"channelId": 1,
"channelName": "Boing"
}
]
},
{
"archiveId": 8,
"identifier": "NGLCRS97D12G866L",
"portal": "PORTALE_TITOLARI",
"channels": [
{
"channelId": 39,
"channelName": "Sky news"
}
]
}
如您所见,它会返回同一行的 2 个值 (archiveId: 9)
但是当我通过更多频道以匹配我想要的内容时,我需要获得确切的值,因为我需要在删除中使用。谢谢大家。
即使我要使用本机查询,我也不知道如何编写正确的查询来获取我需要的值
对于更复杂的查询,我建议使用 @Query
而不是 JPA 存储库。
您可以通过多种方式编写它。第一个选项是编写本机查询。
@Query(value = "select * from table where something = :variableName", nativeQuery = true);
public List<MyClass> myQuery(@Param("variableName") String
variable);
第二个选项是编写一个简单的非本机查询并使用您的 Entiry class 名称和字段。为此,您可以使用 javax.persistence.EntityManager
或 @Query(value = "", nativeQuery = false)
。默认 @Query
是非原生查询,你不必写 nativeQuery = false
//@Autowired Constructor dependency injection is more preferred instead
@Autowired
private EntityManager entityManager;
List<MyClass> query = entityManager.createQuery("SELECT new
MyClass(m.id, m.name) from MyClass m where m.name = :variableName", ValidDomain.class).getResultList();
您不必使用 EntityManager
您也可以使用我上面提到的 @Query
注释编写非本机查询。
通过将此添加到我的查询中解决:
@Query(value = "Select * from tbl_archive A where a.archive_id IN (select A.ARCHIVE_ID FROM tbl_archive A JOIN tbl_channel C ON(C.ARCHIVE_ID =A.ARCHIVE_ID)" +
"WHERE a.identifier= :Identifier AND c.channel_name IN :channels group by A.ARCHIVE_ID" +
" having count(c.ARCHIVE_ID) = :channelSize)", nativeQuery = true)
如果有人有更好的解决方案请告诉我