-->
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: How do I map composite primary key/foreign key
PostPosted: Mon Oct 18, 2010 3:36 am 
Newbie

Joined: Wed May 28, 2008 3:56 pm
Posts: 18
I have the following table structure

[code]Year - Master Table
YearId (PK)
YearNo

Manager - Master Table
ManagerId (PK)
ManagerName

AuditDepartments - Master Table
DepartmentId (PK)
DepartmentName

YearDepartment
YearDepartmentId (PK)
DepartmentId (FK)
YearId (FK)
YearDepartmentAuditName
YearDepartmentAuditComments

DepartmentMonths
ManagerId (PK/FK)
YearDepartmentId (PK/FK)
JanAudit
FebAudit
MarAudit
AprAudit[/code]

How do I map the composite primary key of DepartmentMonths to YearDepartment and Manager using annotations ?
Do I have to use @Embeddable annotation or is there another way to do this.


Top
 Profile  
 
 Post subject: Re: How do I map composite primary key/foreign key
PostPosted: Mon Oct 18, 2010 8:02 am 
Newbie

Joined: Wed May 28, 2008 3:56 pm
Posts: 18
It is working now but the primary key should have been a composite key of managerId & yearDepartmentId

I had to battle with it a bit but adding updatable = false, insertable = false fixed it.

@Id
@GeneratedValue
@Column(name="Manager_Id")
private int managerId;

@ManyToOne
@JoinColumns( { @JoinColumn(name = "Manager_Id", referencedColumnName = "Manager_Id", updatable = false, insertable = false) })
private Manager manager;

@Column(name="Year_Department_Id")
private int yearDepartmentId;

@ManyToOne
@JoinColumns( { @JoinColumn(name = "Year_Department_Id", referencedColumnName = "Year_Department_Id", updatable = false, insertable = false) })
private YearDepartmentId yearDepartmentId;


Top
 Profile  
 
 Post subject: Re: How do I map composite primary key/foreign key
PostPosted: Mon Oct 18, 2010 2:45 pm 
Newbie

Joined: Mon Oct 18, 2010 9:35 am
Posts: 10
Hi!

I am new here also and I have a similar problem.
Maybe this could help you:
Look at 5.1.7. composite-id http://docs.jboss.org/hibernate/core/3.2/reference/en/html/mapping.html
this one also: http://docs.jboss.org/hibernate/core/3.2/reference/en/html/components.html#components-compositeid
and this one: http://docs.jboss.org/hibernate/core/3.2/reference/en/html/example-mappings.html#example-mappings-composite-key
I presume you can achieve the same with annotations.

Regards,
Despot


Top
 Profile  
 
 Post subject: Re: How do I map composite primary key/foreign key
PostPosted: Thu Oct 21, 2010 12:17 am 
Newbie

Joined: Wed May 28, 2008 3:56 pm
Posts: 18
I have fixed this as follows

[code]@Id
private DepartmentMonthsPK id;

@Embeddable
public class DepartmentMonthsPK implements Serializable{
private static final long serialVersionUID = 3543236089104499992L;

@Column(name="manager_Id")
private int managerId;

@Column(name="Year_Department_Id")
private int yearDepartmentId;

public DepartmentMonthsPK() {
}

public DepartmentMonthsPK(int managerId, int yearDepartmentId) {
// super(); Do we need this call
this.managerId = managerId;
this.yearDepartmentId = yearDepartmentId;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + managerId;
result = prime * result + yearDepartmentId;
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
DepartmentMonthsPK other = (DepartmentMonthsPK) obj;
if (managerId != other.managerId)
return false;
if (yearDepartmentId != other.yearDepartmentId)
return false;
return true;
}

@ManyToOne
@JoinColumns( { @JoinColumn(name = "Manager_Id", referencedColumnName = "Manager_Id", updatable = false, insertable = false) })
private Manager manager;

@ManyToOne
@JoinColumns( { @JoinColumn(name = "Year_Department_Id", referencedColumnName = "Year_Department_Id", updatable = false, insertable = false) })
private YearDepartmentId yearDepartmentId;
}[/code]


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.