hibernate关联关系映射 博客分类: Hibernate hibernate注解关联关系映射
程序员文章站
2024-03-19 10:55:28
...
1.单向关联
a.多对一(many-to-one)
@Entity(name = "COLOR") public class Color implements Serializable { private static final long serialVersionUID = 6402753847018667163L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column private String color; @ManyToOne(fetch = FetchType.EAGER) @JoinColumns({ @JoinColumn(name = "DOG_ID",nullable = false,referencedColumnName="id"), @JoinColumn(name = "DOG_NAME",nullable = false,referencedColumnName="DOG_NAME") }) private Dog dogs; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public Dog getDogs() { return dogs; } public void setDogs(Dog dogs) { this.dogs = dogs; } @Override public String toString() { return "Color [id=" + id + ", color=" + color + "]"; } }
@Entity(name = "DOG") @IdClass(DogPrimarykey.class) public class Dog { private static final long serialVersionUID = 7219216851705947642L; @Id private String id; @Id private String name; @Column(name = "DOG_AGE", length = 2) private int age; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { if (age > 100 || age < 1) { this.age = 1; } else { this.age = age; } } public void say() { // TODO Auto-generated method stub System.out.println("汪 汪 汪"); } @Override public String toString() { return "Dog [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
因为一的那一段采用的联合主键,因此采用@JoinColumns的方式注解
@ManyToOne(fetch = FetchType.EAGER) 和 lazy 区别
如果是EAGER,那么表示取出这条数据时,它关联的数据也同时取出放入内存中。
如果是LAZY,那么取出这条数据时,它关联的数据并不取出来,在同一个session中,什么时候要用,就什么时候取(再次访问数据库)。
但是,在session外,就不能再取了。用EAGER时,因为在内存里,所以在session外也可以取。
CascadeType.PERSIST--级联保存,CascadeType.MERGE--级联更新,CascadeType.REFRESH--级联刷新,CascadeType.ALL--包含所有级联属性,所以可以多个。他们代表的操作是不一样的,如果你想包括所有级联关系就只需要用一个CascadeType.ALL就可以
b.一对一(One-to-one)
把上面的ManyToOne改为了OneToOne
2.双向关联(Bidirectional associations)
a.一对多(one to many)/多对一(many to one)
@Entity(name = "DOG") @IdClass(DogPrimarykey.class) public class Dog { private static final long serialVersionUID = 7219216851705947642L; @Id private String id; @Id private String name; @Column(name = "DOG_AGE", length = 2) private int age; @OneToMany(cascade=CascadeType.ALL,mappedBy="dogs") private Set<Color> colors; public Set<Color> getColors() { return colors; } public void setColors(Set<Color> colors) { this.colors = colors; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { if (age > 100 || age < 1) { this.age = 1; } else { this.age = age; } } public void say() { // TODO Auto-generated method stub System.out.println("汪 汪 汪"); } @Override public String toString() { return "Dog [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
@Entity(name = "COLOR") public class Color implements Serializable { private static final long serialVersionUID = 6402753847018667163L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column private String color; @OneToOne @JoinColumns({ @JoinColumn(name = "DOG_ID",nullable = false,referencedColumnName="id"), @JoinColumn(name = "DOG_NAME",nullable = false,referencedColumnName="DOG_NAME") }) private Dog dogs; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public Dog getDogs() { return dogs; } public void setDogs(Dog dogs) { this.dogs = dogs; } @Override public String toString() { return "Color [id=" + id + ", color=" + color + "]"; } }
推荐阅读
-
hibernate关联关系映射 博客分类: Hibernate hibernate注解关联关系映射
-
hibernate关联关系映射 博客分类: Hibernate hibernate注解关联关系映射
-
Hibernate中用left join(左外连接)查询映射中没有关联关系的两个表记录问题
-
多对一单向关联 博客分类: hibernate 多对一单向关联
-
多对多双向关联 博客分类: hibernate HibernateJavaBeanDAOXML
-
多对一单向关联 博客分类: hibernate 多对一单向关联
-
五、hibernate中持久层操作(多表之间的关联关系操作,1对1,一对多,多对多、继承)
-
hibernate annotation对jpa主键one-to-one映射关系支持的有问题 HibernateJPAJBoss
-
hibernate annotation对jpa主键one-to-one映射关系支持的有问题 HibernateJPAJBoss
-
Hibernate 一对一关系(基于XML) 博客分类: Hibernate hibernate