[ad_1]
I have a service in Spring boot which is supposed to receive a JSON like:
{
"tasks": [
{
"assets": [
{
"uid": "b10c6d97-8ca2-4a05-bb69-705815134136"
},
{
"uid": "a08c6d97-4da3-4a05-bb69-123456787754"
}
]
}
]
}
Via a POST and is supposed to insert in the database the following entities in this order:
- Insert a schema entity (the base of the JSON)
- Insert multiple task entities pointing to the schema previously inserted
- Insert multiple asset entities in the “Join table” pointing to the task sent. Assets are already inserted in their corresponding table and should NOT be created from the POST.
The database i’m working on (and cannot change) is a PostgreSQL with the following:
My classes are:
public class SchemeEntity{
@Id
@GeneratedValue(generator = "UUID")
private UUID uid;
@Column(name = "created")
private Date created;
@Column(name = "updated")
private Date updated;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "scheme_uid")
private List<TaskEntity> tasks;
}
public class TaskEntity {
@Id
@GeneratedValue(generator = "UUID")
private UUID uid;
@Column(name = "created")
private Date created;
@Column(name = "updated")
private Date updated;
@ManyToOne(fetch = FetchType.LAZY)
private SchemeEntity scheme;
@OneToMany(mappedBy = "task", cascade = ALL)
private Set<TaskAsset> assets;
}
public class AssetEntity{
@Id
@GeneratedValue(generator = "UUID")
private UUID uid;
@Column(name = "created")
private Date created;
@Column(name = "updated")
private Date updated;
@OneToMany(mappedBy = "asset", cascade = CascadeType.ALL)
private Set<TaskAsset> tasks = new HashSet<>();
}
public class TaskAssetEntity implements Serializable {
@Id
@GeneratedValue(generator = "UUID")
private UUID uid;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "task_uid")
private TaskEntity task;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "asset_uid")
private AssetEntity asset;
public TaskAssetEntity (TaskEntity task) {
this.task= task;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof TaskAssetEntity)) return false;
TaskAssetEntity that = (TaskAssetEntity ) o;
return Objects.equals(task.getUid(), that.task.getUid()) &&
Objects.equals(asset.getUid(), that.asset.getUid());
}
@Override
public int hashCode() {
if(asset!=null && task!=null)
return Objects.hash(asset.getUid(), task.getUid());
if(asset==null && task!=null)
return Objects.hash(task.getUid());
else
return Objects.hash(asset.getUid());
}
}
Im following this tutorial:
However, when I want to insert a new Schema with the data passed in the JSON I showed I get
NullPointerException in my hashCode() function on the middle table.
I’ve tried other tutorials but with no luck, having errors of ‘detached’ entities (tried MERGE then but no luck.
What am I missing? Help, I’m going insane
[ad_2]