hibernate annotation (注解映射) 一对多
2012-07-26 13:08
260 查看
此处 一对多关联映射 采用外键关联的映射策略
以company 和employee 为例
1.company 实体
2.Employee实体
对应的表结构 为:
以company 和employee 为例
1.company 实体
/** * company 与 employee 一对多的关系 * */ @Entity @Table(name="company", schema="test") public class Company implements Serializable{ private static final long serialVersionUID = 5949825011764837885L; private int id; private String name; private List<Employee> employeeList = new ArrayList<Employee>(); @Id @GeneratedValue(strategy=GenerationType.AUTO) public int getId() { return id; } public void setId(int id) { this.id = id; } @Column(name="name") public String getName() { return name; } public void setName(String name) { this.name = name; } /** * 一对多 关联映射 * targetEntity 表示关联的实体类为 Employee * JoinColumn 表示 关联字段 即 在表employee中关联company的外键为company_id * */ @OneToMany(targetEntity=Employee.class) @JoinColumn(name="company_id") public List<Employee> getEmployeeList() { return employeeList; } public void setEmployeeList(List<Employee> employeeList) { this.employeeList = employeeList; } }
2.Employee实体
@Entity @Table(name="employee", schema="test") public class Employee implements Serializable{ private static final long serialVersionUID = -2121143635315546780L; private int id; private String name; @Id public int getId() { return id; } public void setId(int id) { this.id = id; } @Column(name="name") public String getName() { return name; } public void setName(String name) { this.name = name; } }
对应的表结构 为:
CREATE TABLE `employee` ( `id` INT(11) NOT NULL, `name` VARCHAR(255) DEFAULT NULL, `company_id` INT(11) DEFAULT NULL, PRIMARY KEY (`id`), KEY `FK4722E6AE5BDF175E` (`company_id`), CONSTRAINT `FK4722E6AE5BDF175E` FOREIGN KEY (`company_id`) REFERENCES `company` (`id`) ) ENGINE=INNODB DEFAULT CHARSET=utf8
CREATE TABLE `company` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `name` VARCHAR(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=INNODB DEFAULT CHARSET=utf8
相关文章推荐
- Hibernate之关系映射的两种方式(xml映射、annotation注解映射)
- Hibernate -- 注解(Annotation)关系映射
- Hibernate -- 注解(Annotation)关系映射
- Hibernate -- 注解(Annotation)关系映射
- Hibernate -- 注解(Annotation)关系映射
- Hibernate -- 注解(Annotation)关系映射
- hibernate annotation注解方式来处理映射关系
- Hibernate -- 注解(Annotation)关系映射
- Hibernate -- 注解(Annotation)关系映射
- hibernate annotation注解方式来处理映射关系
- Hibernate -- 注解(Annotation)关系映射
- Hibernate之Annotation(注解的方式,非映射)
- Hibernate -- 注解(Annotation)关系映射
- hibernate Annotation(注解映射)测试一
- hibernate annotation注解方式来处理映射关系
- Hibernate -- 注解(Annotation)关系映射
- Hibernate -- 注解(Annotation)关系映射
- Hibernate -- 注解(Annotation)关系映射
- hibernate annotation注解方式来处理映射关系
- Hibernate中,One2Many和Many2One的双向映射关系的基于注解Annotation实现方式