Java JPA Realtions

时间:2016-04-22 18:17:05

标签: java hibernate jpa

我正在做一个需要2个实体的应用程序:User和Car。

    public class User{
        private long id;
        private String name;
    }

    public class Car{
        private long id;
        private User firstUser;
        private User secondUser;
    }

所以我的table cars_info信息将包含 RENT(日期),FIRST_USER(id),SECOND_USER(id)

如何使用休眠连接?我需要单向关系。我尝试了OneToOne,ManyToOne,但两者都没有。

1 个答案:

答案 0 :(得分:0)

检查您是否正确命名CAR表上的列与USER表上的引用列。示例如下:

@Entity
@Table(name = "USER")
public class User {

    @Id
    @Column(name = "USER_ID")
    private long id;

    @Column(name = "NAME")
    private String name;

}

@Entity
@Table(name = "CAR")
public class Car {

    @Id
    @Column(name = "CAR_ID")
    private long id;

    @ManyToOne
    @JoinColumn(name = "FIRST_USER_ID", referencedColumnName = "USER_ID")
    private User firstUser;

    @ManyToOne
    @JoinColumn(name = "SECOND_USER_ID", referencedColumnName = "USER_ID")
    private User secondUser;

}