我试图通过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个模型House
,User
,UserHouseMap
。我希望能够通过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
答案 0 :(得分:0)
从table="users"
注释中删除table="houses"
和JoinColumn
。
此属性不应包含外键的目标表。它仅在实体映射到两个表时使用,以便告知连接列所在的表。
另外,你的表定义很奇怪: