-->
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.  [ 3 posts ] 
Author Message
 Post subject: OneToMany Entity Association Problem , several columns keys
PostPosted: Wed Sep 15, 2010 11:09 am 
Newbie

Joined: Wed Sep 15, 2010 10:40 am
Posts: 2
Hello there,
sorry I am a beginner with Hibernate mapping system.
I have those 2 tables Teacher and Contact, a teacher can have x Contacts
SO here we are looking at a @OneToMany association.

Tables Structure:
User [userid, username, email,...]
Contact [contactid, contactname, ref, reftype,...]

You have to understand that an Contact is linked to a User thanks to the ref, reftype fields. ref being the userid, reftype='T'. As this table is used for different type of persons/entity. This way we can add contact to a "School" reftype would be S and ref would be the schoolid... The problem is I have no idea how to do this with Hibernate. I checked a lot of websites but I still don't get it and I need HELP!!
Should I use embedbedId? or a JoinCloumns?

What I have done so far is to link my teacher to contacts having contact.ref=teacher.teacherid but what I want is :
contact.ref=teacher.teacherid AND contact.reftype='T'

How do I do that?

Here is my code
Teacher.class
Quote:
private Integer teacherid;
private Set<Contact> contact;
....
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "teacherid", unique = true, nullable = false)
public Integer getTeacherId() {
return teacherid;
}

@OneToMany(fetch = FetchType.EAGER)
@JoinColumns({
@JoinColumn(name="ref"),
})
public Set<Contact> getContact() {
return contact;
}

public void setContact(Set<Contact> contact) {
this.contact = contact;
}


Contact.class
Quote:
package com.impactteachers.clientsite.domain;
@Entity
@Table(name = "contact")
public class Contact implements java.io.Serializable {

private Integer contactid;
private String contactname;
private String contacttype;
private String reftype;
private int ref;

/*private Teacher teacher;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumns({
@JoinColumn(name = "ref"),
@JoinColumn(name = "reftype")
})
public Teacher getTeacher() {
return teacher;
}
public void setTeacher (Teacher teacher) {
this.teacher= teacher;
}
*/
private Set<ContactItem> contactItems;
private Set<ContactAddress> contactAddressess;

@OneToMany(fetch=FetchType.EAGER)
@JoinColumn(name="contactid")
public Set<ContactItem> getContactItems(){
return contactItems;
}

public void setContactItems(Set<ContactItem> contactItems) {
this.contactItems = contactItems;
}

@OneToMany(fetch=FetchType.EAGER)
@JoinColumn(name="contactid")
public Set<ContactAddress> getContactAddressess(){
return contactAddressess;
}

public void setContactAddressess(Set<ContactAddress> contactAddressess) {
this.contactAddressess = contactAddressess;
}


@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "contactid", unique = true, nullable = false)
public Integer getContactid() {
return this.contactid;
}

public void setContactid(Integer contactid) {
this.contactid = contactid;
}

@Column(name = "contactname", nullable = false)
public String getContactname() {
return this.contactname;
}

public void setContactname(String contactname) {
this.contactname = contactname;
}

@Column(name = "contacttype", nullable = false)
public String getContacttype() {
return this.contacttype;
}

public void setContacttype(String contacttype) {
this.contacttype = contacttype;
}

@Column(name = "reftype", nullable = false, length = 1)
public String getReftype() {
return this.reftype;
}

public void setReftype(String reftype) {
this.reftype = reftype;
}

@Column(name = "ref", nullable = false)
public int getRef() {
return this.ref;
}

public void setRef(int ref) {
this.ref = ref;
}

public String toString(){
return "\n#"+this.contactname+" : ("+this.ref+"-"+this.reftype+") \n" +
"#Items-----\n"+getContactItems()+"\n" +
"#Address---\n"+getContactAddressess()+"\n";

}

}


Top
 Profile  
 
 Post subject: Re: OneToMany Entity Association Problem , several columns keys
PostPosted: Tue Sep 21, 2010 5:03 am 
Newbie

Joined: Wed Sep 15, 2010 10:40 am
Posts: 2
No one can help me really?
Please?!


Top
 Profile  
 
 Post subject: Re: OneToMany Entity Association Problem , several columns keys
PostPosted: Tue Oct 05, 2010 5:09 pm 
Beginner
Beginner

Joined: Fri Mar 11, 2005 7:46 am
Posts: 29
Are you saying that ref column is a reference to primary keys from several tables? If so, it is not really a foreign key relationship and you should rethink your model if possible.


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