Spring Data Rest : Foreign key is update with null

2019-05-11 15:05发布

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

2条回答
不美不萌又怎样
2楼-- · 2019-05-11 15:17

It seems the problem is that you try to persist the relation on the wrong side.

You are posting an UpdateEntity (against the UpdateRepository) where you mapped the Set<DailyUpdateEntity> collection as mappedBy = "updateEntity" which means that this is just the readonly side of the bidirectional association and there will be no connection between the entities.

You would face a not-null constraint error if you would enhance the DailyUpdateEntity mapping like:

@ManyToOne(optional = false, fetch = FetchType.EAGER)
@JoinColumn(name = "update_id")
private UpdateEntity updateEntity;

Maybe you should adjust your mapping or think about another storing strategy / order.

查看更多
混吃等死
3楼-- · 2019-05-11 15:28

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<>();
查看更多
登录 后发表回答