persistence@glassfish.java.net

Re: Remove some children of entity

From: Gordon Yorke <gordon.yorke_at_oracle.com>
Date: Mon, 13 Oct 2008 07:43:49 -0400

Yes, the functionality I believe you are looking for (remove Orphan) is
being planned for JPA 2.
--Gordon

Laurent Yhuel wrote:
>
> Thanks for your response.
>
>
>
> So I use your solution.
>
>
>
> This functionality is plan in JPA 2 ?
>
>
>
> ------------------------------------------------------------------------
>
> *De :* Gordon Yorke [mailto:gordon.yorke_at_oracle.com]
> *Envoyé :* dimanche 12 octobre 2008 16:24
> *À :* persistence_at_glassfish.dev.java.net
> *Objet :* Re: Remove some children of entity
>
>
>
> The child record will only be removed from the database table if you
> actually call em.remove(Entity).
> You can not null the property "requete" because you have disallowed
> null in your mappings.
>
> Try something like this...
>
> TRequete parent = em.find(TRequete.class, parentID);
> TRequeteParam managedChild =
> em.find(TRequeteParam.class,childToRemoveId);
> parent.getListParam().remove(managedChild);
> em.remove(managedChild);
>
> merge can also work in place of find but you must still call
> em.remove(Entity) on the managed instance of the child.
> --Gordon
>
> Laurent Yhuel wrote:
>
> Hello,
>
>
>
> I try to delete some children (not all) of an entity.
>
> Cascade work in add, update, and if I remove parent.
>
> But if in parent, I've 3 children and I want to remove one of them, I
> don't known how can I do.
>
> I've tried to remove one child of Set and merge parent: Record is not
> deleted.
>
> I've tried to set null the property TRequete of one child: I've got
> exception : null is not allow.
>
>
>
> Thanks for your help.
>
> Laurent
>
>
>
> Object parent:
>
> @Entity
>
> @Table(name="t_requete"
>
> )
>
> public class TRequete implements java.io.Serializable {
>
>
>
> private Integer id;
>
> private String reqNom;
>
> private Set<TRequeteParam> listParam = new HashSet<TRequeteParam>(0);
>
>
>
> public TRequete() {
>
> }
>
> @SequenceGenerator(name="seq_TRequete",
> sequenceName="t_requete_id_requete_seq")@Id
> @GeneratedValue(strategy=SEQUENCE, generator="seq_TRequete")
>
>
>
>
>
> @Column(name="id_requete", unique=true, nullable=false)
>
> public Integer getId() {
>
> return this.id;
>
> }
>
>
>
> public void setId(Integer id) {
>
> this.id = id;
>
> }
>
>
>
> @Column(name="req_nom")
>
> public String getReqNom() {
>
> return this.reqNom;
>
> }
>
>
>
> public void setReqNom(String reqNom) {
>
> this.reqNom = reqNom;
>
> }
>
>
>
> @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY,
> mappedBy="requete")
>
> public Set<TRequeteParam> getListParam() {
>
> return this.listParam;
>
> }
>
>
>
> public void setListParam(Set<TRequeteParam> listParam) {
>
> this.listParam = listParam;
>
> }
>
> }
>
>
>
> Child object:
>
> @Entity
>
> @Table(name="t_requete_param"
>
> )
>
> public class TRequeteParam implements java.io.Serializable {
>
>
>
> private Integer id;
>
> private TRequete requete;
>
> private String reqparNom;
>
>
>
> public TRequeteParam() {
>
> }
>
>
>
> @SequenceGenerator(name="seq_TRequeteParam",
> sequenceName="t_requete_param_id_requete_param_seq")@Id
> @GeneratedValue(strategy=SEQUENCE, generator="seq_TRequeteParam")
>
>
>
>
>
> @Column(name="id_requete_param", unique=true, nullable=false)
>
> public Integer getId() {
>
> return this.id;
>
> }
>
>
>
> public void setId(Integer id) {
>
> this.id = id;
>
> }
>
> @ManyToOne(fetch=FetchType.LAZY)
>
> @JoinColumn(name="fk_requete", nullable=false)
>
> public TRequete getRequete() {
>
> return this.requete;
>
> }
>
>
>
> public void setRequete(TRequete requete) {
>
> this.requete = requete;
>
> }
>
>
>
>
>
> @Column(name="reqpar_nom")
>
> public String getReqparNom() {
>
> return this.reqparNom;
>
> }
>
>
>
> public void setReqparNom(String reqparNom) {
>
> this.reqparNom = reqparNom;
>
> }
>
> }
>
>
>