在hibernate中,通常配置对象关系映射关系有两种,一种是基于xml的方式,另一种是基于annotation的注解方式。
在hibernate4及以后的版本直接使用注解,如果使用hibernate3的版本就需要引入annotation的jar包。
在前面的博客中创建student.hbm.xml文件和score.hbm.xml文件就是基于xml的方式。下面介绍注解的使用。
与以上两篇博客同。
与以上两篇博客同。
下面仍然以student和score表为例说明注解的使用
注解方式最重要的就是写在Model类中的注解。
student的Model类如下:
@Entity //表示是hibernate中的实例 @Table(name = "student")//对应student表 @Proxy(lazy = false)//不使用懒加载机制 public class Student { private int id; private String name; private int age; private Set<Score> score = new HashSet<>(); @Id//对应表中的ID @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } @Column(name = "name" ,length=50)//对应表中的一列,name表示表的字段名,若不写则认为类的字段名就是表的字段名 public String getName() { return name; } public void setName(String name) { this.name = name; } @Column(length=11) public int getAge() { return age; } public void setAge(int age) { this.age = age; } //一对多的关系,mappedBy的值对应Score类中ManyToOne注解所在的字段 @OneToMany(cascade={javax.persistence.CascadeType.ALL}, fetch=FetchType.EAGER,mappedBy = "student") public Set<Score> getScore() { return score; } public void setScore(Set<Score> score) { this.score = score; } }score的Model类如下:
@Entity @Table(name = "score") @Proxy(lazy = false) public class Score { private int id; private int score; private String type; private Student student; @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } @Column(length=11) public int getScore() { return score; } public void setScore(int score) { this.score = score; } @Column(length=20) public String getType() { return type; } public void setType(String type) { this.type = type; } //多对一 @ManyToOne(cascade={CascadeType.ALL}) @JoinColumn(name="student_id")//外键 public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } }全局配置文件放在resource目录下,命名为:hibernate.cfg.xml。文件内容如下:
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <!-- 一个sessionFactory代表数据库的一个连接--> <session-factory> <!-- 链接数据库的用户名 --> <property name="connection.username">root</property> <!-- 链接数据库的密码 --> <property name="connection.password">root</property> <!-- 链接数据库的驱动 --> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <!-- 链接数据库的url --> <property name="connection.url"> jdbc:mysql://localhost:3306/xia </property> <!--方言,告诉hibernate使用什么样的数据库,hibernate就会在底层拼接什么样的sql语句--> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <!-- 配置根据持久化类生成表的策略 validate 通过映射文件检查持久化类与表的匹配 update 每次hibernate启动的时候,检查表是否存在,如果不存在,则创建,如果存在,则什么都不做了 create 每一次hibernate启动的时候,根据持久化类和映射文件生成表 create-drop --> <property name="hbm2ddl.auto">update</property> <property name="show_sql">true</property> <!--加载model类与表结构映射关系文件--> <!--<mapping resource="student.hbm.xml" />--> <!--<mapping resource="score.hbm.xml" />--> <!--注:此时没有映射文件,需配置class文件--> <mapping class="com.honor.sql.Student"/> <mapping class="com.honor.sql.Score"/> </session-factory> </hibernate-configuration>以查询为例,代码如下:
public static Student selectStudent(int id) { //获取Session对象 Session session = getSession(); try { session.beginTransaction(); //根据id查询Student对象 Student student = session.load(Student.class, id); session.getTransaction().commit(); return student; } catch (Exception e) { e.printStackTrace(); //出现异常时回滚事务 session.getTransaction().rollback(); } finally { if (session != null) { if (session.isOpen()) { session.close();//关闭session } } } return null; }结果如下:
