I am using hibernate to persist two tables,`Project` and `Department`.
`Department` table has a composite primary key `DeptCompID`.
@Embeddable
public class DeptCompID implements Serializable
{
@Column(name = "DeptID")
private int DeptID;
@Column(name = "RoleID")
private int RoleID;
//getters and setters
}
@Entity
public class Department implements Serializable
{
@EmbeddedId
private DeptCompID id;
private String name;
@OneToOne(mappedBy="department",targetEntity = Project.class)
private Project pro;
//getters and setters
}
@Entity
public class Project
{
@Id
private int ProId;
@OneToOne(targetEntity = Department.class)
@MapsId("DeptID")
@JoinColumns({
@JoinColumn(name = "RoleID", referencedColumnName =
"RoleID"),
@JoinColumn(name = "DeptID", referencedColumnName = "DeptID")
})
private Department department;
//getters and setters
}
**Code to persist the tables**
Department department = new Department();
department.setName("HR");
DeptCompID cpk=new DeptCompID();
cpk.setRoleID(10);
cpk.setDeptID(60);
department.setId(cpk);
Project pro=new Project();
pro.setDepartment(department);
pro.setProId(10);
department.setPro(pro);
session.save(department);
session.save(pro);
Everytime I persist the tables Project and Department the `DeptID` column in `Project`
table is always null when it should be 60.
why is `@MapsId("DeptID")` not working?Could some provide a resolution.
Posted by forums
Original post:
https://community.jboss.org/message/878168#878168