Tuesday, 24 August 2021

Hibernate associations

The following post is for playing with the assiocations.


Uni-directional

 public class SourceEntity {

@Id

private Long id;

private String name;

@OneToMany

private List<TargetEntity> targets;

}

public class TargetEntity {
@Id
private Long id;
private String description;
}

Both tables have two columns each. No FK column is added.

For the able table, the third table is "source_entity_targets" is created with two columns "source_entity_id" and "target_id".

Bi-directional

If I add convert it to bi-directional, still it will create a third table. Following changes are added for bi-directional and Target Entity looks like. 

public class TargetEntity {

@Id
private Long id;
private String description;
@ManyToOne(fetch = FetchType.LAZY)
    private SourceEntity sourceEntity;
}

This time the target entity table has "source_entity_id" column as well. It is "MUL" key.

Adding @JoinColumn annotation in Source table


public class SourceEntity {
@Id
private Long id;
private String name;
        @OneToMany(cascade = {CascadeType.ALL})
@JoinColumn
private List<TargetEntity> targets;
}

public class TargetEntity {

@Id
private Long id;
private String description;

@ManyToOne(fetch = FetchType.LAZY)
private SourceEntity sourceEntity;

}

This creates two tables. The source table has 2 columns, Target tables have 4 columns out of which 2 columns are: source_entity_id and targets_id. 

On saving the object, the source_entity id was saved in target_ids.

Join Column name

When I added the name to the Join column annotation as following in the source entity class.

Two tables were created.

@OneToMany(cascade = {CascadeType.ALL})
@JoinColumn(name="source_id")
private List<TargetEntity> targets;

Target table's column name "targets_id" is converted to "source_id".

When I added the @JoinColumn annotation in targetClass as 
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="source_id")
private SourceEntity sourceEntity;

Three tables were created.
source_entity has 2 columns.
The target entity has now 3 columns. The third column is "source_id".
And source_entity_targets table with two columns as discussed above.

When saved an object, the source_id was set to null and the source_entity_targets columns had values.

After adding the following to source
@OneToMany(cascade = {CascadeType.ALL}, mappedBy = "sourceEntity") 
private List<TargetEntity> targets;

2 tables were created but the source id was not saved in the target table. the source_id column value was set to null.

To fix this, a new sourceEntity should be added to the target entity before saving the source entity. Example: 

SourceEntity src = new SourceEntity();
src.setName("source");
List<TargetEntity> targetEntities = new ArrayList<>();
TargetEntity target = new TargetEntity();
target.setDescription("description");
target.setSourceEntity(src); // <<<<< this line adds the source to target.
targetEntities.add(target);
src.setTargets(targetEntities);
dataService.persistData(src);









No comments:

Post a Comment