@ManyToOne使用@JoinColumnOrFormula检索包含部分键的对象

时间:2016-05-10 15:56:16

标签: hibernate

我在使用@JoinColumnOrFormula使用密钥的一部分检索特定数据时遇到问题。 让我们试着解释一下背景。

数据库中有两个表彼此没有依赖关系。

以下是这些表的定义:



    @Entity
    @Table(name = "table_A")
    @IdClass(TableAPK.class)
    public class TableA {

        @Id
        @Column(name = "tableA_key")
        private String key;

        @Id
        @Column(name = "tableA_product")
        private String product;

        @Id
        @Column(name = "tableA_territory")
        private String territory;

        @Column(name = "tableA_description")
        private String description;

        @OneToMany(fetch = FetchType.EAGER, targetEntity = TableB.class)
        private List listTableB;

        // getter/setter
    }



    @IdClass(TableAPK.class)
    public class TableAPK implements Serializable {

        private static final long serialVersionUID = -6343683597717600643L;

        private String key;

        private String product;

        private String territory;

        // getter/setter
    }



    @Entity
    @Table(name = "table_B")
    @IdClass(TableBPK.class)
    public class TableB {

        @Id
        @Column(name = "tableB_key")
        private String key;

        @Id
        @Column(name = "tableB_product")
        private String product;

        @Id
        @Column(name = "tableB_region")
        private String region;

        @Column(name = "tableB_description")
        private String description;

        @ManyToOne(targetEntity = TableA.class)
        @JoinColumnsOrFormulas(value = {
          @JoinColumnOrFormula(column = @JoinColumn(name = "tableB_key", referencedColumnName = "tableA_key", insertable = false, updatable = false, nullable = false)),
          @JoinColumnOrFormula(column = @JoinColumn(name = "tableB_product", referencedColumnName = "tableA_product", insertable = false, updatable = false, nullable = false)),
          @JoinColumnOrFormula(formula = @JoinFormula(value = "????", referencedColumnName = "????"))
        })
        private TableA tableA;

        // getter/setter
    }



    @IdClass(TableBPK.class)
    public class TableBPK implements Serializable {

        private static final long serialVersionUID = -6343683597717600643L;

        private String key;

        private String product;

        private String region;

        // getter/setter
    }

如您所见,TableAPK和TableBPK的主键不是由相同的属性组成。



    List listElements = em.createQuery("FROM TableA WHERE key = :key ORDER BY territory", TableA.class).setParameter("key", key).getResultList();

此查询应根据提供的密钥返回TableA对象列表。每个TableA都有一个TableB列表,它匹配作为密钥一部分的密钥,产品和区域。

键是一个看起来像这样的字符串" territory_region_product_xxxx"

如何检索包含' table_B.tableB_region'的TableB列表?在&table; A_tableA_key'?

例如table_A.tableA_key有 " territoryA_regionA_productA_xxxx" " territoryA_regionA_productA_xxxx" " territoryA_regionB_productA_xxxx" " territoryA_regionB_productA_xxxx"

和table_B.tableB_region有 "地域性"

在这种情况下,如何获得第一行的两个?

如何使用@JoinFormula实现此功能?

@JoinColumnsOrFormulas在这种情况下是一个很好的解决方案吗?

欢迎任何建议。

0 个答案:

没有答案