gpt4 book ai didi

java - JPA 加入两个辅助表

转载 作者:行者123 更新时间:2023-11-29 09:11:25 25 4
gpt4 key购买 nike

我有这五个表:

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

REF_LANGUAGE

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;

感谢您的帮助!

最佳答案

您的代码正在生成 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;

关于java - JPA 加入两个辅助表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12204906/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com