JPA Entity mapping for join columns

瘦欲@ 提交于 2020-01-16 08:59:13

问题


I have three tables EmployeeDepartment, EmployeeGroup and EmpplyeeDetails Table. EmployeeDepartment table has primary key departmentId and a column groupId, EmployeeGroupTable has primary key groupid which should be generated from databse sequence GroupIdGenerator. EmployeeDetails have two primary keys as groupid and employeeid. Groupid should be same as of the previous table These values in all table should insert in one transaction. Can you help me with correct JAP Entity mapping?

I already tried with different combination of Generated value and Sequence generator but not able to save the data into table.

  @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() {
}

}

Expected result in these 3 tables should have proper values like.

Table EmployeeDepartment

DepartmentID   GroupId
   1               1

Table EmployeeGroup

 GroupID  
   1

Table EmployeeDetail

GroupId       EmployeeId
  1            1
  1            2
  1            3

Actual results are below

Table EmployeeDepartment

DepartmentID   GroupId
   1               0

Table EmployeeGroup

 GroupID  
   1

Table EmployeeDetail

GroupId       EmployeeId
  2            1
  3            2
  4            3

回答1:


The important annotation is @MapsId("groupId")

Your mapping should be something like this:

   @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;



回答2:


 @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;

You also need to change the EmployeeId accordingly:

public class EmployeeId{

private EmployeeGroup employeeGroup;

private Long employeeId;

public EmployeeId(final EmployeeGroup employeeGroup, final Long employeeId) {
    this.employeeGroup= employeeGroup;
    this.employeeId = employeeId;
}

However I haven't use composite keys this way before. If it doesn't work, then change EmployeeId to an 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;

}

If it still doesn't work, then please attach the code snippet where you create the entities.



来源:https://stackoverflow.com/questions/56580313/jpa-entity-mapping-for-join-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!