ashok
ashok

Reputation: 1268

Spring JPA one to one mapping getting null

I'm doing a unidirectional one to one mapping.

ActivitiProcessDeployment class

@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 class:

@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 interface:

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


    public List<ActivitiProcessDeployment> findAll();

}

controller class:

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

Response I am getting includes the filed from only ActivitiProcessDeployment class,but another class which has been mapped with ActivitiProcessDeployment class giving null value.

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

In the above response activitiProcessDefinition is null.

Please find below the table data. The id_ column in act_re_deployment table I am relating with deployment_id_ column of table act_re_procdef.

act_re_deployment table

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 table

      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

Upvotes: 2

Views: 2903

Answers (1)

Alan Hay
Alan Hay

Reputation: 23226

The problem is you have deployment_id columns in both tables. JPA will be using the one in act_re_deployment (as you have defined the @JoinColumn on ActivitiProcessDeployment) and that is set to null.

If you want the join column to be in the other table you can make a bi-directional mapping:

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

Upvotes: 1

Related Questions