Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Hibernate: Adding an element to a collection

Posted on 2009-05-18
2
Medium Priority
?
347 Views
Last Modified: 2013-11-11
Hi everybody,
this question is also asked on javaranch:
http://www.coderanch.com/t/445436/Object-Relational-Mapping/java/Adding-element-collection
I'm not sure whether or not I should post it again here..anyway I did.
The question itself is in the code block, any help is highly appreciated!


// I have the following 3 classes (only approproate code is shown): 
// - Vestiging 
@Entity  
@Table(name = Vestiging.TABLE)   
public class Vestiging extends VersionedEntity {   
  
    public static final String TABLE = "ORG_VESTIGING";   
  
    private Set<VestigingOmschrijving> omschrijvingen;   
    private Set<VestigingEmail> emails;   
  
    @OneToMany(fetch = FetchType.LAZY, cascade = { CascadeType.ALL })   
    @JoinColumn(name = VestigingOmschrijving.VESTIGING_ID, nullable = false)   
    public Set<VestigingOmschrijving> getOmschrijvingen() {   
        if (omschrijvingen == null) {   
            omschrijvingen = new HashSet<VestigingOmschrijving>();   
        }   
        return omschrijvingen;   
    }   
    public void setOmschrijvingen(Set<VestigingOmschrijving> omschrijvingen) {   
        this.omschrijvingen = omschrijvingen;   
    }   
  
    @OneToMany(fetch = FetchType.LAZY, cascade = { CascadeType.ALL })   
    @JoinColumn(name = VestigingEmail.VESTIGING_ID, nullable = false)   
    public Set<VestigingEmail> getEmails() {   
        if (emails == null) {   
            emails = new HashSet<VestigingEmail>();   
        }   
        return emails;   
    }   
    public void setEmails(Set<VestigingEmail> emails) {   
        this.emails = emails;   
    }   
  
    // correct implementation of hashCode and equals   
} 
//----------------------------------------------------------------- 
// VestigingOmschrijving 
@Entity  
@Table(name = VestigingOmschrijving.TABLE)   
public class VestigingOmschrijving extends VersionedEntity {   
  
    public static final String TABLE = "ORG_VESTIGING_OMSCHRIJVING";   
  
    public static final String VESTIGING_ID = "VESTIGING_ID";   
    public static final String TAAL_ID = "TAAL_ID";   
    public static final String OMSCHRIJVING = "OMSCHRIJVING";   
  
    private Taal taal;   
    private String omschrijving;   
  
    @ManyToOne  
    @JoinColumn(name = TAAL_ID)   
    public Taal getTaal() {   
        return taal;   
    }   
    public void setTaal(Taal taal) {   
        this.taal = taal;   
    }   
  
    @Column(name = OMSCHRIJVING)   
    public String getOmschrijving() {   
        return omschrijving;   
    }   
    public void setOmschrijving(String omschrijving) {   
        this.omschrijving = omschrijving;   
    }   
  
    // correct implementation of hashCode and equals   
//-----------------------------------------------------------------
//VestigingEmail 
@Entity  
@Table(name = VestigingEmail.TABLE)   
public class VestigingEmail extends VersionedEntity {   
  
    public static final String TABLE = "ORG_VESTIGING_EMAIL";   
  
    public static final String VESTIGING_ID = "VESTIGING_ID";   
    public static final String EMAIL_ID = "EMAIL_ID";   
    public static final String EMAIL = "EMAIL";   
  
    private EmailCode emailCode;   
    private String email;   
  
    @ManyToOne  
    @JoinColumn(name = EMAIL_ID)   
    public EmailCode getEmailCode() {   
        return emailCode;   
    }   
  
    public void setEmailCode(EmailCode emailCode) {   
        this.emailCode = emailCode;   
    }   
  
    @Column(name = EMAIL)   
    public String getEmail() {   
        return email;   
    }   
  
    public void setEmail(String email) {   
        this.email = email;   
    }   
  
    // correct implementation of hashCode and equals   
}  
//-----------------------------------------------------------------
 
/* When the following code is executed, i see in the console 1 insert statement and 2 update statements generated (and when i look into the database, the new VestigingOmschrijving is added to the appropriate table). so it works like a charm and does what i expect.  */
 
       
Vestiging entity = vestigingDao.findById(vestigingId); // gets the entity via the entityManager   
VestigingOmschrijving omschr = new VestigingOmschrijving();   
omschr.setTaal(taalDao.findById(taalId));   
omschr.setOmschrijving("ABCDEF");   
entity.getOmschrijvingen().add(omschr);  
    
Vestiging entity = vestigingDao.findById(vestigingId); // gets the entity via the entityManager
VestigingOmschrijving omschr = new VestigingOmschrijving();
omschr.setTaal(taalDao.findById(taalId));
omschr.setOmschrijving("ABCDEF");
entity.getOmschrijvingen().add(omschr);
 
 
/* When I try to do the same for VestigingEmail, but no insert/update statements are generated and the new email address is not added to the database. */
       
Vestiging entity = vestigingDao.findById(vestigingId); // gets the entity via the entityManager   
VestigingEmail email = new VestigingEmail();   
email.setEmailCode(emailCodeDao.findById(emailCodeId));   
email.setEmail("test@test.com");   
entity.getEmails().add(email);  
    
Vestiging entity = vestigingDao.findById(vestigingId); // gets the entity via the entityManager
VestigingEmail email = new VestigingEmail();
email.setEmailCode(emailCodeDao.findById(emailCodeId));
email.setEmail("test@test.com");
entity.getEmails().add(email);
 
 
/* I have totally no clue about why the VestigingEmail is not added, but the VestigingOmschrijving is. Code seems similar to me, so totally lost on this.  
Any help highly appreciated
*/

Open in new window

0
Comment
Question by:themuppeteer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 12

Expert Comment

by:Gibu George
ID: 24410498
0
 
LVL 2

Accepted Solution

by:
themuppeteer earned 0 total points
ID: 24411561
we found it.. apperantly a nullpointer was thrown (in a non shown method) because we used the id of a non committed entity.. the 'VestigingOmschrijving' worked because the id was not used but the value (a string)
thnx for reading this anyway!
 
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question