@JoinColumns在两个表上都有复合主键

时间:2015-07-26 20:14:47

标签: hibernate one-to-many composite-primary-key jpa-2.1 joincolumn

我必须使用像这些

这样的几个表来映射遗留数据库
---------
- GROUP -
---------
+idcust -
+key    -
+idgroup- 
-domain -
---------

pk(idcust,key,idgroup)

------------------
-   GROUP_LABEL  -
------------------
+idcust          -
+key             -
+idgroup         - 
+idlabel         -
-domain          -
-description_lang-
------------------

pk(idcust,key,idgroup,idlabel)
fk(idcust,key,idgroup) references group.pk

它们都有复合主键,而后者具有第一个的所有字段。我无法更改表格,因此我不得不按原样映射它们。

以下是课程:

这是关系的一方

@Embeddable
public class GroupId implements Serializable {

  private static final long serialVersionUID = 7202018350067509393L;

  @Column(name = "idcust")
  private String idCust;
  @Column(name = "key")
  private String key;
  @Column(name = "idgroup")
  private Long idGroup;

  //getters, setters, hashcode, equals

}

@Entity
@Table(name = "group")
public class Group{

  @EmbeddedId
  private GroupId groupId;

  @Column(name = "domain")
  private String domain;

  @OneToMany(mappedBy="group", fetch=FetchType.EAGER)
  private Set<GroupLabels> groupLabels;

  //getters, setters

}

虽然这是针对很多方面的

@Entity
@Table(name = "group_labels")
public class GroupLabels{

    @EmbeddedId
    private GroupLabelsId groupLabelsId;

    @Column(name = "description_lang")
    private String descriptionLang;

    @Column(name = "domain")
    private String domain;

    @ManyToOne
    @JoinColumns({
        @JoinColumn(name="idcust", referencedColumnName="idcust" ,insertable=false, updatable=false),
        @JoinColumn(name="key", referencedColumnName="key" ,insertable=false, updatable=false),
        @JoinColumn(name="idgroup", referencedColumnName="idgroup" ,insertable=false, updatable=false)
    })
    private Group group;

//getters, setters

}   


@Embeddable
public class GroupLabelsId implements Serializable {

    private static final long serialVersionUID = 761043173785225772L;

    @Column(name = "idcust")
    private String idCust;
    @Column(name = "key")
    private String key;
    @Column(name = "idgroup")
    private Integer idGroup;
    @Column(name = "idlabel")
    private Integer idLabel;

//getters, setters, hashcode, equals

}

这种映射类工作,因为我能够检索实体,但是,我看了一下这个简单测试生成的sql:

@Test
public void testIntegrity(){
    Group got = (Group) sessionFactory.getCurrentSession().createCriteria(Group.class)
            .add(Restrictions.idEq(groupSaved.getGroupId())).uniqueResult();
    assertEquals(groupSaved.getGroupId(),got.getGroupId());

}

这是sql代码:

select
        this_.idcust as idcust1_0_1_,
        this_.idgroup as idgroup2_0_1_,
        this_.key as key3_0_1_,
        this_.domain as domain4_0_1_,
        grouplabel2_.idcust as idcust1_0_3_,
        grouplabel2_.idgroup as idgroup2_0_3_,
        grouplabel2_.key as key4_0_3_,
        grouplabel2_.idcust as idcust1_1_3_,
        grouplabel2_.idgroup as idgroup2_1_3_,
        grouplabel2_.idlabel as idlabel3_1_3_,
        grouplabel2_.key as key4_1_3_,
        grouplabel2_.idcust as idcust1_1_0_,
        grouplabel2_.idgroup as idgroup2_1_0_,
        grouplabel2_.idlabel as idlabel3_1_0_,
        grouplabel2_.key as key4_1_0_,
        grouplabel2_.description_lang as descript5_1_0_,
        grouplabel2_.domain as domain6_1_0_ 
    from
        group this_ 
    left outer join
        group_labels grouplabel2_ 
            on this_.idcust=grouplabel2_.idcust 
            and this_.idgroup=grouplabel2_.idgroup 
            and this_.key=grouplabel2_.key 
    where
        this_.idcust=? 
        and this_.key=? 
        and this_.domain=?

如你所见,它会多次检索第二个实体的ID,这让我相信我的映射是次优的。

我使用Hibernate 4.3.10作为ORM。

有没有办法可以纠正@JoinColumns以避免多列检索?

0 个答案:

没有答案