我有3个模型UserHouseUserHouseMap。而且我需要通过 map 访问用户的房屋。唯一的问题是这是一个旧的数据库,我无法更改需要使用非主键UserUserHouseMap映射到user.name的事实。

Hibernate一直给我错误,说我需要将它作为主键,否则我收到错误,说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
我已经尝试过@Formula作为一种解决方法,但是那没有用。我也尝试过@JoinColumnOrFormula,但是也没有用。这是我用@Formula的解决方案

@Expose
@ManyToOne(targetEntity = House.class)
@Formula("(select * from houses inner join user_house_map on houses.house_name = user_house_map.house_name where user_house_map.user_name=name)")
public House house;

这是我尝试使用@JoinColumnOrFormula解决方案的尝试。
@Expose
@ManyToOne(targetEntity = House.class)
@JoinColumnsOrFormulas({
        @JoinColumnOrFormula(formula=@JoinFormula(value="select name from users where users.id= id", referencedColumnName="name")),
        @JoinColumnOrFormula(column = @JoinColumn(name= "house_name", referencedColumnName="house_name"))
})
public House house;

这是我的 map
@Id
@GeneratedValue
@Expose
public Long id;

@Expose
@Required
@ManyToOne
@JoinTable(
        name="user_house_map",
        joinColumns=
        @JoinColumn(unique=true,name="user_name", referencedColumnName="name"),
        inverseJoinColumns=
        @JoinColumn(name="house_name", referencedColumnName="house_name"))
private House house;

这是数据库模式

用户

                               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

最佳答案

这是您的映射应如下所示:

@Entity
public class User {

    @Id
    private Long id;

    private String name;

    @OneToMany(mappedBy = "user")
    private List<UserHouseMap> houses = new ArrayList<>();
}

@Entity
public class House {

    @Id
    @Column(name = "house_name", nullable = false, unique = true)
    private String house_name;

    private String address;

    @OneToMany(mappedBy = "house")
    private List<UserHouseMap> users = new ArrayList<>();
}

@Entity
public class UserHouseMap implements Serializable {

    @Id @ManyToOne
    @JoinColumn(name = "user_name", referencedColumnName = "name")
    private User user;

    @Id @ManyToOne
    @JoinColumn(name = "house_name", referencedColumnName = "house_name")
    private House house;
}
UserHouse都可以访问与数据库模式匹配的关联UserHouseMap实体。

08-17 23:21