tutorials.de Buch-Aktion 05/2012
ERLEDIGT
NEIN
ANTWORTEN
2
ZUGRIFFE
1035
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    Andyx1975 Andyx1975 ist offline Mitglied Silber
    Registriert seit
    Nov 2009
    Beiträge
    66
    Hallo,

    ich habe gerade angefangen mich mit JPA auseinander zu setzen. Ich habe mir 2 Entity Klassen gebaut und diese über ManyToMany miteinander verbunden. Funktioniert auch soweit.


    DEmail
    Code java:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    
    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.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.JoinTable;
    import javax.persistence.ManyToMany;
    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" )
    @NamedQuery( name = "findEmails", 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( 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 int email_type;
     
        @Column
        private Boolean self_copy;
     
        @Column
        private String creator;
     
        @Column
        @Temporal( TIMESTAMP )
        private Date creation_date;
     
        @ManyToMany
        @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 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 static long getSerialversionuid()
        {
            return serialVersionUID;
        }
     
        public void addUpload( DUpload upload )
        {
            if ( !getUploads().contains( upload ) == false)
            {
                getUploads().add( upload );
            }
            if ( upload.getEmails().contains( this ) == false)
            {
                upload.getEmails().add( this );
            }
        }
        
        public void removeUpload( DUpload upload )
        {
            if ( getUploads().contains( upload ) == true)
            {
                getUploads().remove( upload );
            }
            if ( upload.getEmails().contains( this ) == true)
            {
                upload.getEmails().remove( this );
            }
        }
     
    }


    DUploads
    Code java:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    
    package com.uds.webadmin.data;
     
    import java.io.Serializable;
    import java.lang.String;
    import java.util.Date;
    import java.util.List;
     
    import javax.persistence.*;
     
    import static javax.persistence.TemporalType.TIMESTAMP;
    import javax.xml.bind.annotation.XmlRootElement;
     
    /**
     * Entity implementation class for Entity: email_uploads
     */
    @Entity
    @Table( name = "email_uploads" )
    @XmlRootElement
    @NamedQuery( name = "findUploads", query = "SELECT e FROM DUpload e ORDER BY e.creation_date DESC" )
    public class DUpload
        implements Serializable
    {
     
        @Id
        @GeneratedValue( strategy = GenerationType.AUTO )
        @SequenceGenerator( name = "email_uploads_id_seq", sequenceName = "email_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 status;
     
        @Column
        @Temporal( TIMESTAMP )
        private Date creation_date;
     
        @Column
        private String editor;
     
        @Column
        @Temporal( TIMESTAMP )
        private Date edit_date;
     
        @ManyToMany( mappedBy = "uploads" )
        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.status = 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 getStatus()
        {
            return status;
        }
     
        public void setStatus( Boolean status )
        {
            this.status = status;
        }
     
        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;
        }
     
        public List<DEmail> getEmails()
        {
            return emails;
        }
     
        public void setEmails( List<DEmail> emails )
        {
            this.emails = emails;
        }
        
    }

    Wenn ich jetzt aber den Select "SELECT e FROM DEmail e ORDER BY e.creation_date DESC" abfuere werden die zugewiesenen Uploads aus der "List<DUpload> uploads" nicht mit aufgeführt. Mein Ergbnis sieht wie folgt aus, also ohne die Uploads:

    Code xml:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    
    <dEmails>
        <dEmail>
            <creation_date>2011-12-22T14:44:25.045+01:00</creation_date>
            <creator>Andy</creator>
            <email_id>151</email_id>
            <email_text>test test</email_text>
            <email_type>1</email_type>
            <headline>testAW</headline>
            <self_copy>false</self_copy>
            <to_recipients>test@web.de</to_recipients>
        </dEmail>
    </dEmails>


    ich möchte aber haben, dass die zugewiesenen Uploads auch aufgelistet werden. Sollte also wie folgt ausehen:

    Code xml:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    
    <dEmails>
        <dEmail>
            <creation_date>2011-12-22T14:44:25.045+01:00</creation_date>
            <creator>Andy</creator>
            <email_id>151</email_id>
            <email_text>test test</email_text>
            <email_type>1</email_type>
            <headline>testAW</headline>
            <self_copy>false</self_copy>
            <to_recipients>test@web.de</to_recipients>
            <dUploads>
                <dUpload>
                    <creation_date>2011-12-22T14:41:28.613+01:00</creation_date>
                    <creator>Andy</creator>
                    <edit_date>2011-12-22T14:41:28.613+01:00</edit_date>
                    <editor>Andy</editor>
                    <status>true</status>
                    <upload_id>1</upload_id>
                    <upload_link>2011_12_22_14_41_28_test.java</upload_link>
                    <upload_name>test</upload_name>
                    <upload_type>1</upload_type>
                </dUpload>
                <dUpload>
                    <creation_date>2011-12-22T14:41:28.613+01:00</creation_date>
                    <creator>Andy</creator>
                    <edit_date>2011-12-22T14:41:28.613+01:00</edit_date>
                    <editor>Andy</editor>
                    <status>true</status>
                    <upload_id>2</upload_id>
                    <upload_link>2011_12_22_14_41_28_test2.java</upload_link>
                    <upload_name>test2</upload_name>
                    <upload_type>1</upload_type>
                </dUpload>
            </dUploads>
        </dEmail>
    </dEmails>

    Wie bekome ich das hin? Sorry ist vielleicht recht einfach zu lösen, bin aber recht neu in dem Thema JPA.

    Danke
    Andy
     

  2. #2
    Andyx1975 Andyx1975 ist offline Mitglied Silber
    Registriert seit
    Nov 2009
    Beiträge
    66
    kann keiner weiter helfen? Danke******
     

  3. #3
    sebastianb sebastianb ist offline Mitglied Gold
    Registriert seit
    Dec 2009
    Beiträge
    125
    Hi,

    vll hilft ja folgendes:

    Code java:
    1
    
    @ManyToMany(mappedBy = "uploads", fetch=FetchType.EAGER)

    Gruß Sebastian
     

Ähnliche Themen

  1. JPA EclipseLink HashMap persistieren
    Von Cajus im Forum Java
    Antworten: 0
    Letzter Beitrag: 24.03.10, 23:24
  2. EclipseLink Mapping und Polymorphie
    Von sofastar im Forum Enterprise Java (JEE, J2EE, Spring & Co.)
    Antworten: 0
    Letzter Beitrag: 11.01.09, 09:46
  3. Hibernate 3 Tabellen in manytomany
    Von maxpade im Forum Java
    Antworten: 1
    Letzter Beitrag: 19.11.08, 11:02
  4. Problem mit Hibernate ManyToMany Mapping
    Von MS-Tech im Forum Java
    Antworten: 0
    Letzter Beitrag: 05.08.08, 11:38
  5. @ManyToMany mit sich selbst
    Von shaddyMS im Forum Enterprise Java (JEE, J2EE, Spring & Co.)
    Antworten: 2
    Letzter Beitrag: 22.08.07, 09:39