Hibernate使用JPA实现一对多配置
程序员文章站
2022-04-23 15:45:44
...
直接上代码,以下省略set和get方法
@Entity
@Table(name="t_customer")
public class Customer{
@Id
@Column(name="cust_id")
@GenericGenerator(name="sysnative",strategy="native")
@GeneratedValue(generator="sysnative")
private Long custId;
@Column(name="cust_name")
private String custName;
@Column(name="cust_sex")
private String custSex;
//mappedBy="customer" 外键映射,放弃外键维护
@OneToMany(targetEntity=Linkman.class,mappedBy="customer",cascade={CascadeType.ALL}) //默认放弃外键维护
private Set<Linkman> linkmans = new HashSet<Limkman>();
}
@Entity
@Table(name="t_limkman")
public class Linkman{
@Id
@Column(name="lkm_id")
@GenericGenerator(name="sysnative",strategy="native")
@GeneratedValue(generator="sysnative")
private Long lkmId;
@Column(name="lkm_name")
private String lkmName;
//另一方的字节码对象
@ManyToOne(targetEntity=Customer.class)
//referencedColumnName="cust_id" 另一个表的主键字段名称
@JoinColumn(name="lkm_cust_id",referencedColumnName="cust_id")
private Customer customer;
}
一对多中,一的一方默认延迟加载,多的一方默认立即加载