Hi!
I'm trying one of the examples of the
Pro EJB 3 - Java Persistence API book by Mike Keith and Merrick Schincariol, specifically the one which Maps Relationship State (
Page 235). The problem is that hibernate is generating two columns instead of one for each of the fields that make the primary key.
I'm using
JBoss 4.2.2 GA with the included version of hibernate, also, I've used the latest development and production versions of hibernate:
Hibernate Core 3.3.0 CR1 &
3.2.6 GA
Hibernate Annotations 3.4.0 CR1 &
3.3.1 GA
Hibernate EntityManager 3.4.0 CR1 &
3.3.2 GA
Hibernate Validator 3.1.0 CR1 &
3.0.0 GA
The database is
MySQL 5.0.51b
I've also tried this example on
Glassfish V2 with the versions of Hibernate mentioned above with the same results.
The expected DB schema is as follows:
Code:
┌-------------┐
| EMPLOYEE |
├----┬--------┤
| PK | ID |
├----┼--------┤
| | NAME |
| | SALARY |
└----┴--------┘
┌---------------------┐
| EMP_PROJECT |
├--------┬------------┤
| PK,FK1 | EMP_ID |
| PK,FK2 | PROJECT_ID |
├--------┼------------┤
| | START_DATE |
└--------┴------------┘
┌-----------┐
| PROJECT |
├----┬------┤
| PK | ID |
├----┼------┤
| | NAME |
└----┴------┘
But the resulting DB schema is as follows:
Code:
┌-------------┐
| EMPLOYEE |
├----┬--------┤
| PK | ID |
├----┼--------┤
| | NAME |
| | SALARY |
└----┴--------┘
┌------------------┐
| EMP_PROJECT |
├-----┬------------┤
| PK | empId |
| PK | projectId |
├-----┼------------┤
| | START_DATE |
| FK1 | EMP_ID |
| FK2 | PROJECT_ID |
└-----┴------------┘
┌-----------┐
| PROJECT |
├----┬------┤
| PK | ID |
├----┼------┤
| | NAME |
└----┴------┘
The EMP_PROJECT table shouldn't have the empId and the projectId columns, instead, the EMP_ID and PROJECT_ID columns should work as composite primary key and foreign keys to the corresponding table. Even using a reference to the ProjectAssignmentId class with the @Id annotation instead of having each field of the primary with the @Id annotation results in the EMP_PROJECT table generated incorrectly.
The Employee entity:
Code:
@Entity
public class Employee {
@Id
private int id;
private String name;
private long salary;
@OneToMany(mappedBy="employee")
private Collection<ProjectAssignment> assignments;
public Employee() {
assignments = new ArrayList<ProjectAssignment>();
}
// getters & setters
}
The Project entity:
Code:
@Entity
public class Project {
@Id
private int id;
private String name;
@OneToMany(mappedBy="project")
private Collection<ProjectAssignment> assignments;
public Project() {
assignments = new ArrayList<ProjectAssignment>();
}
// getters & setters
}
The ProjectAssignment entity:
Code:
@Entity
@Table(name="EMP_PROJECT")
@IdClass(ProjectAssignmentId.class)
public class ProjectAssignment {
@Id
@Column(name="EMP_ID", insertable=false, updatable=false)
private int empId;
@Id
@Column(name="PROJECT_ID", insertable=false, updatable=false)
private int projectId;
@ManyToOne
@JoinColumn(name="EMP_ID")
Employee employee;
@ManyToOne
@JoinColumn(name="PROJECT_ID")
Project project;
@Temporal(TemporalType.TIMESTAMP)
@Column(name="START_DATE", updatable=false)
private Date startDate;
public ProjectAssignment() {}
public ProjectAssignment(Employee emp, Project proj) {
this.empId = emp.getId();
this.employee = emp;
this.projectId = proj.getId();
this.project = proj;
}
// getters & setters
}
The ProjectAssignmentId class:
Code:
public class ProjectAssignmentId implements Serializable {
private int empId;
private int projectId;
public ProjectAssignmentId() {}
public ProjectAssignmentId(int empId, int projectId) {
this.empId = empId;
this.projectId = projectId;
}
// getters, setters, hashCode & equals
}
I've tried this example on
Glassfish V2 with included
TopLink Essentials and in
JBoss 4.2.2 GA with
EclipseLink 1.0M11 and the DB schema is generated as expected.
I hope you could help me with this issue, thanks in advance.