• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mapping two Hibernate Object based on their foriegn keys

 
Seetharaman Venkat
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Info Table:
InfoID
Name
Address
userID

Membership Table:
MembershipID
MembershipCode
userID

Now I am creating an Info which has a Membership

class Info{
private int infoID;
private String name;
private String address;
private Membership membership;
}

class Membership {
private int membershipID;
private String membershipCode;
private int userID;
}

Question:
In the above scenario, how do I associate the two objects based on 'userID', which is not a primary key in either tables

help/suggestions are really appreciated.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy Greenhorn! Welcome to JavaRanch.

Here's my take on a very quick glance. Why not create a User class? Is there no user table? Surely there's a user table spittin' out all of those pesky UserIds, isn't there? Then you'd have three classes, and you'd do the mapping like any other mapping between classes.

If there's no separate table, then you're really asking about mapping compound keys, right? Just tell me if I'm not right, but I'm thinking that might be what you're trying to do. Here's a little tutorial of mine on mapping compound keys:

http://www.hiberbook.com/HiberBookWeb/learn.jsp?tutorial=15usingcompoundprimarykeys

Still, I'm not sure if that's what you're trying to do. Update us.

-Cameron McKenzie
 
Seetharaman Venkat
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mr.Mckenzie,
Thank you.

Heres the scenario

Table named Feedback has an infoID
class Feedback{
private int feedbackID;
private Info info;
}

using the many-to-one mapping I was able to obtain a unidirectional one2one mapping between Feedback and Info.

Now I need to gather Membership information too, so that I can assimilate all the information and send an email reminder to the Service Desk with all the details from Feedback, Info, Membership table. The only thing common between Info and Membership is the UserID. So, I need some suggestion to implement this.

Sorry about not being clear the first time. At the moment, I am not using JPA.
 
Seetharaman Venkat
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there no way to achieve the above scenario? The silence is appalling!
 
Seetharaman Venkat
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was unable to find a solution with Hibernate, so i decided to go with JPA+Hibernate.

Now I have two issuess:
1) How to I restrict/filter, and obtain on 'non-null' values from a Tables column?
2) I still havent figured out a way to Join two tables based on a 'non-primary key' column.
3) Write an HQL query along with annotations saying "get me records with columnCode =72 " during joins; where columnCode is COLUMN_CODE (is a Secondary Table, with a one-to-one relation with a Primary Table) in a COLUMNS table.

I would apperciate if anyone could direct me in this issue! or provide me links!
 
Seetharaman Venkat
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I took me a while to figure this simple solution:
class Info{
private int infoID;
private String name;
private String address;
@OneToOne(optional = true)
@JoinColumn(name = "userID ", referencedColumnName = "userID ", insertable = false, updatable = false)
private Membership membership;
}

Now I will be looking into writing queries on Secondary tables to restrict null values on getting "columnCode =72", wish me luck
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for posting back your solution! Sometimes if feels like you're talking to yourself, but you're not. I've been reading your posts - just without anything intelligent to contribute to them. Keep up the good work!

-Cameron McKenzie
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic