У меня проблема с удалением предметов из набора с использованием Spring Data JPA.У меня есть три сущности: commercialepiece, commercialepieceIem, sellpiece. Таким образом, отношения не связаны.commercialePiece 1 ----------> * commercialepieceitem.Я использую Postgresql в качестве СУБД,
Схема базы данных
Entity commercialepieceitem
CREATE TABLE public.commercialepieceitem
(
idpiece integer NOT NULL,
idpieceitem integer NOT NULL DEFAULT nextval('commercialepieceitem_idpieceitem_seq'::regclass),
idproduct integer NOT NULL,
qty numeric,
price numeric,
name_h character varying(255),
tax numeric,
discount numeric,
operationcode integer,
CONSTRAINT pk_commercialepieceitem PRIMARY KEY (idpiece, idpieceitem),
CONSTRAINT fk_commerci_affecte_product FOREIGN KEY (idproduct)
REFERENCES public.product (idproduct) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT fk_commerci_construit_commerci FOREIGN KEY (idpiece)
REFERENCES public.commercialepiece (idpiece) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
OIDS=FALSE
);
ALTER TABLE public.commercialepieceitem
OWNER TO postgres;
Entity commercialepiece
CREATE TABLE public.commercialepiece
(
idpiece integer NOT NULL DEFAULT nextval('commercialepiece_idpiece_seq'::regclass),
idtva integer,
idsession integer,
numpiece character varying(30),
datepiece date,
status character varying(20),
observation text,
dateecheance date,
discount numeric,
CONSTRAINT pk_commercialepiece PRIMARY KEY (idpiece),
CONSTRAINT fk_commerci_appliquer_tva FOREIGN KEY (idtva)
REFERENCES public.tva (idtva) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT fk_commerci_require_comptoir FOREIGN KEY (idsession)
REFERENCES public.comptoirsession (idsession) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
OIDS=FALSE
);
ALTER TABLE public.commercialepiece
OWNER TO postgres;
После сопоставления этого смного двунаправленных отношений:
@Entity
@Table(name = "commercialepiece")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class CommercialePiece {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "idpiece")
private Long idPiece;
@OneToMany(mappedBy = "commercialepiece", cascade = {CascadeType.ALL}, orphanRemoval = true, fetch = FetchType.EAGER)
private Set<CommercialePieceItem> pieceItems = new HashSet<>();
// Getter and setters and other attributes removed simplicity purprose
}
@Entity
@Table(name = "commercialepieceitem")
public class CommercialePieceItem {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "idpieceitem")
private Long idPieceItem;
@ManyToOne()
@JoinColumn(name = "idpiece")
private CommercialePiece commercialepiece;
// Getter and setters and other attributes removed simplicity purprose
}
@Entity
@Table(name = "sellpiece")
@PrimaryKeyJoinColumn(name = "idpiece")
public class SellPiece extends CommercialePiece {
}
Я пытался использовать некоторые аннотации, такие как @OnDelete (action = OnDeleteAction.CASCADE), но ничего не работает.
Итак, какой правильный путьудалить Orpahs?