我有三个表EmployeeDepartment,EmployeeGroup和EmpplyeeDetails表。 EmployeeDepartment表具有主键departmentId和一列groupId,EmployeeGroupTable具有主键groupid,应从数据库序列GroupIdGenerator生成该主键。 EmployeeDetails有两个主键,分别是groupid和employeeid。 Groupid应与上表相同 所有表中的这些值应插入一个事务中。 您能帮我正确的JAP实体映射吗?
我已经尝试过使用生成值和序列生成器的不同组合,但是无法将数据保存到表中。
@Entity
@Table(name="EMPLOYEE_DEPARTMENT")
public class EmployeeDepartment {
@Column(name = "DEPARTMENT_ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator =
"departmentid-gen")
@Id
@NotNull
@SequenceGenerator(name = "departmentid-gen", sequenceName =
"DEAPARTMENT_ID_GENERATOR" )
private long departmentId;
@OneToOne(mappedBy = "employeeGroup")
private EmployeeGroup employeeGroup;
}
@Coulmn(name="GROUP_ID")
private long groupId;
@Entity
@Table(name="EMPLOYEE_GROUP")
public class EmployeeGroup {
@Column(name = "GROUP_ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator =
"groupid-gen")
@Id
@NotNull
@SequenceGenerator(name = "groupid-gen", sequenceName =
"GROUIP_ID_GENERATOR" )
private long groupId;
@OneToMany(mappedBy = "employeeDetail")
private List<EmployeeDetail> employeeDetails;
@OneToOne
@JoinColumn(name = "DEPARTMENT_ID", insertable=false ,
updatable=false)
private EmployeeDepartment employeeDepatment;
}
@Entity
@Table(name = "EMPLOYEE_DETAIL")
@IdClass(EmployeeID.class)
public class EmployeeDetail {
@ManyToOne
@JoinColumn(name = "GROUP_ID", insertable=false , updatable=false)
private EmployeeGroup employeeGroup;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator =
"groupid-gen")
@SequenceGenerator(name = "groupid-gen", sequenceName =
"GROUIP_ID_GENERATOR" )
@Column(name = "GROUP_ID")
@Nonnull
private Long groupId;
@Id
@Nonnull
@Column(name = "EMPLOYEE_ID")
private Long employeeId;
}
public class EmployeeId{
private Long groupId;
private Long employeeId;
public EmployeeId(final Long groupId, final Long employeeId) {
this.groupId = groupId;
this.employeeId = employeeId;
}
public EmployeeId() {
}
}
这3个表中的预期结果应具有适当的值。
表EmployeeDepartment
DepartmentID GroupId
1 1
表EmployeeGroup
GroupID
1
表EmployeeDetail
GroupId EmployeeId
1 1
1 2
1 3
实际结果如下
表EmployeeDepartment
DepartmentID GroupId
1 0
表EmployeeGroup
GroupID
1
表EmployeeDetail
GroupId EmployeeId
2 1
3 2
4 3
答案 0 :(得分:0)
重要的注释是@MapsId("groupId")
您的映射应如下所示:
@Entity
@Table(name="EMPLOYEE_DEPARTMENT")
public class EmployeeDepartment {
@Column(name = "DEPARTMENT_ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator =
"departmentid-gen")
@Id
@NotNull
@SequenceGenerator(name = "departmentid-gen", sequenceName =
"DEAPARTMENT_ID_GENERATOR" )
private long departmentId;
@OneToOne(mappedBy = "employeeGroup")
private EmployeeGroup employeeGroup;
@Column(name="GROUP_ID")
private long groupId;
@Entity
@Table(name="EMPLOYEE_GROUP")
public class EmployeeGroup {
@Column(name = "GROUP_ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator =
"groupid-gen")
@Id
@NotNull
@SequenceGenerator(name = "groupid-gen", sequenceName =
"GROUIP_ID_GENERATOR" )
private long groupId;
@OneToMany(mappedBy = "employeeDetail")
private List<EmployeeDetail> employeeDetails;
@OneToOne
@JoinColumn(name = "DEPARTMENT_ID", insertable=false, updatable=false)
private EmployeeDepartment employeeDepatment;
@Entity
@Table(name = "EMPLOYEE_DETAIL")
@IdClass(EmployeeID.class)
public class EmployeeDetail {
@MapsId("groupId")
@ManyToOne
@JoinColumn(name = "GROUP_ID", insertable=false , updatable=false)
private EmployeeGroup employeeGroup;
@Id
@Nonnull
@Column(name = "EMPLOYEE_ID")
private Long employeeId;
答案 1 :(得分:0)
@Entity
@Table(name="EMPLOYEE_DEPARTMENT")
public class EmployeeDepartment {
@Column(name = "DEPARTMENT_ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator =
"departmentid-gen")
@Id
@NotNull
@SequenceGenerator(name = "departmentid-gen", sequenceName =
"DEAPARTMENT_ID_GENERATOR" )
private Long departmentId;
@OneToOne(mappedBy = "employeeGroup")
private EmployeeGroup employeeGroup;
}
@Column(name="GROUP_ID")
private EmployeeGroup group;
@Entity
@Table(name="EMPLOYEE_GROUP")
public class EmployeeGroup {
@Column(name = "GROUP_ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator =
"groupid-gen")
@Id
@NotNull
@SequenceGenerator(name = "groupid-gen", sequenceName =
"GROUIP_ID_GENERATOR" )
private Long groupId;
@OneToMany(mappedBy = "employeeDetail")
private List<EmployeeDetail> employeeDetails;
@OneToOne
@JoinColumn(name = "DEPARTMENT_ID", insertable=false ,
updatable=false)
private EmployeeDepartment employeeDepatment;
}
@Entity
@Table(name = "EMPLOYEE_DETAIL")
@IdClass(EmployeeID.class)
public class EmployeeDetail {
@Id
@ManyToOne
@JoinColumn(name = "GROUP_ID", insertable=false , updatable=false)
private EmployeeGroup employeeGroup;
@Id
@Nonnull
@Column(name = "EMPLOYEE_ID")
private Long employeeId;
您还需要相应地更改EmployeeId:
public class EmployeeId{
private EmployeeGroup employeeGroup;
private Long employeeId;
public EmployeeId(final EmployeeGroup employeeGroup, final Long employeeId) {
this.employeeGroup= employeeGroup;
this.employeeId = employeeId;
}
但是我以前从未以这种方式使用复合键。如果不起作用,则将EmployeeId更改为EmbeddedId:
@Embeddable
public class EmployeeId implements Serializable{
@ManyToOne(fetch = FetchType.LAZY, optional = false)
private EmployeeGroup employeeGroup;
@Nonnull
@Column(name = "EMPLOYEE_ID")
private Long employeeId;
public EmployeeId(final EmployeeGroup employeeGroup, final Long employeeId) {
this.employeeGroup= employeeGroup;
this.employeeId = employeeId;
}
@Entity
@Table(name = "EMPLOYEE_DETAIL")
public class EmployeeDetail {
@EmbeddedId
private EmployeeId id;
@ManyToOne
@JoinColumn(name = "GROUP_ID", insertable=false , updatable=false)
private EmployeeGroup employeeGroup;
}
如果仍然无法使用,请在创建实体的地方附加代码段。