开发者

Hibernate different name of foreign key column

开发者 https://www.devze.com 2023-04-05 22:25 出处:网络
I got in DB tables: a) rb_user: id_user, username, ... b) rb_user_password: id, user_id, password. How to annotate this in my java classes? I got:

I got in DB tables: a) rb_user: id_user, username, ... b) rb_user_password: id, user_id, password.

How to annotate this in my java classes? I got:

@Entity
@Table(name="rb_users")
@SecondaryTable(name="rb开发者_运维问答_user_password")
public class User {

    @Id
    @Column(name="id_user")
    private int id;
    private String name;
    ...
    @Column(table="rb_user_password", name="password")
    private String password

I got these exception:

org.postgresql.util.PSQLException: ERROR: column user0_1_.id_user does not exist

I know its because I got primary key "id_user", and in my table with password column name is "user_id"


You can specifiy the name of the attribute in the secondary table with the pkJoinColumns attribute on @SecondaryTable.

@Entity
@Table(name="rb_users")
@SecondaryTable(name="rb_user_password", pkJoinColumns=@PrimaryKeyJoinColumn(name="user_id"))
public class User {
...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号