一对多
package cn.pojo; import javax.persistence.*; import java.io.Serializable; import java.util.Set; @Entity //声明这是一个实体类 必写 public class District implements Serializable { private static final long serialVersionUID = 8009273978191878070L; //编号 @Id //标识属性 相当于数据库中的主键 @GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "seq_dis") //strategy设置标识的生成策略 @SequenceGenerator(name = "seq_dis",sequenceName = "seq_dis_id", //声明序列 自动增长唯1 从1开始自增 allocationSize = 1,initialValue = 1) private Integer id; //区县名称 @Column(name = "name") //将属性映射到数据库中 填写列名不区分大小写 private String name; //街道 @OneToMany(cascade = {CascadeType.ALL}) //一对多,不需要指定类型,cascade是为了指定级联操作。 @JoinColumn(name = "district_Id") //另一个表的外键 填写列名 有了@joincolumn注解 就不能有mappedBy属性否则会出兼容问题这个跟hibernate版本有关.我的是5 private Set<Street> streetSet; public Set<Street> getStreetSet() { return streetSet; } public void setStreetSet(Set<Street> streetSet) { this.streetSet = streetSet; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public District(Integer id, String name) { this.id = id; this.name = name; } public District(){} }
多对一
package cn.pojo; import javax.persistence.*; import java.io.Serializable; import java.util.Set; @Entity //声明这是一个实体类 必写 public class Street implements Serializable { private static final long serialVersionUID = 2465299058773906791L; //编号 @Id //标识属性 @GeneratedValue(strategy = GenerationType.IDENTITY) //设置生成策略 private Integer id; //街道名称 @Column(name = "name") //将属性映射到数据库中 填写列名 不区分大小写 private String name; //所属区县 @ManyToOne //多对一 @JoinColumn(name = "district_Id") //指向另一个表的外键 填写列名 private District district; @OneToMany @JoinColumn(name = "street_id") private Set<House> houses; public District getDistrict() { return district; } public void setDistrict(District district) { this.district = district; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Street(){} }
多对多
package entity; import javax.persistence.*; import java.util.Objects; import java.util.Set; @Entity public class EmployeeEntity { private long empid; //员工名 private String empname; //开发过的项目 private Set<ProjectEntity> projects; @ManyToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY) @JoinTable(name = "proemp" //中间表的名字 ,joinColumns ={@JoinColumn(name = "rempid")} //与当前id有关的外键 ,inverseJoinColumns = {@JoinColumn(name = "rproid")}) //另一个表有关的外键,也就是跟Project public Set<ProjectEntity> getProjects() { return projects; } public void setProjects(Set<ProjectEntity> projects) { this.projects = projects; } @Id public long getEmpid() { return empid; } public void setEmpid(long empid) { this.empid = empid; } @Column(name = "EMPNAME") public String getEmpname() { return empname; } public void setEmpname(String empname) { this.empname = empname; } }
package entity; import javax.persistence.*; import java.util.Objects; import java.util.Set; @Entity public class ProjectEntity { private long proid; //项目名称 private String proname; //工作人员 private Set<EmployeeEntity> employees; @ManyToMany @JoinTable(name = "proemp" //如上 ,joinColumns ={@JoinColumn(name = "rproid")} //如上 ,inverseJoinColumns = {@JoinColumn(name = "rempid")}) //如上 public Set<EmployeeEntity> getEmployees() { return employees; } public void setEmployees(Set<EmployeeEntity> employees) { this.employees = employees; } @Id @Column(name = "PROID") public long getProid() { return proid; } public void setProid(long proid) { this.proid = proid; } @Column(name = "PRONAME") public String getProname() { return proname; } public void setProname(String proname) { this.proname = proname; } }
多对多测试的时候出现了一个问题如下
An AnnotationConfiguration instance is required to use <mapping class="entity.ProjectEntity"/>
意思是,用了注解了,自然要用注解类生成SessionFactory,刚出现的时候很懵逼,因为说hibernate4以上的版本是不会出现这种问题的,于是找了半天.
最后发现就是这个问题如下
configuration=new AnnotationConfiguration(); //configuration = new Configuration().configure(); 注释这一段加上 上面一段 下面再多点下configure(); //获取session对象 sessionFactory =configuration.configure().buildSessionFactory();
因为自动生成的注解是在get上面的所以就没改了.
使用idea自动生成实体类注解和实体类
没有hibernate配置点击加号自动生成一个
第二步
第三步
确保这个连接成功之后第四步
之后会进入如下界面
最后确定即可