繁体   English   中英

从 controller 中的另一种方法查询时,JPA 实体未更新

[英]JPA entity not updated when querying from another method in controller

我是 Spring-boot 和 Spring JPA 框架的新手。 我有一个持久的 JPA 实体(“activeNode”)与另一个持久的 JPA 实体(“activeAudience”)具有一对多关系。

在我的 controller 函数之一中,在为每个函数创建两个实例之后,我将 activeAudience 实体添加到 activeNode 的(一对多)ActiveAudienceList。

然后,我使用它的 id 在其存储库中查询这个 activeNode object,并使用返回的 object 向另一个 controller 端点发送一个发布请求,该端点的数据在此点以字符串形式显示它的活动节点。 此时,在其“ActiveAudienceList”字段中,有一个活跃的受众实例。

但是,我随后向第三个 controller 端点(映射到另一个 controller 函数)发送了一个 get 请求,其中相同的 activeNode 的 id 作为路径变量传递,然后尝试在存储库中再次查询这个 activeNode(使用它的 id)然后显示它,但是这次“ActiveAudienceList”字段变成了一个空列表。

为什么在我尝试从另一个 controller function 的存储库中查询此 ActiveNode 实体的字段后未保存? 根据我的理解,ActiveNode 实例应该是持久的,因此 ActiveNode object 本身的任何更新都应该自动保存在存储库中。 以下是我的一些代码:

ActiveNode 和 ActiveAudience 实体:

@Entity
@Table
public class ActiveAudience {
    @Id
    @GeneratedValue
    private Long id;
    @ManyToOne(targetEntity = ActiveNode.class)
    @JoinColumn(name="audience_node_id",referencedColumnName = "id")
    private ActiveNode activeNode;
    //getters and setters...
}

@Entity
@Table(name="active_node")
public class ActiveNode {
    @Id
    @GeneratedValue
    private Long id;
    @OneToMany(mappedBy = "activeNode")
    private List<ActiveAudience> activeAudienceList = new ArrayList<>();
    //getters and setters..
}

存储库和 DAO:

//ActiveNodeRepository.java:
public interface ActiveNodeRepository extends JpaRepository<ActiveNode, Long> {
    @Query(value="select t from ActiveNode t where t.id=:Id")
    ActiveNode findByActiveNodeId(Long Id);
}
//ActiveAudienceRepository.java:
public interface ActiveAudienceRepository extends JpaRepository<ActiveAudience, Long> {
}
//DAO.java:
@Service
public class DAO {
...
    @Autowired
    private ActiveAudienceRepository activeAudienceRepository;
    @Autowired
    private ActiveNodeRepository activeNodeRepository;
    public ActiveNode addNewActiveNode(ActiveNode node){return activeNodeRepository.save(node);}
    public ActiveAudience addNewActiveAudience(ActiveAudience aud){return activeAudienceRepository.save(aud);}
    public ActiveNode searchActiveNodeById(Long id){return activeNodeRepository.findByActiveNodeId(id);}
...
}

Controller class 实现:

... 
    @Autowired
    private DAO dao;
    @GetMapping(value="/smalltest")
    public void test1(){
//create active node and active audience; associate them
        ActiveNode actn = dao.addNewActiveNode(new ActiveNode());
        ActiveAudience acta = dao.addNewActiveAudience(new ActiveAudience());
        actn.getActiveAudienceList().add(acta);
// when I directly query for the object in this method and display it like this, the "ActiveAudienceList" field has an instance
        restTemplate.exchange("http://localhost:8080/show", HttpMethod.POST,new HttpEntity<>(actn.toString()),String.class);
//But when I call a "intermediate" endpoint and do the same query and display it, the "ActiveAudienceList" becomes empty
      restTemplate.exchange("http://localhost:8080/smalltest2/"+Long.toString(actn.getId()), HttpMethod.GET,new HttpEntity<>(new HttpHeaders()),String.class);
    }

//the "intermediate" endpoint
    @GetMapping(value="/smalltest2/{id}")
    public void test2(@PathVariable("id") Long ID){
        ActiveNode actn = dao.searchActiveNodeById(ID);
        restTemplate.exchange("http://localhost:8080/show2", HttpMethod.POST,new HttpEntity<>(actn.toString()),String.class);
    }

//the following methods are used to display the field values of an entity
    private String str = new String();
    @RequestMapping(value="/show",method= RequestMethod.POST)
    public void showjson(@RequestBody String obj){
        this.str = obj;
    }
    @GetMapping(value = "/show",produces =MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String display(){
        return this.str;
    }

方法中直接查询的结果(创建了两个实例): 另一种方法查询的结果: 在此处输入图像描述

在将值添加到其活动受众列表之前,您正在持久化活动节点。 尝试设置活跃的受众,然后同样坚持:

ActiveNode actn = new ActiveNode();
ActiveAudience acta = dao.addNewActiveAudience(new ActiveAudience());
actn.getActiveAudienceList().add(acta);
dao.addNewActiveNode(actn);

暂无
暂无

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

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