这篇博客是闲着没事,自己随便写着玩的。。只简单列举一下实体类在不同关联关系中的写法。
首先是单向多对一,两个实体类,分别是Order和Customer:
package com.sunsy.jpatest.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Table(name="JPA_ORDERS")
@Entity
public class Order {
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Id
private Integer id;
@Column(name="ORDER_NAME")
private String orderName;
@JoinColumn(name="CUSTOMER_ID")
@ManyToOne(fetch=FetchType.LAZY)
private Customer customer;
//get set方法省略
}
package com.sunsy.jpatest.entity;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Table(name="JPA_CUSTOMER")
@Entity
public class Customer {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="CUSTOMER_ID")
private Integer id;
private String lastName;
private String email;
private int age;
private Date createTime;
private Date birth;
}
单向一对多:
package com.sunsy.jpatest.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Table(name="JPA_ORDERS")
@Entity
public class Order {
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Id
private Integer id;
@Column(name="ORDER_NAME")
private String orderName;
}
package com.sunsy.jpatest.entity;
import java.util.Date;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Table(name="JPA_CUSTOMER")
@Entity
public class Customer {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="CUSTOMER_ID")
private Integer id;
private String lastName;
private String email;
private int age;
private Date createTime;
private Date birth;
//加了这个Casade级联策略,那么你只要把customer.setOrders(Set<> orders)之后,直接添加customer就会自动把orders也添加到表中,删除、更新时也一样
@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="CUSTOMER_ID")
private Set<Order> orders;
}
多向一对一,注意,保存的时候应先保存不维护关联关系的一方,也就是没有外键的一方,然后调用另一个类的set方法把没有外键的一方添加到属性中,然后再保存:
package com.sunsy.jpatest.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Table(name="JPA_MANAGERS")
@Entity
public class Manager {
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Id
private Integer id;
@Column(name="MGR_NAME")
private String mgrName;
//对于不维护关联关系,也就是没有外键的一方,要设置mappedBy,里边的值是Dept类里的自身类的属性名。
//否则两边都维护关联关系,添加时就会多出来一些update语句
@OneToOne(mappedBy="mgr")
private Department dept;
}
package com.sunsy.jpatest.entity;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name="JPA_DEPTS")
public class Department {
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Id
private Integer id;
@Column(name="DEPT_NAME")
private String deptName;
@OneToOne
@JoinColumn(name="MGRa_ID", unique=true)
private Manager mgr;
}
最后是双向多对多的关系,注意,应该先存不维护关联关系的role表,比如存了role1和role2,然后user1.setRoles(new HashSet<>().add(role1).add(role2)),再存user1,这样才能在user_roles表中插入关联数据,反过来没戏(如果害怕弄错的话,就直接在user里加上roles集合,再在role里加入users集合,这样就不会错了,但是我感觉这样弄不好,因为这样做必须把所有role依赖的users存进去,然后再把users里各个user依赖的所有roles存进去,说的太绕了,自己去试试就懂了):
package com.sunsy.jpatest.entity;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name="JPA_USERS")
public class User {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
private String userName;
@JoinTable(name="user_roles", joinColumns={@JoinColumn(name="USER_ID", referencedColumnName="id")},
inverseJoinColumns= {@JoinColumn(name="ROLE_ID", referencedColumnName="id")})
@ManyToMany
private Set<Role> roles;
}
package com.sunsy.jpatest.entity;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name="JPA_ROLES")
public class Role {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
private String roleName;
@ManyToMany(mappedBy="roles")
private Set<User> users = new HashSet<>();
}