我在Employee
和SkillSet
表之间存在多对多关系,每个关系都有额外的列numberOfYears
employeeId skillSetId numberOfYears
10 101 2
我是JPA的新手,无法定义具有关系的实体。我应该为Employee_SkillSet
表定义一个新的实体类吗?或者我可以在Employee
和SkillSet
类中定义多对多的关系吗?我在哪里指定numberOfYears
列?
编辑:似乎重复,但我明确要求使用@IdClass
,其中一个实体是@MappedSuperclass
,因此必须同时定义ID实例和引用的实体对象。
答案 0 :(得分:1)
由于你需要为元组(Employee,SkillSet)添加一个额外的字段,你必须创建另一个实体。
@Entity
public class Employee implements Serializable {
private @Id Long id;
@OneToMany(mappedBy="employee")
private List<EmployeeSkillSet> skillSets;
}
@Entity
public class SkillSet implements Serializable {
private @Id Long id;
}
@Entity
public class EmployeeSkillSet implements Serializable {
private @Id Long id;
private @ManyToOne Employee employee;
private @ManyToOne SkillSet skillSet;
private @Basic int numberOfYears;
}
当然,您可以选择使用@IdClass
来制作(&#34;员工&#34;,&#34;技能组&#34;)EmployeeSkillSet
的主键,如下所示:< / p>
@Entity @IdClass(EmployeeSkillSet.Key.class)
public class EmployeeSkillSet implements Serializable {
private @Id @ManyToOne Employee employee;
private @Id @ManyToOne SkillSet skillSet;
private @Basic int numberOfYears;
public static class Key implements Serializable {
private Long employee; // plus getter+setter
private Long skillSet; // plus getter+setter
// plus hashCode, equals
}
}
答案 1 :(得分:1)
虽然您可以使用@ManyToMany注释更好地出于性能原因定义两个多对一关系,以便为多对多关系建模。
你需要4个工件,有
代码将是这样的
<强>员工强>
:: Now execute the rest of the arguments, if any:
shift
if @%1 == @ goto :eof
set command=
:BuildCommand
if @%1 == @ goto :CommandFinished
set "command=%command% %1"
shift
goto :BuildCommand
:CommandFinished
%command%
<强>技能组强>
package <your_package>;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/**
*
*/
@Entity
@Table(name="EMPLOYEE")
public class Employee implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="EMPLOYEEID")
private int id;
// Rest of columns and getter and setters for all
}
<强> EmployeeSkillSetPK 强>
package <your_package>;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/**
*
*/
@Entity
@Table(name="SKILLSET")
public class SkillSet implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="SKILLSETID")
private int id;
// Rest of columns and getter and setters for all
}
<强> EmployeeSkillSet 强>
/**
*
*/
package <your_package>;
import java.io.Serializable;
import javax.persistence.Embeddable;
/**
*
*/
@Embeddable
public class EmployeeSkillSetPK implements Serializable {
@ManyToOne
private Employee emp;
@ManyToOne
private SkillSet sk;
/**
* @return the employee
*/
public Employee getEmployee() {
return emp;
}
/**
* @param employee the employee to set
*/
public void setEmployee(Employee employee) {
this.employee = employee;
}
/**
* @return the sk
*/
public SkillSet getSkillSet() {
return sk;
}
/**
* @param sk the sk to set
*/
public void setSkillSet(SkillSet sk) {
this.sk = sk;
}
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
EmployeeSkillSetPK that = (EmployeeSkillSetPK) o;
if (employee != null ? !employee.equals(that.employee) : that.employee != null) {
return false;
}
if (sk != null ? !sk.equals(that.sk) : that.sk != null) {
return false;
}
return true;
}
public int hashCode() {
int result;
result = (employee != null ? employee.hashCode() : 0);
result = 31 * result + (sk != null ? sk.hashCode() : 0);
return result;
}
}
这是针对JPA 1.0的,我现在无法测试代码,但它应该可以正常工作。
请注意,我自己编写了表名和列名。根据需要调整它。