aspose file tools*
The moose likes Object Relational Mapping and the fly likes Define OneToMany table column as part of composite key (primary-key class) query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Define OneToMany table column as part of composite key (primary-key class) query" Watch "Define OneToMany table column as part of composite key (primary-key class) query" New topic
Author

Define OneToMany table column as part of composite key (primary-key class) query

Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
Hi All,

I am wondering whether it is possible to define a OneToMany column field as part of a composite key (primary-key class) such as the following:

[code]@Entity
@Table(name="EMPLOYEE")
@IdClass(EmployeePK.class)
public class Employee implements Serializable {

@GeneratedValue(strategy = GenerationType.IDENTITY)

@Id
@Column(name="NUMBER")
private int number;
public int getNumber() { return number; }
public void setNumber(int number) { this.number = number; }

@Id
@Column(name="SURNAME")
private String surname;
public String getSurname() { return surname; }
public void setSurname(String surname) { this.surname = surname; }

@Id
@Column(name="SKILLSET")
@OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
@JoinTable(name="EMPLOYEE_SKILLSET",
joinColumns={@JoinColumn(name="EMPLOYEE_ID")},
inverseJoinColumns={@JoinColumn(name="SKILLSET_ID")})
private Collection<Skillset> skillsets = new ArrayList<Skillset>();
public Collection<Skillset> getSkillsets() { return skillsets; }

public void setSkillsets(Collection<Skillset> skillsets) { this.skillsets = skillsets; }

.....
}

public class EmployeePK implements java.io.Serializable
{
private int employee_number;
private String surname;
private String skillset;

public EmployeePK() {}

public EmployeePK(int employee_number, String surname, String skillset)
{
this.employee_number = employee_number;
this.skillset = skillset;
this.surname = surname;
}

public int getEmployee_number() { return this.employee_number; }

public void setEmployee (int employee_number) { this.employee_number = employee_number; }

public String getSkillset() { return this.skillset; }

public void setSkillset(String skillset) { this.skillset = skillset; }

public String getSurname() { return this.surname; }

public void setSurname(String surname) { this.surname = surname; }

public boolean equals(Object obj) { ... }

public int hashcode() { .... }
}[/code]
There are plenty of material on setting up composite key (primary-key class) but most of them uses basic data types.

I was hoping that Glassfish v3 was going to support other type of table column field such as collection type but it turn out not to support EJB altogether. It would be ideal to have a JPA that support this functionality which will eliminiate some cross table matching resulting in much simpler codes.

I am running JDK1.6.07, Glassfish v2r2 together with MySQL, Netbeans on Windows XP.

Any assistance would be greatly appreciated.

Many thanks,

Jack
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Define OneToMany table column as part of composite key (primary-key class) query
 
Similar Threads
Problem in many to one relation.
ORA-02291: integrity constraint, can not find parent key
how to persist an object of a subclass from an existing super class entity object?
toString
Need help to correct @JoinColumn mapping in entity class with composite keys