Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data Rest : Foreign key is update with null after post call in one to many relationship

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);
    }
}
like image 798
Code Poet Avatar asked May 05 '17 13:05

Code Poet


People also ask

What is one to many and many to one relationship in spring boot?

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.

How does one to Many mapping work?

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.

How CrudRepository works in Spring boot?

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 .


1 Answers

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<>();
like image 192
buræquete Avatar answered Sep 28 '22 03:09

buræquete