小编Raf*_*ado的帖子

是否可以在Spring Repository @Query注释中使用Array对象作为参数?

是否可以在Spring Repository @Query注释中使用Array对象作为参数?

我正在尝试检索表中列节点存在于String数组中的所有行.是否可以使用Spring存储库中的@Query注释一次完成?

这是我的位置实体:

@Entity
@Table(name = "LOCATIONS")
public class Location extends Measurement{

    private String latitude;
    private String nsIndicator;

    private String longitude;
    private String ewIndicator;

    @ManyToOne
    @JoinColumn(name="node")
    private Node node;
}
Run Code Online (Sandbox Code Playgroud)

其中节点引用Node类,并将其作为BIGINT映射到数据库中.

我有一个像这样的存储库:

public interface LocationRepository extends CrudRepository<Location, Long>{

    @Query(value=
            "SELECT l1.node, l1.id, l1.latitude, l1.longitude " +
            "FROM LOCATIONS l1 WHERE l1.node IN (:ids)", 
            nativeQuery=true)
    List<Location> findMeasureByIds(@Param("ids") String[] ids);
}
Run Code Online (Sandbox Code Playgroud)

在那里你可以看到我正在尝试执行的查询,但它无法正常工作.我不知道是否可以在那里使用数组,或者参数必须只是字符串和/或整数,我无法在任何地方找到它.

我已经尝试了几种组合,例如使用具有正确格式的简单字符串或长数组......但到目前为止还没有任何工作.

提前致谢.

解:

@Query(value="SELECT * FROM LOCATIONS l1 " + 
             "INNER JOIN (SELECT node, MAX(id) AS id FROM …
Run Code Online (Sandbox Code Playgroud)

postgresql spring spring-data-jpa

6
推荐指数
1
解决办法
1万
查看次数

Spring JpaRepository 不返回更新的数据

我正在开发一个基于 Spring 的服务器。我有两个Controllers正在侦听不同的 http 请求。它们都有一个引用Repository@Autowired注释相同的变量。

@Controller
public class ClientController {
    @Autowired
    NodeRepository nodeRepository;
    ...
}

@Controller
public class NodeController {
    @Autowired
    NodeRepository nodeRepository;
    ...
}
Run Code Online (Sandbox Code Playgroud)

所有的数据库操作都是通过使用NodeRepository

public interface NodeRepository extends JpaRepository<Node, Long>{
    Node findByNodeId(long nodeId);
    List<Node> findByStatusIn(Set<String> status);
    List<Node> findByNodeIdIn(Set<Long> ids);
    Node findByPhoneNumber(String phoneNumber);
    Node findByCloudId(String cloudId);
    Node findByDeviceDescription(String deviceDescription);
}
Run Code Online (Sandbox Code Playgroud)

行为如下:

ClientController接收到一个新的请求时,它被处理并且控制器向外部设备发送一个请求并等待它的答复。
这个答案是由NodeController. 因此,当答案到达时,接收到的信息将保存在数据库中,并向 发送信号ClientController以唤醒它。

当这个控制器被唤醒并试图从数据库中检索更新的信息时,我的问题就出现了。该信息不是刚刚存储的信息,而是之前存储的信息。

这是ClientController块和检索信息发生的代码片段:

// Retrieves the list of nodes …
Run Code Online (Sandbox Code Playgroud)

java spring jpa spring-data-jpa

2
推荐指数
1
解决办法
5068
查看次数

标签 统计

spring ×2

spring-data-jpa ×2

java ×1

jpa ×1

postgresql ×1