gpt4 book ai didi

java - Spring JPA 一对一映射获取 null

转载 作者:行者123 更新时间:2023-12-02 10:37:42 25 4
gpt4 key购买 nike

我正在进行单向一对一映射。

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

存储库接口(interface):

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


public List<ActivitiProcessDeployment> findAll();

}

Controller 类:

@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 类映射的类给出 null 值。

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

在上面的响应中,activitiProcessDefinition 为 null。

请查看下面的表格数据。 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

最佳答案

问题是两个表中都有deployment_id 列。 JPA 将使用其中的一个act_re_deployment(因为您已在 ActivitiProcessDeployment 上定义了 @JoinColumn),并将其设置为 null。

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

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

@OneToOne (cascade=CascadeType.ALL, mappedBy="deployment")
private ActivitiProcessDefinition activitiProcessDefinition;
}

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

@JsonIgnore
@OneToOne
@JoinColumn(name="deployment_id_", unique= true)
private ActivitiProcessDeployment deployment;

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

@JsonProperty
public int getDeploymentId(){
return deployment.getId();
}
}

关于java - Spring JPA 一对一映射获取 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53155944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com