Spring JPA一对一映射为null



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

ActivityProcessDeployment类

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

ActivityProcessDefinition类:

@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();
}
}

我得到的响应包括仅来自ActivityProcessDeployment类的文件,但另一个类已映射为ActivityProcessDeployment类,该类的值为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();
}
}

最新更新