如何在pojo类级别的hibernate中映射2个表?

时间:2013-10-02 05:35:50

标签: java sql hibernate oop

嗨我有2个pojo课程用于DutyManager& DMSchedule

这是我的数据库表详情

表名,
Duty_Manager:Manager_ID,标题,名称,年龄,地址PK = Manager_ID

DM_Schedule:Schedule_ID,Manager_ID,From_Date,To_Date PK = Schedule_ID FK = Manager_ID

我应该如何使用hibernate注释在POJO类级别进行映射。

1 个答案:

答案 0 :(得分:2)

@Entity
@Table(name = "Duty_Manager")
public class DutyManager {
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "my_entity_seq_gen")
@SequenceGenerator(name = "my_entity_seq_gen", sequenceName = "MNG_SEQ")
@Column(name = "MANAGER_ID")
private Long id;
@Column(name = "manager_title", nullable = false)
private String title;
@Column(name = "manager_name", nullable = false)
private String name;
@Column(name = "manager_age", nullable = false)
private int age;

public DutyManager() {
}


//getters and setters
}



@Entity
@Table(name = "DM_Schedule")
public class DM_Schedule {
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "my_entity_seq_gen")
@SequenceGenerator(name = "my_entity_seq_gen", sequenceName = "SCH_SEQ")
@Column(name = "Schedule_ID")
private Long id;
@ManyToOne
@JoinColumn(name = "manager_id", nullable = false)
private DutyManager manager;
@Column(name = "date_from", nullable = false)
private Date from;
@Column(name = "date_to", nullable = false)
private Date to;

public DM_Schedule() {
}


//getters and setters
}

编辑: 如评论中所述,您还可以Collection<DM_Schedule>Manager类注释为@OneToMany注释。