我正在尝试通过休眠中的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
最佳答案
从table="users"
批注中删除table="houses"
和JoinColumn
。
此属性不应包含外键的目标表。仅当实体映射到两个表时才能使用,以告诉联接列位于哪个表中。
另外,您的表定义很奇怪:
连接表应具有两个外键:一个引用用户表的PK,另一个引用内部表的PK。为什么引用用户名而不是ID?
对users.name的外键约束没有任何意义。
关于java - Hibernate JoinTable错误:无法找到具有逻辑名称的列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29528596/