Hibernate : 根据多表数据状态获取数据

Hibernate : Fetching data based on multiple table data status

提问人:KJEjava48 提问时间:11/15/2023 最后编辑:KJEjava48 更新时间:11/16/2023 访问量:45

问:

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

@Entity
@Table(name = "service_staff")
public class ServiceStaff implements Serializable {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "id")
  private long id;

  @JoinColumn(name = "service_id", referencedColumnName = "id", nullable = false)
  @OneToOne(optional = false)
  private Service serviceId;

  @JoinColumn(name = "user_id", referencedColumnName = "id", nullable = true)
  @OneToOne(optional = true)
  private User userId;

  @JoinColumn(name = "designation_id", referencedColumnName = "id", nullable = true)
  @OneToOne(optional = true)
  private Designation designationId;

  @Column(name="head")
  private boolean head;

  @Column(name="head_type")
  private int headType;

  .......
  
}

  @Entity
  @Table(name = "project_staff")
  public class ProjectStaff implements Serializable {

      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = "id")
      private long id;

      @JoinColumn(name = "project_id", referencedColumnName = "id", nullable = false)
      @OneToOne(optional = false)
      private Project projectId;

      @JoinColumn(name = "user_id", referencedColumnName = "id", nullable = true)
      @OneToOne(optional = true)
      private User userId;

      @JoinColumn(name = "designation_id", referencedColumnName = "id", nullable = true)
      @OneToOne(optional = true)
      private Designation designationId;

      @Column(name="head")
      private boolean head;

      @Column(name="head_type")
      private int headType;

      .......
      
  }

上面是我项目中的一些数据表,我想在其中查找“ServiceStaff”模型类中不存在的剩余数据列表,这些数据在 ProjectStaff 类中不存在。在 ServiceStaff 数据中,员工可能属于 User/Designation/Head 类型,其中 Designation & Head 有一个 headType,值为 1 表示“Main”,值为 2 表示“Assistant”。我怎样才能在我的 java 代码中使用负载最小的 Hibernate 获取如下结果。我是否需要使用任何联接来简化操作?请原谅我在这里描述它的方式,因为我没有找到任何其他方法来解释它。

我想要的结果如下:

enter image description here

对这一结果的解释如下:

enter image description here

java hibernate hql hibernate-mapping hibernate-criteria

评论


答: 暂无答案