【问题标题】:Hibernate query to get child休眠查询以获取孩子
【发布时间】:2017-06-19 06:53:02
【问题描述】:

我想获取所有子用户。 我有两列父母和孩子

在父表中--

id  pname  pid(fk of parent)

1   A
2   B      1
3   C      2

在子表中---

id  cname  cid(fk of child)  pid(fk of parent)

1    AA                       1
2    BA     1                 2
3    BC     2                 2
4    CA     3                 3
5    CC     4                 3

如果父母 A -- 得到所有的孩子

如果父母 B-- 获得BA,BB,CA,CC

如果父 C-- 获取 CA、CC

我得到了答案,但它不是单个查询..还有任何休眠单个查询来做到这一点

更新........

public class Parent implements PersistEntity{

    @Id
    @Column(name = "parent_id")
    @SequenceGenerator(name="OH",sequenceName="OH", allocationSize=1)
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="OH")
    private Long parentid;

    @Column(name = "parent_name")
    private String parentName;

    @ManyToOne
    @JoinColumn(name = "fk_parent_id")
    private Parent  fkparentId;
}

public class Child implements PersistEntity{

    @Id
    @Column(name = "child_id")
    @SequenceGenerator(name="OP",sequenceName="OP",allocationSize=1)
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="OP")
    private Long childId;

    @Column(name = "child_name")
    private String childName;

    @ManyToOne
    @JoinColumn(name = "fk_child_id")
    private Child fkchildId;

    @ManyToOne
    @JoinColumn(name = "fk_office_hierarchy_parent")
    private Parent fkparentId;
}

【问题讨论】:

  • 不清楚你在问什么。但这看起来仍然是一个基本上要求将代码写入您的规范的问题。我们不是免费的代码编写服务。
  • 使用 Hibernate 时,您是针对类模型而不是表编写和编写查询。这是拥有 ORM 的原因之一。

标签: hibernate spring-boot criteria


【解决方案1】:

我在 jpa 中使用递归查询解决了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-22
    • 2013-12-12
    • 1970-01-01
    • 1970-01-01
    • 2016-08-25
    • 2010-12-06
    • 2011-04-13
    相关资源
    最近更新 更多