嵌入式Hibernate中的外键

时间:2018-03-12 07:40:54

标签: hibernate jpa

我有以下实体:

@Entity
public class Role {

    @Id
    @GeneratedValue
    private long id;

    @Column(nullable = false)
    private String name;

    @Column(nullable = false)
    private String technicalName;

    @Column(nullable = false)
    private String description;
}

@Entity
public class Country {

    @Id
    @GeneratedValue
    private long id;

    @Column(nullable = false, unique = true)
    private String name;
}

@Embeddable
public class Permission {

    private Country country;    

    private Role role;
}

@Entity
public class User {
    @Id
    @GeneratedValue
    private long id;

    private String name;

    @ElementCollection
    @JoinTable(name = "User_Permissions")
    private Set<Permission> permissions;
}

User_Permissions表应如下所示:

+------------+-------------+-----+
| Field      | Type        | Key |
+------------+-------------+-----+
| user_id    | int(10)     | FK  |
| role_id    | int(10)     | FK  |
| country_id | int(10)     | FK  |
+------------+-------------+-----+

我正在使用hibernate.hbm2ddl.auto进行测试。当我部署我的应用程序时,我收到此错误:

Could not determine type for: 
Country, at table: User_Permissions, for columns: 
[org.hibernate.mapping.Column(country)]

我可以使用@OneToMany对其进行建模,但从我的观点来看,权限不是实体而是值类型,因为它为用户实体提供了意义,但对自身没有意义。

我做错了什么?

0 个答案:

没有答案