@ManyToMany - Kein Löschen bereits zugewiesener Entities

andyx1975

Aktives Mitglied
Hallo,

ich habe mit Hilfe von EclipseLink eine Webanwendung geschrieben, über die Emails versendet werden. Die Anhänge werden als Uploadas Zentral verwaltet und können in einem extra Manager hochgeladen bzw. gelöscht werden. Jetzt möchte ich aber, dass Anhänge nicht gelöscht werden, wenn Sie einer Email zugewiesen wurden. Diese werden über @ManyToMany Beziehung zugewiesen. Momentan ist es so, dass alle Anhänge gelöscht werden. Auch wenn sie bereits einer Mail zugewiesen sind. Wie kann ich das ändern?

Danke
Andy


Entity Klasse für Emails:
Java:
package com.uds.webadmin.data;

import static javax.persistence.TemporalType.TIMESTAMP;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * Entity implementation class for Entity: DEmail
 */
@Entity
@XmlRootElement
@Table( name = "email_sended" )
@NamedQueries( {
    @NamedQuery( name = "findAllMyEmails", query = "SELECT e FROM DEmail e WHERE e.creator = :login ORDER BY e.creation_date DESC" ),
    @NamedQuery( name = "findEmailAssignments", query = "SELECT e FROM DEmail e ORDER BY e.creation_date DESC" ) } )
public class DEmail
    implements Serializable
{

    @Id
    @GeneratedValue( strategy = GenerationType.AUTO )
    @SequenceGenerator( name = "email_sended_id_seq", sequenceName = "email_sended_id_seq", allocationSize = 1 )
    @Column( columnDefinition = "SERIAL" )
    private long email_id;

    @Column
    private String sender_adress;

    @Column( columnDefinition = "TEXT" )
    private String to_recipients;

    @Column( columnDefinition = "TEXT" )
    private String cc_recipients;

    @Column( columnDefinition = "TEXT" )
    private String bcc_recipients;

    @Column
    private String headline;

    @Column( columnDefinition = "TEXT" )
    private String email_text;

    @Column
    private Boolean attachments;

    @Column
    private int email_type;

    @Column
    private Boolean self_copy;

    @Column
    private String creator;

    @Column
    @Temporal( TIMESTAMP )
    private Date creation_date;

    @ManyToMany( fetch = FetchType.EAGER )
    @JoinTable( name = "assignments_emails_uploads", joinColumns = @JoinColumn( name = "email_id", referencedColumnName = "email_id" ), inverseJoinColumns = @JoinColumn( name = "upload_id", referencedColumnName = "upload_id" ) )
    private List<DUpload> uploads;

    private static final long serialVersionUID = 1L;

    public DEmail()
    {
        super();
    }

    public long getEmail_id()
    {
        return email_id;
    }

    public void setEmail_id( long email_id )
    {
        this.email_id = email_id;
    }

    public String getSender_adress()
    {
        return sender_adress;
    }

    public void setSender_adress( String sender_adress )
    {
        this.sender_adress = sender_adress;
    }

    public String getTo_recipients()
    {
        return to_recipients;
    }

    public void setTo_recipients( String to_recipients )
    {
        if ( to_recipients.trim().equals( "null" ) )
        {
            to_recipients = null;
        }

        this.to_recipients = to_recipients;
    }

    public String getCc_recipients()
    {
        return cc_recipients;
    }

    public void setCc_recipients( String cc_recipients )
    {
        if ( cc_recipients.trim().equals( "null" ) )
        {
            cc_recipients = null;
        }

        this.cc_recipients = cc_recipients;

    }

    public String getBcc_recipients()
    {
        return bcc_recipients;
    }

    public void setBcc_recipients( String bcc_recipients )
    {
        if ( bcc_recipients.trim().equals( "null" ) )
        {
            bcc_recipients = null;
        }
        this.bcc_recipients = bcc_recipients;
    }

    public String getHeadline()
    {
        return headline;
    }

    public void setHeadline( String headline )
    {
        if ( headline.trim().equals( "null" ) )
        {
            headline = null;
        }
        this.headline = headline;
    }

    public String getEmail_text()
    {
        return email_text;
    }

    public void setEmail_text( String email_text )
    {
        if ( email_text.trim().equals( "null" ) )
        {
            email_text = null;
        }
        this.email_text = email_text;
    }

    public int getEmail_type()
    {
        return email_type;
    }

    public void setEmail_type( int email_type )
    {
        this.email_type = email_type;
    }

    public Boolean getSelf_copy()
    {
        return self_copy;
    }

    public void setSelf_copy( Boolean self_copy )
    {
        this.self_copy = self_copy;
    }

    public String getCreator()
    {
        return creator;
    }

    public void setCreator( String creator )
    {
        this.creator = creator;
    }

    public Date getCreation_date()
    {
        return creation_date;
    }

    public void setCreation_date( Date creation_date )
    {
        this.creation_date = creation_date;
    }

    public List<DUpload> getUploads()
    {
        return uploads;
    }

    public void setUploads( List<DUpload> uploads )
    {
        this.uploads = uploads;
    }

    public Boolean getAttachments()
    {
        return attachments;
    }

    public void setAttachments( Boolean attachments )
    {
        this.attachments = attachments;
    }

    public static long getSerialversionuid()
    {
        return serialVersionUID;
    }
}


Entity Klasse für Uploads:
Java:
package com.uds.webadmin.data;

import static javax.persistence.TemporalType.TIMESTAMP;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 * Entity implementation class for Entity: DUploads
 */

@Entity
@Table( name = "uploads" )
@XmlRootElement
@NamedQueries( { @NamedQuery( name = "findAllMyUploads", query = "SELECT e FROM DUpload e WHERE e.creator = :login ORDER BY e.creation_date DESC" ) /*
                                                                                                                                                     * add
                                                                                                                                                     * new
                                                                                                                                                     * namedQuery
                                                                                                                                                     * here
                                                                                                                                                     */} )
public class DUpload
    implements Serializable
{

    @Id
    @GeneratedValue( strategy = GenerationType.AUTO )
    @SequenceGenerator( name = "uploads_id_seq", sequenceName = "uploads_id_seq", allocationSize = 1 )
    @Column( columnDefinition = "SERIAL" )
    private long upload_id;

    @Column
    private String upload_name;

    @Column
    private int upload_type;

    @Column
    private String creator;

    @Column
    private String upload_link;

    @Column
    private Boolean active;

    @Column
    @Temporal( TIMESTAMP )
    private Date creation_date;

    @Column
    private String editor;

    @Column
    @Temporal( TIMESTAMP )
    private Date edit_date;

    @ManyToMany( mappedBy = "uploads", fetch = FetchType.EAGER )
    private List<DEmail> emails;

    private static final long serialVersionUID = 1L;

    public DUpload()
    {

    }

    public DUpload( long upload_id, String upload_name, int upload_type, String creator, String upload_link,
                    Boolean status, Date creation_date, String editor, Date edit_date )
    {
        this.upload_id = upload_id;
        this.upload_name = upload_name;
        this.upload_type = upload_type;
        this.creator = creator;
        this.upload_link = upload_link;
        this.active = status;
        this.creation_date = creation_date;
        this.editor = editor;
        this.edit_date = edit_date;
    }

    public long getUpload_id()
    {
        return upload_id;
    }

    public void setUpload_id( long upload_id )
    {
        this.upload_id = upload_id;
    }

    public String getUpload_name()
    {
        return upload_name;
    }

    public void setUpload_name( String upload_name )
    {
        this.upload_name = upload_name;
    }

    public int getUpload_type()
    {
        return upload_type;
    }

    public void setUpload_type( int upload_type )
    {
        this.upload_type = upload_type;
    }

    public String getCreator()
    {
        return creator;
    }

    public void setCreator( String creator )
    {
        this.creator = creator;
    }

    public String getUpload_link()
    {
        return upload_link;
    }

    public void setUpload_link( String upload_link )
    {
        this.upload_link = upload_link;
    }

    public Boolean getActive()
    {
        return active;
    }

    public void setActive( Boolean active )
    {
        this.active = active;
    }

    public Date getCreation_date()
    {
        return creation_date;
    }

    public void setCreation_date( Date creation_date )
    {
        this.creation_date = creation_date;
    }

    public String getEditor()
    {
        return editor;
    }

    public void setEditor( String editor )
    {
        this.editor = editor;
    }

    public Date getEdit_date()
    {
        return edit_date;
    }

    public void setEdit_date( Date edit_date )
    {
        this.edit_date = edit_date;
    }

    public static long getSerialversionuid()
    {
        return serialVersionUID;
    }

    @XmlTransient
    public List<DEmail> getEmails()
    {
        return emails;
    }

    public void setEmails( List<DEmail> emails )
    {
        this.emails = emails;
    }

}
 

andyx1975

Aktives Mitglied
Hallo,

vielen Dank für deine Antwort...die hatte ich mir bereits angeschaut. Leider gibt es für EclipseLink meines Wissens nur ein CascadeOnDelete. Die Notationen sind auf jedenfall anders.

Gruß
Andy
 

andyx1975

Aktives Mitglied
hey danke für dein Feeback...das mit dem Clear bringt mich nicht weiter, weil ich ja nicht löschen möchte wenn Objekte zugewiesen werden und die liste ist sowieso leer da ich wegen den loops ein @XmlTransient auf die EMailListe setzen muss.

Gruß
Andy
 

DanZ

Bekanntes Mitglied
Oh sorry, ich hab mich wohl komplett verlesen.

Aber wenn du sagst die Liste ist leer ist der Upload doch auch keinen Emails zugewiesen? Oder holst du dir die aktuelle Instanz nich aus dem PersistenceContext bevor du löscht?
 
Ähnliche Java Themen

Ähnliche Java Themen


Oben