-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: OneToMany relation - strange behaviour
PostPosted: Fri Jun 20, 2014 8:27 am 
Newbie

Joined: Thu Oct 29, 2009 2:59 am
Posts: 10
Hello,

I habe a one to many relation ship between Customer and Contact and Customer and Bank.

Code:
                                    n
              1         |------------Contact
Customer <----------|
                        |------------Bank
              1                             n


Now I have a method called mergeCustomer (merges existing customer and customer in parameter) in one of my DAO- classes. In this method I will set the (existing)customer for the banks and the contacts to prevent the Exception "cannot insert NULL into CUSTOMER_ID":

Code:
@Override
    public Customer mergeCustomer(final Customer customer) {
        for (final Iterator<BankAccount> iterator = customer.getBankAccounts().iterator(); iterator.hasNext();) {
            final BankAccount bankAccount = iterator.next();
            if (bankAccount.getCustomer() == null) {
                bankAccount.setCustomer(customer);
            }
        }
        for (final Iterator<Contact> iterator = customer.getContacts().iterator(); iterator.hasNext();) {
            final Contact contact = iterator.next();
            if (contact.getCustomer() == null) {
                contact.setCustomer(customer);
            }
        }

        Customer customerChanged = this.merge(customer);  // HibernateDAOBase == this
        return customerChanged;
    }


In my JUnit TEST- class I invoke this method like this:

Code:
        Bank bank = new Bank();
        bank .setBankName("new bank");
        // bank .setCustomer(customer); // must!! be done before add
        customerSaved.getBanks().add(bank );

        // 2. new contact
        final Contact contact = new Contact();
        contact.setFirstName("Vladimir");
        // contact.setCustomer(customer); // must!! be done before add - strange
        customerSaved.getContacts().add(contact);

        final Customer customerChanged = customerDAO.mergeCustomer(customer);


What is strange for me is, when

Code:
bankAccount.setCustomer(customer);
and
contact.setCustomer(customer);

are active (not commented out) than everything works fine, when th two lines are not active, than I get an exception:

Code:
Cannot insert NULL into column CUSTOMER_ID of Bank


although I set the customer in my DAO class before invoking merge.
Does anybody have any idea why this happens.
It looks like that all has to be set into th brand new object, after


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.