1.对Hibernate注解的理解
HIbernate注解代替的是实体类的映射文件 xxx.hbm.xml;
Hibernate注解在实体类中实现;
注解使用的是javax.persistence.* 包。
注意:Hibernate.cfg.xml中的映射文件需改为映射类:
<mapping class="com.entity.Stud"/>
2.HIbernate注解基础
@Entity 注解实体类
@Table 注解在数据库中的表名 (@Table(name="Student"))
public class Stud {
@Id 表主键
//@GeneratedValue(strategy=GenerationType.IDENTITY) //生成策略
@GeneratedValue(generator="Na")
@GenericGenerator( name="Na",strategy="native") //使用Hibernate生成策略
@Column(name="tid") //设置表中的字段名
private Integer id;
//默认为@Basic @Column(name="属性")
//@Basic //表示当前属性将被持久化到DB
//@Column(name="tname")
private String name;
//@Transient //表示当前属性不写到DB中
private double score;
}
3.Hibernate注解 一对多
在这里只写除核心代码,那国家(Country)和城市(Minister)实体类举例
关联属性:只有一方(一对多)可以放弃关联关系的维护权;
mappedBy的设置表示当前的一方放弃了关联关系的维护权,将维护权交给多方的关联属性
@OneToMany(cascade=CascadeType.ALL,mappedBy="country")
//放弃维护权后,下面注解不需要了
@JoinColumn(name="cid") 该注解设置外键
@Entity
@Table
public class Country {
@Id
@GeneratedValue(generator="Na")
@GenericGenerator(name="Na",strategy="native")
private Integer cid;
private String cname;
@OneToMany(cascade=CascadeType.ALL,mappedBy="country")
//放弃维护权后,下面注解不需要了
//@JoinColumn(name="cid")
private Set<Minister> ministers;
}
@Entity
@Table
public class Minister {
@Id
@GeneratedValue(generator="Na")
@GenericGenerator(name="Na",strategy="native")
private Integer mid;
private String mname;
//@ManyToOne(targetEntity=Country.class,cascade=CascadeType.ALL)
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="cid")
private Country country;
}
在Hibernate.cfg.xml注册映射类
<mapping class="com.entity.Country"/>
<mapping class="com.entity.Minister"/>
一对多测试类:
//@Test
public void testSave() {
Session session = HbnUtils.getSession();
try {
session.beginTransaction();
Minister minister1 = new Minister("北京");
Minister minister2 = new Minister("上海");
Country country = new Country("中国");
//一方在维护
country.getMinisters().add(minister1);
country.getMinisters().add(minister2);
session.save(country);
//多方在维护
//minister1.setCountry(country);
//minister2.setCountry(country);
//session.save(minister1);
//session.save(minister2);
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}
}
4.Hibernate注解 多对多
使用学生(Student)与课程(Course)举例说明。让另一方放弃关联关系的维护权,其他与单表注解一样
@ManyToMany(cascade=CascadeType.ALL,mappedBy="courses")
@Entity
@Table
public class Course {
@Id
@GeneratedValue(generator="Na")
@GenericGenerator(name="Na",strategy="native")
private Integer cid;
private String cname;
@ManyToMany(cascade=CascadeType.ALL,mappedBy="courses")
private Set<Student> students;
}
@Entity
@Table
public class Student {
@Id
@GeneratedValue(generator="Na")
@GenericGenerator(name="Na",strategy="native")
private Integer sid;
private String name;
@ManyToMany(cascade=CascadeType.ALL)
private Set<Course> courses;
}
在Hibernate,cfg..xml注册映射类:
<mapping class="com.entity.Course"/>
<mapping class="com.entity.Student"/>
多对多测试类:
@Test
public void testSave() {
Session session = HbnUtils.getSession();
try {
session.beginTransaction();
Course course1 = new Course("javaEE");
Course course2 = new Course("jsp");
Course course3 = new Course("c++");
Student student1 = new Student("张三");
student1.getCourses().add(course1);
student1.getCourses().add(course2);
Student student2 = new Student("李四");
student2.getCourses().add(course2);
student2.getCourses().add(course3);
session.save(student1);
session.save(student2);
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}
}