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: Mapping problems, extending questions
PostPosted: Tue Sep 19, 2006 9:16 am 
Newbie

Joined: Tue Sep 19, 2006 8:50 am
Posts: 7
Hi,

I'm in a bit of a pickle with an relation mapping that I can't get working properly.

--------------------------------------------------------------

one User -----> many Account

one Account ------> one User

AAccount and BAccount extends Account

-----------------------------------------------------------------

* An user can have one AAccount and one BAcount
* One Aaccount/BAccount can only belong to one User

User: one-to-many -> Account
Account: ?

I don't know if the User in Account should be mapped with one-to-one (as only one instance of AAccount or BAccount are 'allowed' in User) or if it is the super class that has an many-to-one (as in this case two Account objects can have relations with the User)

I've almost got this working with
User: many-to-many to account
Account: one-to-one to User
but I'm having problems with getting null objects when trying to access account.getUser(); despite that the database looks correct.

Grateful for any help I can get.

/Manny


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 19, 2006 11:01 am 
Beginner
Beginner

Joined: Thu Aug 24, 2006 6:01 am
Posts: 49
Location: sophia-antipolis, France
Hi Manny,

what you have is:

User: OneToMany to Account
Account: ManyToOne to User

You mustn't use OneToOne on the Account-side.

Here's an example:

I have a ManyToOne relationship between Item and Definition. I have annotated both ends of the relationship:

Code:
class Definition {
    private List<Item> items;

    @OneToMany(mappedBy="definition")
    public List<Item> getItems() {
        return items;
    }
}

class Item {

     private Definition definition;
    @ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
    @JoinColumn(name="DEFINITION_ID")
    public Definition getDefinition() {
        return definition;
    }
}


In your case, it's the same with Item = Account and Definition = User.

hope this helps.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 21, 2006 6:17 am 
Newbie

Joined: Tue Sep 19, 2006 8:50 am
Posts: 7
Hi wykoff,

thx for pushing me in the right direction. This code worked for me:

User-side:
Code:
...
<set name="accounts" table="accounts" cascade="all" inverse="true">
   <key column="userId" />
   <one-to-many class="model.Account" />
</set>



Account-side:
Code:
<class name="Account" table="account">
   <id name="id" unsaved-value="null">
      <generator class="native"/>
   </id>
   <property name="type" />
   <many-to-one column="userId" name="user" />
      
   <joined-subclass name="model.TestAccount" table="test_account">
      <key column="id" />
      <one-to-one name="cart" cascade="all" />
   </joined-subclass>      
</class>


Still I think that to get an one-to-one working from a subclass would save me the userId column. :)

/Manny


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 22, 2006 11:15 am 
Beginner
Beginner

Joined: Thu Aug 24, 2006 6:01 am
Posts: 49
Location: sophia-antipolis, France
Hi Manny

sorry, but I lost this post. I didn't understand exactly what you wanted before, but I think I understand now. It looks to me like you want something like this:

Code:
class User {
    private List<Account> Accounts;

    @OneToMany(mappedBy="user")
    public List<Account> getAccounts() {
        return Accounts;
    }
}

class abstract Account {

    protected User user;

    @Transient
    public abstract User getUser();
}

class AAccount extends Account {
    @ManyToOne
    public User getUser() {
        return user;
    }
}
class BAccount extends Account {
    @ManyToOne
    public User getUser() {
        return user;
    }
}



I'm sorry, but I don't know how to do the mapping in the config file, but perhaps this will give you the right idea.


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.