Good Morning,
I have a problem to delete a orphan of my relationship...
When I try to save a entity without one of the relationship, that I want to delete, the hibernate try to update the relationship entity setting null the pk (composite pk) instead of delete... Follow my entities:
CompositePK-01:
Code:
@Embeddable
public class ScenarioEnvironmentPK implements Serializable {
/**
*
*/
private static final long serialVersionUID = -7167793695463259418L;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumns({@JoinColumn(referencedColumnName = "id", name = "scenario_id", insertable = false, updatable = false),
@JoinColumn(referencedColumnName = "use_case_pk", name = "use_case_pk", insertable = false, updatable = false)})
private Scenario scenario;
private Long environment;
/*Getters and Setters*/
}
Relationship-01
Code:
@Entity
@Table(name = "scenario_environment")
public class ScenarioEnvironment implements Serializable {
@EmbeddedId
private ScenarioEnvironmentPK pk;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@MapsId("environment")
@JoinColumn(name = "environment_pk")
private Environment environment;
/*Another attribute*/
/*Getters and Setters*/
}
CompositePK-02:
Code:
@Embeddable
public class ScenarioPK implements Serializable {
private static final long serialVersionUID = -9051477071487317416L;
private Long useCase;
private String id;
/*Getters and setters*/
}
Entity:
Code:
@Entity
@Table(name = "scenario")
public class Scenario implements Serializable {
private static final long serialVersionUID = 9150529649737523948L;
@EmbeddedId
private ScenarioPK pk;
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("useCase")
@JoinColumn(name = "use_case_pk")
private UseCase useCase;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumns({@JoinColumn(referencedColumnName = "id", name = "scenario_id", insertable = true, updatable = true),
@JoinColumn(referencedColumnName = "use_case_pk", name = "use_case_pk", insertable = true, updatable = true)})
private Set<ScenarioEnvironment> scenarioEnvironment;
/*Another Attributes*/
/*Getters and Setters*/
}
I did the following test:
- Created a Scenario with 2 ScenarioEnvironments on Set object and requested to the hibernate save the Scenario entity, and It saved perfectly the entity and your relationship;
- I removed 1 ScenarioEnvironment and requested to hibernate save the Scenario entity, the hibernate sets null 2 of 3 attributes of the pk to null instead to delete the scenarioEnvironment;
The hibernate SQL:
Code:
INFO [stdout] (http--0.0.0.0-8080-4) Hibernate:
INFO [stdout] (http--0.0.0.0-8080-4) update
INFO [stdout] (http--0.0.0.0-8080-4) scenario_environment
INFO [stdout] (http--0.0.0.0-8080-4) set
INFO [stdout] (http--0.0.0.0-8080-4) scenario_id=null,
INFO [stdout] (http--0.0.0.0-8080-4) use_case_pk=null
INFO [stdout] (http--0.0.0.0-8080-4) where
INFO [stdout] (http--0.0.0.0-8080-4) scenario_id=?
INFO [stdout] (http--0.0.0.0-8080-4) and use_case_pk=?
INFO [stdout] (http--0.0.0.0-8080-4) and environment_pk=?
INFO [org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl] (http--0.0.0.0-8080-4) HHH000010: On release of batch it still contained JDBC statements
WARN [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http--0.0.0.0-8080-4) SQL Error: 0, SQLState: S1093
Have a way to delete the orphan relantionship directly?
I saw one examples that say to delete the relationship before to save the entity, but I don't want this.