简体   繁体   English

使用 JPARepository 查询列表

[英]Query a list with JPARepository

I need to get the exact values who match with my query but with my query it returns more values (who also contains my list) and return even two value of the same rows: the query that I do is this:我需要获取与我的查询匹配的确切值,但对于我的查询,它会返回更多值(也包含我的列表)并返回相同行的两个值:我执行的查询是这样的:

 List<Archive> findAllByIdentifierAndChannelsChannelNameIn(String identifier, List<String> channel);

my model class is this:我的 model 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;
    }

The problem is when I pass these body:问题是当我通过这些身体时:

{
"identifier": "NGLCRS97D12G866L",
"channels": ["Sky news","Rai 4"]
}

It give me back this:它给了我这个:

"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"
            }
        ]
    }

As you can see it will give me back 2 value og the same row (archiveId: 9) but I need to get the exact value when I pass more channels to match what I want beacuse I need to use in a delete.正如您所看到的,它会在同一行 (archiveId: 9) 中返回 2 个值,但是当我传递更多通道以匹配我想要的值时,我需要获得准确的值,因为我需要在删除中使用。 thanks to all.谢谢大家。 Even if I'm going to use the native query I don't know how to write the right query to get just the values that I need it即使我要使用本机查询,我也不知道如何编写正确的查询来获取我需要的值SQL查询

For more complex queries I recommend using @Query instead of JPA repository.对于更复杂的查询,我建议使用@Query而不是 JPA 存储库。 You can write it in several ways.您可以通过多种方式编写它。 First option is writing a native query.第一个选项是编写本机查询。

@Query(value = "select * from table where something = :variableName", nativeQuery = true);
public List<MyClass> myQuery(@Param("variableName") String 
variable);

Second option is writing a simple non native Query and use your Entiry class names and fields.第二个选项是编写一个简单的非本机查询并使用您的 Entiry class 名称和字段。 For this you purpose you can use javax.persistence.EntityManager or @Query(value = "", nativeQuery = false) .为此,您可以使用javax.persistence.EntityManager@Query(value = "", nativeQuery = false) By default @Query is non native query, you dont have to write 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();

You don't have to use EntityManager you can also write non native Query with the @Query annotation as I mentioned above.您不必使用EntityManager ,您也可以使用我上面提到的@Query注释编写非本机查询。

Solved By adding this to my 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)

if somebody has a better solution please let me know如果有人有更好的解决方案请告诉我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM