-->
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.  [ 4 posts ] 
Author Message
 Post subject: OneToMany Foreign Key not being persisted
PostPosted: Fri May 20, 2011 5:32 am 
Newbie

Joined: Wed Dec 19, 2007 6:49 am
Posts: 8
Location: London
Hi,

I have a simple oneToMany relationship which I am trying to map with annotations. I have two tables, ACCOUNT and USER, and two model objects, Account and User. Account has a set of User objects which are related to the account by an account_id foreign key. My schema looks like this -

Code:
ACCOUNT (ID)
USER(ID, ACCOUNT_ID, USERNAME)


Where ACCOUNT_ID is an fk referencing ACCOUNT(ID)

My Account class looks like this -

Code:
@Entity
@Table(name="ACCOUNT")
public class Account {
    @Id
    @Column(name="ID")
    private int id;

    @OneToMany(cascade=CascadeType.ALL, mappedBy="account")
    @Column(name="ACCOUNT_ID", nullable = false)
    private Set<User> users;

public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Set<User> getUsers() {
        return users;
    }

    public void setUsers(Set<User> users) {
        this.users = users;
    }

   ....
   ..........
}


My User class -

Code:

@Entity
@Table(name="USER")
public class User implements UserDetails {
    @Id
    @Column(name="ID")
    private int id;

@ManyToOne
    @JoinColumn(name="ACCOUNT_ID")
    private Account account;

public Account getAccount() {
        return account;
    }

    public void setAccount(Account account) {
        this.account = account;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
   ...
   ........
}


I am using the Spring HibernateTemplate in a Dao object to save, read, ect... heres my save method -

Code:
....
public void saveAccount(final Account account) {
        hibernateTemplate.saveOrUpdate(account);
    }
.....


I am running an integration test which simply creates a new account, a new user, adds that user to the account object and saves the account object. When I call saveAccount from my intgration unit test the account and user objects are persisted. However, the field ACCOUNT_ID in the USER table remains NULL, the reference between USER and ACCOUNT is not maintained.

Can anyone tell me what I have wrong in my annotation mappings? I can't understand why such a simple relationship isn't working.

Thanks in advance :-)

John


Top
 Profile  
 
 Post subject: Re: OneToMany Foreign Key not being persisted
PostPosted: Fri May 20, 2011 6:32 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Are you setting the Account object on the User: user.setAccount(account)?


Top
 Profile  
 
 Post subject: Re: OneToMany Foreign Key not being persisted
PostPosted: Fri May 20, 2011 6:49 am 
Newbie

Joined: Wed Dec 19, 2007 6:49 am
Posts: 8
Location: London
@nordborg

Thanks, I've just tried that and it now works, in my Account class I set the account object when the User object is added -

Code:
public void addUser(User user) {
        if(users == null) users = new HashSet<User>();
        user.setAccount(this);
        users.add(user);
    }



However, shouldn't hibernate take care of this for me? I find it odd that I have to tell hibernate what the referencing parent object is.

Thanks again.

John


Top
 Profile  
 
 Post subject: Re: OneToMany Foreign Key not being persisted
PostPosted: Fri May 20, 2011 7:16 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
However, shouldn't hibernate take care of this for me?


Not really. What matters to Hibernate when saving is what is set on the object that owns the association. In this case it is the User that is the owner and if the account is not set it will not be saved. The Set on the Account side is only used for cascading the save from Account to User.


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

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.