Spring JPA一对一映射变为空

时间:2018-11-05 14:03:01

标签: java spring-boot jpa spring-data-jpa spring-data

我正在做单向一对一映射。

ActivitiProcessDeployment类

@Entity
@Table(name="act_re_deployment")
public class ActivitiProcessDeployment implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @JsonIgnore
    @Id
    @JsonProperty
    @Column(name="id_")
    private String id;

    @JsonProperty
    @Column(name="name_")
    private String name;

    @JsonProperty
    @Column(name="category_")
    private String category;

    @JsonProperty
    @Column(name="tenant_id_")
    private String tenantId;

    @JsonProperty
    @Column(name="deploy_time_")
    private Date deployTime;

    @OneToOne (cascade=CascadeType.ALL)
    @JoinColumn(name="deployment_id_", unique= true, nullable=true, insertable=true, updatable=true)
    private ActivitiProcessDefinition activitiProcessDefinition;
//getters and setters
//tostring method
}

ActivitiProcessDefinition类:

@Entity
@Table(name="act_re_procdef")
public class ActivitiProcessDefinition implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;


    @Id
    @Column(name="id_")
    @JsonProperty("process_def")
    private String id;

    @JsonIgnore
    @Column(name="rev_")
    private String rev;

    @JsonProperty
    @Column(name="category_")
    private String category;

    @JsonProperty
    @Column(name="name_")
    private String name;

    @JsonProperty
    @Column(name="key_")
    private String key;

    @JsonProperty
    @Column(name="resource_name_")
    private String resource_name;

    @JsonProperty
    @Column(name="version_")
    private String version;

    @JsonProperty
    @Column(name="deployment_id_")
    private String deploymentId;

    @JsonProperty
    @Column(name="dgrm_resource_name_")
    private String diagramResourceName;

    @JsonProperty
    @Column(name="description_")
    private String description;

    @JsonProperty
    @Column(name="has_start_form_key_")
    private String hasStartFormKey;

    @JsonProperty
    @Column(name="has_graphical_notation_")
    private String hasGraphicalNotation_;

    @JsonProperty
    @Column(name="suspension_state_")
    private String suspensionState;

    @JsonProperty
    @Column(name="tenant_id_")
    private String tenant_id_;

 //getters and setters
    //tostring method
    }
}

存储库界面:

@Repository
public interface ActivitiGetDeploymentRepository extends JpaRepository<ActivitiProcessDeployment, Long> {


    public List<ActivitiProcessDeployment> findAll();

}

控制器类:

@RestController
@RequestMapping("/ProcessInfo/1.0.0")
public class RestController {
@ApiOperation(value = "getdeployments", notes = "This REST API is used to get deployments")
    @GetMapping(value = "/getdeployments")
    private List<ActivitiProcessDeployment> getdeployments() {

        return ActivitiGetDeploymentRepository.findAll();
    }
}

我得到的响应包括仅来自ActivitiProcessDeployment类的文件,但是另一个类已与ActivitiProcessDeployment类映射为空值。

[
  {
    "id": "2505",
    "name": "newtest",
    "category": null,
    "tenantId": "-1234",
    "deployTime": "2018-11-05T12:47:02.547+0000",
    "activitiProcessDefinition": null
  }
]

在上述响应中,activitiProcessDefinition为空。

请在表格数据下方找到。我将act_re_deployment表中的id_列与表act_re_procdef的deployment_id_列相关。

act_re_deployment表

id_  |  name_  | category_ | tenant_id_ |      deploy_time_       | activiti_process_definition_id_ | deployment_id_
------+---------+-----------+------------+-------------------------+---------------------------------+----------------
 2505 | newtest |           | -1234      | 2018-11-05 18:17:02.547 |                                 |

act_re_procdef表

      id_       | rev_ |          category_           |  name_  |  key_   | version_ | deployment_id_ |   resource_name_   | dgrm_resource_name_ | description_ | has_start_form_key_ | has_graphical_notation_ | suspension_state_ | tenant_id_
----------------+------+------------------------------+---------+---------+----------+----------------+--------------------+---------------------+--------------+---------------------+-------------------------+-------------------+------------
 newtest:1:2508 |    1  | http://www.activiti.org/test | newtest | newtest |        1 | 2505           | newtest.bpmn20.xml | newtest.newtest.png |              | f                   | t                       |                 1 | -1234

1 个答案:

答案 0 :(得分:1)

问题在于两个表中都包含deployment_id列。 JPA将使用其中一种 /p:UseWPP_CopyWebApplication=true /p:PipelineDependsOnBuild=false(如您在act_re_deployment上定义的@JoinColumn一样),该属性设置为null。

如果您希望联接列位于另一个表中,则可以进行双向映射:

ActivitiProcessDeployment