2

我有这五张桌子:

REF_CT          REF_CT_L        REF_ST        REF_ST_L
======          ========        ======        ========
CT_ID           CT_ID           ST_ID         ST_ID
ST_ID           LANGUAGE_ID     NAME          LANGUAGE_ID
NAME            NAME            REF_CODE      NAME
REF_CODE        DESCRIPTION                   DESCRIPTION


REF_LANGUAGE
============
LANGUAGE_ID
LOCALE_CODE
NAME

通过使用 JPA,我创建了这三个实体类:

  1. 类 common.contact.City

    @Entity
    @Table(name = "REF_CT")
    @SecondaryTable(name = "REF_CT_L")
    public class City implements Reference {
       @Id
       @Column(name = "CT_ID")
       @TableGenerator(name = "TABLE_GEN", table = "SYS_SEQ",
            pkColumnName = "SEQ_NAME", valueColumnName = "SEQ_VALUE", pkColumnValue =   "CITY_SEQ")
       @GeneratedValue(strategy = GenerationType.TABLE, generator = "TABLE_GEN")
       private Integer id;
    
       @Column(name = "NAME")
       private String name;
    
       @Column(table = "REF_CT_L", name = "NAME")
       private String value;
    
       @Column(name = "REF_CODE")
       private String refCode;
    
       @ManyToOne(targetEntity = Language.class)
       @JoinColumn(table = "REF_CT_L", name = "LANGUAGE_ID")
       private Language language;
    
       @ManyToOne(targetEntity = State.class, optional = true)
       @JoinColumn(name = "ST_ID", referencedColumnName = "ST_ID")
       private State state;
    
  2. 类 common.contact.State

    @Entity
    @Table(name = "REF_ST")
    @SecondaryTable(name = "REF_ST_L")
    public class State implements Reference {
       @Id
       @Column(name = "ST_ID")
       @TableGenerator(name = "TABLE_GEN", table = "SYS_SEQ",
            pkColumnName = "SEQ_NAME", valueColumnName = "SEQ_VALUE", pkColumnValue = "STATE_SEQ")
       @GeneratedValue(strategy = GenerationType.TABLE, generator = "TABLE_GEN")
       private Integer id;
    
       @Column(name = "NAME")
       private String name;
    
       @Column(table = "REF_ST_L", name = "NAME")
       private String value;
    
       @Column(name = "REF_CODE")
       private String refCode;
    
       @OneToMany(targetEntity = City.class, mappedBy = "state")
       private Set<City> cities;
    
       @ManyToOne(targetEntity = Language.class)
       @JoinColumn(table = "REF_ST_L", name = "LANGUAGE_ID", unique = true, referencedColumnName = "LANGUAGE_ID")
       private Language language;
    
  3. 类 common.system.Language

    @Entity
    @Table(name = "SYS_LANGUAGE")
    public class Language implements BaseEntity {
       @Id
       @Column(name = "LANGUAGE_ID")
       @TableGenerator(name = "TABLE_GEN", table = "SYS_SEQ",
            pkColumnName = "SEQ_NAME", valueColumnName = "SEQ_VALUE", pkColumnValue = "LANGUAGE_SEQ")
       @GeneratedValue(strategy = GenerationType.TABLE, generator = "TABLE_GEN")
       private Integer id;
    
       @Column(name = "LOCALE_CODE")
       private String locale;
    
       @Column(name = "NAME")
       private String name;
    

但是当我尝试运行这个查询时

select city from City city where city.id = 1 

我收到此错误:

org.hibernate.HibernateException: More than one row with the given identifier was found: 10, for class: common.contact.State

仅供参考,添加以下数据:

REF_CT

CT_ID    ST_ID    NAME    REF_CODE
=====    =====    ====    ========
  1        10    CHERAS     CHE

REF_CT_L

CT_ID    LANGUAGE_ID    NAME       DESCRIPTION
=====    ===========    ====       ===========
  1           1      BANDAR CHERAS  CHERAS BARU
  1           2      CHERAS TOWN    NEW CHERAS  

REF_ST

ST_ID    NAME     REF_CODE
=====    ====     ========
  10    SELANGOR    SEL

REF_ST_L

ST_ID    LANGUAGE_ID     NAME      DESCRIPTION
=====    ===========     ====      ===========
  10          1        SELANGOR    SELANGOR BARU
  10          2        SERIANGOR   NEW SERIANGOR

参考语言

LANGUAGE_ID    LOCALE_CODE    NAME
===========    ===========    ====
     1           ms_En       Malay
     2           en_US       English

生成的 SQL 是:

SELECT state0_.st_id AS st1_50_0_,
       state0_.NAME AS name50_0_, state0_.ref_code AS ref4_50_0_,
       state0_1_.language_id AS language2_51_0_, state0_1_.NAME AS name51_0_
  FROM ref_st state0_ LEFT OUTER JOIN ref_st_l state0_1_
       ON state0_.st_id = state0_1_.st_id
 WHERE state0_.st_id = 10;

谢谢你的帮助!

4

2 回答 2

0

“REF_ST”与“REF_ST_L”是一对多的关系。这意味着“REF_ST_L”不能映射为辅助表(这意味着一对一的关系)。

相同的逻辑对“REF_CT”->“REF_CT_L”有效。

于 2013-09-06T14:07:35.787 回答
0

您的代码正在生成 LEFT JOIN,因为您将 City -> State 关系设置为可选。将其更改为不可为空,您应该得到一个内部联接,它应该解决返回的多行。

common.contact.City ->

@ManyToOne(targetEntity = State.class, optional = false)
@JoinColumn(name = "ST_ID", referencedColumnName = "ST_ID", nullable = false)
private State state;
于 2012-08-30T21:30:49.850 回答