Spring Data Rest: foreign key updated with null after post call in one to many relationship

I am using spring-data-rest

.

update

and daily_update

- these are 2 tables that have a one-to-many relationship. Running this application with spring boot.

When I add data using a post query, the records are added to both tables without any error, but the "update_id" column (foreign key to the update table) appears in the child table (daily_update) null

.

I am using Lombok

for setter and getter.

Could you 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 to hit from the 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 run this application from spring boot

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

      

+3


source to share


2 answers


I had the exact problem, the problem is with declaring a table relationship. From UpdateEntity

to, DailyUpdateEntity

you declare a relationship in the following form:

@OneToMany(mappedBy = "updateEntity", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Set<DailyUpdateEntity> dailyUpdateEntities = new HashSet<>();

      



which causes the problem by decoupling the insert of the dependent object and the addition of the foreign key to it. Therefore, there will always be no foreign key for the first create operation. The following declaration resolves this problem and the creation will contain a foreign key;

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "update_id", nullable = false, updatable = false)
private Set<DailyUpdateEntity> dailyUpdateEntities = new HashSet<>();

      

+4


source


The problem seems to be that you are trying to keep the relationship on the other side.

You are posting an UpdateEntity (versus UpdateRepository) where you mapped the collection Set<DailyUpdateEntity>

as mappedBy = "updateEntity"

, which means it is only the plaintext side of the bidirectional communication and there will be no connection between objects.

You would run into a non-null constraint error if you improved the DailyUpdateEntity display, for example:



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

      

Perhaps you need to tweak your mapping or think of a different storage strategy / order.

0


source







All Articles