views:

537

answers:

1

Hello good people!

i've hit a block once again with hibernate.I've posted numerous times on different aspects of the user and contact management that i've been building.

The sad thing is that i didn't really have the time to play with it and understand it better before actually starting working with it. Sorry but English is not my native language, i rather speak french. And again i've started coding in java in an autodidact way.i'm doing all of this by reading books and haven't gone to school for it. with time constraints it's hard to read a book from beginning to the end.
I'm not sure i should put every of my codes dealing with an issue here and from what i've learned from other forum is to post just the necessary and being concise.

So in my User model i have UserAccount class, Profile that holds details like name, preferences etc , AccountSession and Phone. my contact management model have Contact and Group.

UserAccount has one-to-one association with Profile, one-to-many with AccountSession,contact and group, all bidirectional.the one-to-many association with phone is unidirectional because contact also has and unidirectional with Phone.

Contact has a bidirectional many-o-many with group and one-to-many with phone that i said earlier. Group also has a many-to-many bedirectional with contact.

here are the mappings

// UserAccount 
 ......
 @OneToOne(targetEntity=UserProfileImpl.class,cascade={CascadeType.ALL})
 @org.hibernate.annotations.Cascade(value=org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
@JoinColumn(name="USER_PROFILE_ID")
private UserProfile profile;

@OneToMany(targetEntity=ContactImpl.class, cascade={CascadeType.ALL}, mappedBy="userAccount")
@org.hibernate.annotations.Cascade(value=org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
private Set<Contact> contacts = new HashSet<Contact>();

@OneToMany(targetEntity=GroupImpl.class, cascade={CascadeType.ALL}, mappedBy="userAccount")
@org.hibernate.annotations.Cascade(value=org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
private Set<Group> groups = new HashSet<Group>();
.......

//Group
     @ManyToOne(targetEntity=UserAccountImpl.class)
@JoinColumn(name="USER_ACCOUNT_ID",nullable=false)
private UserAccount userAccount;

@ManyToMany(targetEntity=ContactImpl.class,cascade={CascadeType.PERSIST, CascadeType.MERGE})
@JoinTable(name="GROUP_CONTACT_MAP", joinColumns={@JoinColumn(name="GROUP_ID")},
inverseJoinColumns={@JoinColumn(name="CONTACT_ID")})
private Set<Contact> contacts = new HashSet<Contact>();

//Contact
 ....
 @ManyToOne(targetEntity=UserAccountImpl.class)
@JoinColumn(name="USER_ACCOUNT_ID",nullable=false)
private UserAccount userAccount;

@ManyToMany(targetEntity=GroupImpl.class, mappedBy="contacts")
private Set<Group> groups=new HashSet<Group>();
....

// helper methods from group
public void addContact(Contact contact) {
    try{
        this.getContacts().add(contact);
        contact.getGroups().add(this);
    }catch(Exception e) {

    }

}

//helper method from group
public void removeContact(Contact contact) {
    contact.getGroups().remove(contact);
    this.getContacts().remove(contact);

}

//helper method from contact   
public void addGroup(Group group) {
    try{
        this.getGroups().add(group);
        group.getContacts().add(this);
    } catch(Exception e) {
        e.printStackTrace();
    }
}

//Helper method from group
public void removeGroup(Group group){
     try{

        group.getContacts().remove(this);
        this.getGroups().remove(group);
    } catch(Exception e) {
        e.printStackTrace();
    }
}

//UserAccount setter from Contact.All the children with many-to-one have the same /** * @param userAccount the userAccount to set */ public void setUserAccount(UserAccount userAccount) { this.userAccount = userAccount; }

I'ld like to pull the UserAccount by its email field which is an unique field in the UserAccount table.
In the UserAccountDAO the method i call to get the UserAccount is getUserAccountByEmail here below.So i expect this method to load all the children collections of the UserAccount namely its Contact collection, group collection.I want it in such a way that when UserAccount is loaded with Contacts collection each of the contact object has its reference with its belonging groups collection if any etc and vice versa.

 public UserAccount getUserAccountByEmail(String email) {
 //       try {
       logger.info("inside getUserAccountByEmail");
       logger.debug(email);
        Session session = (Session) this.getDBSession().getSession();
        UserAccount user = (UserAccount) session.createCriteria(this.getPersistentClass())
                .setFetchMode("contacts", FetchMode.SELECT) //recently added
                .setFetchMode("groups", FetchMode.SELECT) // recently added
                .add(Restrictions.eq("email", email))
                .uniqueResult();
        logger.debug(user);
        return user;
 //       } catch(NonUniqueResultException ne) {
//           logger.debug("Exception Occured: getUserAccountByEmail returns more than one result  ", ne);
//           return null;
//       } catch(HibernateException he){
//           logger.debug("Exception Occured: Persistence or JDBC exception in method  getUserAccountByEmail ",he);
//           return null;
//       }catch(Exception e) {
//           logger.debug("Exception Occured: Exception in method getUserAccountByEmail", e);
//           return null;
//       }

Since there has to be an UserAccount before any contact and groups, in my unit test when testing the saving of a contact object for which there must be an existing group i do this in order
a create userAccount object ua.
b create group object g1;
c create contact object c1;
d ua.addGroup(g1);
e c1.setUserAccount(ua);
f c1.addGroup(g1);
g uaDao.save(ua); // which saves the group because of the cascade
h cDao.save(c1);

Most of the time i use the session.get() from hibernate to pull c1 by its it id generated by hibernate and do all the assertions which works actually.

but in Integration test when i call getUserAccountByEmail with and without the setFetchMode and it returns the right object but then all the children collections are empty. i've tried the JOIN and the SELECT.the query string changes but then the result set is still the same. So this arises some questions :

1. What should i do to fix this?
2. the helper method works fine but it's on the parent side(i do it in the test).What i've been wondering about is that doing c1.setUserAccount(ua); is enough to create a strong relationship between UserAccount and contact.most of the time there will not be cases where i save the userAccount with contact but yet the helper method that set the association in both side and which is in UserAccount will not been called before i save the contact for a particular userAccount.So i'm little confused about that and suspecting that setting of the association is part of the why something is not working properly.and then calling session.get(UserAccount.class, ua.getID()) i think goes what i want and i'ld like getUserAccountByEmail to do the same.
3. ChssPly76 thinks the mapping has to be rewrite.So i'm willing to let you guide me through this.I really need to know the proper way to do this because we can't lean everything from a good book.So i you think i should change the mapping just show me how.and probable i'm doing things the wrong way without even been aware of that so don't forget i'm still learning java itself.THanks for the advise and remarks and thanks for reading this

A: 

I agree with you that it seems likely that the associations between your parent objects and their child collections are not getting persisted properly. I always like to start out by looking at what is in the database to figure out what's going on. After you run your test what do you see in the actual database?

It seems likely that one of two things is happening (using UserAccount as an example):

  1. The items in the child collection are not getting saved to the database at all, in which case you'll be able to see in the database that there are no records associated with your UserAccount. This could be caused by saving the UserAccount object before you've added the child object to the UserAccount's collection.

  2. The items in the child collection are getting saved to the database, but without the needed association to the parent object, in which case you'll see rows for your child items but the join column (ie 'userAccount' will be null). This could be caused by not setting the userAccount() property on the child object.

These are the two scenarios that I've run into where I've seen the problem you describe. Start by taking a look at what goes into your database and see if that leads you farther.

BryanD
thanks bryanD for your suggestions
black sensei