adarsh
adarsh

Reputation: 161

Child entity does not get saved when parent entity saved . why?

I have a FilterEvent class like below

public class FilterEvent {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer filterId;
    @NotNull
    private String userId;
    @NotNull
    private String columnId;
    private String columnName;
    private String operator;
    private String filterValue;
    //@Column(columnDefinition = "varchar(255) DEFAULT 'filter'")
    @ColumnDefault("'frequency'")
    private String filterType;

    @JsonIgnore
    @ManyToOne(cascade = CascadeType.PERSIST,fetch = FetchType.EAGER)
    @JoinColumn(name = "dataset_id")
    private Dataset dataset;

    @JsonIgnore
    @ManyToOne(cascade = CascadeType.PERSIST,fetch = FetchType.EAGER)
    @JoinColumn(name = "prep_id")
    private Preparation preparation;
}

And i mapped this class with Preparation class with @ManyToOne

My Preparation class looks like below

@Entity
@Table(name = "preparation")
public class Preparation {

    @Id
    @Column(name = "prep_id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long prepId;
    @Column(name = "prep_name")
    private String prepName;

    @OneToMany(mappedBy = "preparation", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private Set<FilterEvent> filterEvents;
}

And when i tried to update the filterEvents during save of preparation as below , the filterEvents are not get saved in FilterEvent table .

public Preparation savePreparation(Integer datasetId, Preparation preparation) throws DatasetNotFoundException {
    LOGGER.trace("PreparationService : inside addPreparation");
    Dataset dataset = datasetRepository.findById(datasetId).get();
    if (null == dataset) {
        throw new DatasetNotFoundException(Integer.toString(datasetId));
    }
    preparation.setDataset(dataset);
    preparation.setUserId(dataset.getUserId());
    Set<FilterEvent> filterEvents = preparation.getFilterEvents();
    if(null!=filterEvents) {
        filterEvents.stream().forEach(f -> f.setFilterId(null));
    }

    preparationRepository.save(preparation);
    Long prepId = preparation.getPrepId();
    return preparationRepository.findById(prepId).get();

}

Why it is happening ? The need to save filterEvents in the FilterTable , When i save a preparation . Any help will be appreciated .

Upvotes: 0

Views: 93

Answers (1)

GauravRai1512
GauravRai1512

Reputation: 844

Change FilterEvent class property to cascade type ALL.as you have mentioned PERSIST.

public class FilterEvent {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer filterId;
@NotNull
private String userId;
@NotNull
private String columnId;
private String columnName;
private String operator;
private String filterValue;
//@Column(columnDefinition = "varchar(255) DEFAULT 'filter'")
@ColumnDefault("'frequency'")
private String filterType;

@JsonIgnore
@ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
@JoinColumn(name = "dataset_id")
private Dataset dataset;

@JsonIgnore
@ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
@JoinColumn(name = "prep_id")
private Preparation preparation;}

Upvotes: 1

Related Questions