Hibernate JoinTable错误:无法找到具有逻辑名称的列

时间:2015-04-09 02:13:06

标签: java hibernate hibernate-mapping psql

我试图通过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

1 个答案:

答案 0 :(得分:0)

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

此属性不应包含外键的目标表。它仅在实体映射到两个表时使用,以便告知连接列所在的表。

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

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