I am using spring-data-rest
.
update
and daily_update
are 2 table which is having one to many relationship. Running this application with spring boot.
When i am adding data using post request, entries being added into both table without any error but in child table (daily_update) column "update_id" (foreign key to update table) is coming null
.
I am using Lombok
for setter and getter.
Can you please help me with this?
UpdateEntity class :
@Data
@Entity
@Table(name = "update")
public class UpdateEntity {
@Id
@Column(name = "id")
@GeneratedValue(generator = "UUID")
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
private String id;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@Column(name = "start_time")
private Date startTime;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@Column(name = "end_time")
private Date endTime;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@Column(name = "date_created")
private Date dateCreated;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@Column(name = "date_modified")
private Date dateModified;
@OneToMany(mappedBy = "updateEntity", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Set<DailyUpdateEntity> dailyUpdateEntities = new HashSet<>();
}
DailyUpdateEntity class :
@Data
@Entity
@Table(name = "daily_update")
public class DailyUpdateEntity {
@Id
@Column(name = "id")
@GeneratedValue(generator = "UUID")
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
private String id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "update_id")
private UpdateEntity updateEntity;
@Column(name = "dimension_id")
private String dimensionId;
@Column(name = "hour")
private Integer hour;
@Column(name = "updated_type_id")
private String updatedTypeId;
}
UpdateRepository :
@RepositoryRestResource(collectionResourceRel = "update", path = "update")
public interface UpdateRepository extends CrudRepository<UpdateEntity, String> {
}
POST
request hitting from postman http://localhost:8080/update
{
"startTime" : "2016-08-18 10:34:26",
"endTime" : "2016-08-19 10:34:26",
"dateCreated" : "2016-06-18 10:34:26",
"dateModified" : "2016-06-18 10:34:26",
"dailyUpdateEntities" :
[
{
"dimensionId" : "6ea91f60-2b1d-11e7-93ae-92361f002671",
"hour" : "01",
"updatedTypeId" : "6ea9273a-2b1d-11e7-93ae-92361f002671"
},
{
"dimensionId" : "6ea92636-2b1d-11e7-93ae-92361f002671",
"hour" : "02",
"updatedTypeId" : "6ea92816-2b1d-11e7-93ae-92361f002671"
}
]
}
and running this application from spring boot
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
A one-to-many relationship between two entities is defined using the @OneToMany annotation in Spring Data JPA. It declares the mappedBy element to indicate the entity that owns the bidirectional relationship. Usually, the child entity owns the relationship, and the parent entity contains the @OneToMany annotation.
In simple terms, one to many mapping means that one row in a table can be mapped to multiple rows in another table. For example, think of a Cart system where we have another table for Items. A cart can have multiple items, so here we have one to many mapping.
Spring provides CrudRepository implementation class automatically at runtime. It contains methods such as save , findById , delete , count etc. Spring boot automatically detects our repository if the package of that repository interface is the same or sub-package of the class annotated with @SpringBootApplication .
I have had the exact problem, the issue stems from the declaration of table relationship. From UpdateEntity
to DailyUpdateEntity
, you are declaring the relationship in the following form;
@OneToMany(mappedBy = "updateEntity", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Set<DailyUpdateEntity> dailyUpdateEntities = new HashSet<>();
which causes the issue, by separating the insertion of dependent entity, and addition of foreign key to it. So first create operation will always lack a foreign key. With the following declaration, this issue is resolved, and creation will contain a foreign key;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "update_id", nullable = false, updatable = false)
private Set<DailyUpdateEntity> dailyUpdateEntities = new HashSet<>();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With