Java 带有一对多参数的Spring查询

Java 带有一对多参数的Spring查询,java,spring,spring-data-jpa,Java,Spring,Spring Data Jpa,我必须查询一些参数与给定值相似的网络元素,例如搜索“ExampleV”将返回带有“ExampleValue”的行 这目前正在工作,但端口参数在网元中映射为一对多 @Entity public class NetworkElement { @Id @GeneratedValue(strategy = GenerationType.AUTO) public long id; /** * The ip address of the NetworkEleme

我必须查询一些参数与给定值相似的网络元素,例如搜索“ExampleV”将返回带有“ExampleValue”的行

这目前正在工作,但端口参数在网元中映射为一对多

@Entity
public class NetworkElement {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public long id;

    /**
     * The ip address of the NetworkElement. This should be unique
     */
    public String ip;

    /**
     * The port or NetworkElement
     */
    public int port;

    /**
     * The name this node is called. This is what the user want to see on the Gui for such NetworkElement
     */
    public String name;

    /**
     * Timestamp of the last alarm received for this NetworkElement
     */
    public long lastEventTimestamp;

    /**
     * Last read index from the alarm status table
     */
    public int lastReadIndex;

    public long latitude;

    public long longitude;

    public String address;

    public String upTime;


    @OneToMany(mappedBy = "node")
    public List<Port> ports;

    @ManyToOne
    @JoinColumn(name = "type_id")
    public NetworkElementType networkElementType;

    public NetworkElement(String ip, String name, long lastEventTimestamp, List<Port> ports, NetworkElementType networkElementType) {
        this.ip = ip;
        this.name = name;
        this.lastEventTimestamp = lastEventTimestamp;
        this.ports = ports;
        this.networkElementType = networkElementType;
    }


}

如何正确查询具有特定customerId端口的网络元素?

假设
端口
实体值被称为
customerId
,例如:

@Entity
class Port {
    private Integer customerId;
}
您需要一个新的
加入
并更改
的位置

@Query("SELECT ne FROM NetworkElement ne JOIN ne.ports port WHERE " +
    "ne.ip LIKE %:ip% AND " +
    "lower(ne.name) LIKE concat('%', lower(:name), '%') AND " +
    "lower(ne.networkElementType.type) LIKE concat('%', lower(:type), '%') AND " +
    "lower(ne.networkElementType.vendor) LIKE concat('%', lower(:vendor), '%') AND " +
    "lower(ne.networkElementType.version) LIKE concat('%', lower(:version), '%') AND "+
    "port.customerId LIKE concat('%', lower(:customerId), '%')")`

并删除
@Param(“port”)字符串port

实体端口有一个名为customerId的字段,该字段必须匹配。这意味着代码中的最后一行应该被删除,并且“str(port.value)像%:port%和“+应该是”str(port.customerId)像%:customerId%和“+是这样吗?是的!我将更改我的答案,我看不到最后一行。您是否仍应删除port.value?我只是想确认一下:)是的,删除了。我尝试了以下方法:@Query(“从NetworkElement ne中选择ne”+“加入ne.ports端口”+,其中“+”ne.ip类似%:ip%和“+”lower(ne.name)类似concat(“%”)、lower(:name)、“%”和“+”lower(ne.networkElementType)类似concat(“%”)、lower(:type)下(“%”、下(:vendor)、“%”和“+”下(ne.networkElementType.vendor)如concat(“%”)、下(:vendor)、“%”和“+”下(ne.networkElementType.version)如concat(“%”)、下(:version)、“%”和“+”下(port.customerId)如concat(“%”、下(:customerId)、“%”)
@Entity
class Port {
    private Integer customerId;
}
@Query("SELECT ne FROM NetworkElement ne JOIN ne.ports port WHERE " +
    "ne.ip LIKE %:ip% AND " +
    "lower(ne.name) LIKE concat('%', lower(:name), '%') AND " +
    "lower(ne.networkElementType.type) LIKE concat('%', lower(:type), '%') AND " +
    "lower(ne.networkElementType.vendor) LIKE concat('%', lower(:vendor), '%') AND " +
    "lower(ne.networkElementType.version) LIKE concat('%', lower(:version), '%') AND "+
    "port.customerId LIKE concat('%', lower(:customerId), '%')")`