2

我必须用几个像这样的表映射一个遗留数据库

---------
- 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 以避免检索多个列?

4

0 回答 0