-->
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.  [ 2 posts ] 
Author Message
 Post subject: Help on one-to-many relation.
PostPosted: Thu Jun 19, 2008 1:01 am 
Newbie

Joined: Thu Jun 19, 2008 12:37 am
Posts: 1
Hi All,

I am new to hibernate.

We have a DB like this.

Companies
========
company_id(PK),
parent_company_id,
status,
...


User
=====
user_id(PK),
company_id,
....

contacts
======
contact_id,
firstname,
lastname,
companyname,
ref_object_id,
ref_object_name,
.....


Contact information for company or user will be stored in contact table with respective primary key values in "ref_object_id" column and table name in "ref_object_name" column of the contacts table.


NOTE: ref_object_id can have similar value in it for different table, means
for company 1, ref_object_id will have "1" with ref_object_name having "Companies" in it, at the same time for user 1, ref_object_id will also have "1" with ref_object_name having "User" in it.

One company/user can have multiple contacts which we have to take as a set of contacts.
How can i get the company/user contact information using set and one-to-many with contacts? because set will have one key column(which is ref_object_id in contact table). With only one key we can not relate to contact table from company/user.

Please help me in this....


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 19, 2008 4:58 am 
Newbie

Joined: Thu Jan 03, 2008 8:39 am
Posts: 5
Hi,

try this

Code:
@DiscriminatorColumn(name = "ref_object_name")
abstract class Contact {
    String name;
    ...
}

@DiscriminatorValue(value = "User")
UserContact extends Contact
{
    @ManyToOne
    @JoinColumn(name = "ref_object_id")
    User user;
}

@DiscriminatorValue(value = "Companies")
CompanyContact extends Contact
{
    @ManyToOne
    @JoinColumn(name = "ref_object_id")
    Company company;
}

class User {
    @OneToMany(mappedBy="user")
    Set<UserContact> contacts;
    ...
}


class Company {
    @OneToMany(mappedBy="company")
    Set<CompanyContact> contacts;
    ...
}


Best regards
Joerg


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.