gpt4 book ai didi

java - Hibernate连接表错误: Unable to find column with logical name

转载 作者:太空宇宙 更新时间:2023-11-04 13:58:48 25 4
gpt4 key购买 nike

我试图通过 hibernate 中的 JoinTable 进行单向 ManyToOne 关联,但我不断收到以下错误:

A JPA error occurred (Unable to build EntityManagerFactory): Unable to find column with logical name: name in org.hibernate.mapping.Table(users) and its related supertables and secondary tables

我有 3 个模型 HouseUserUserHouseMap。我希望能够通过 UserHouseMap 访问用户之家。这是 User

上的映射

由于其他原因,我需要通过非主键的列将 User 映射到 UserHouseMap

@Id
@GeneratedValue
@Expose
public Long id;

@Expose
@Required
@ManyToOne
@JoinTable(name = "user_house_map",
joinColumns= {@JoinColumn(table="users", name="user_name", referencedColumnName="name")},
inverseJoinColumns={@JoinColumn(table="houses", name="house_name", referencedColumnName="house_name")})
public House house;

以下是所有 3 个模型的数据库架构

用户

                               Table "public.users"
Column | Type | Modifiers
-----------------------+-----------------------------+-----------------------------
name | character varying(255) |
id | integer | not null
Indexes:
"user_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
"housing_fkey" FOREIGN KEY (name) REFERENCES user_house_map(user_name) DEFERRABLE INITIALLY DEFERRED

房屋

                Table "public.houses"
Column | Type | Modifiers
---------------+------------------------+-----------
house_name | character varying(255) | not null
address | text |
city | text |
state | text |
zip | integer |
zip_ext | integer |
phone | text |
Indexes:
"house_pkey" PRIMARY KEY, btree (house_name)
Referenced by:
TABLE "user_house_map" CONSTRAINT "house_map_fkey" FOREIGN KEY (house_name) REFERENCES house(house_name) DEFERRABLE INITIALLY DEFERRED

UserHouseMap

         Table "public.user_house_map"
Column | Type | Modifiers
-------------+------------------------+-----------
user_name | character varying(255) | not null
house_name | character varying(255) | not null
Indexes:
"user_house_map_pkey" PRIMARY KEY, btree (user_name)
"user_house_map_house_key" btree (house_name)
Foreign-key constraints:
"user_house_map_house_fkey" FOREIGN KEY (house_name) REFERENCES houses(house_name) DEFERRABLE INITIALLY DEFERRED
Referenced by:
TABLE "users" CONSTRAINT "housing_fkey" FOREIGN KEY (name) REFERENCES user_house_map(user_name) DEFERRABLE INITIALLY DEFERRED

最佳答案

JoinColumn 注释中删除 table="users"table="houses"

该属性不应包含外键的目标表。仅当实体映射到两个表时才使用它,以便判断连接列位于哪个表中。

此外,你的表定义很奇怪:

  • 连接表应该有两个外键:一个引用 users 表的 PK,另一个引用 house 表的 PK。为什么引用用户的名称而不是其 ID?
  • users.name 上的外键约束没有意义。

关于java - Hibernate连接表错误: Unable to find column with logical name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29528596/

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