entity:
package com.bxw.entity; public class Student { private int id; private String name; private String sex; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
不采用注解的方式。
package com.bxw.entity; import javax.persistence.Entity; import javax.persistence.Id; @Entity public class Teacher { private int id; private String title; private String sex; @Id public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
采用注解的方式。
配置文件:Student.hbm.xml
<?xml version="1.0" encoding='UTF-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.bxw.entity"> <class name="Student"> <id name="id"></id> <property name="name"></property> <property name="sex"></property> </class> </hibernate-mapping>
Test:
package com.bxw.test; import org.hibernate.Session; import org.hibernate.cfg.Configuration; import com.bxw.entity.Student; public class StudentTest { public static void main(String[] args) { Student ss = new Student(); ss.setId(1); ss.setName("Han meimei"); ss.setSex("女"); Configuration conf = new Configuration(); conf.configure();//不指定路径默认访问classpath下的hibernate.cfg.xml Session session = conf.buildSessionFactory().openSession(); session.beginTransaction(); session.save(ss); session.getTransaction().commit(); session.close(); } }
需要初始化一个读取配置文件的Configuration。
conf.configure()初始化该配置文件,如果不指定路径,则默认该配置文件再classPath下。拿到sessionFactory,拿到session,打开事务,持久化对象,提交事务,关闭事务。
p.s.读取的配置文件是:
<?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> <session-factory> <!-- 连接数据库 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver </property> <property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate </property> <property name="hibernate.connection.username">root </property> <property name="hibernate.connection.password">root </property> <!-- DB Dialog --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect </property> <!-- showSql --> <property name="hibernate.show_sql">true </property> <!-- mapping --> <mapping resource="com/bxw/entity/Student.hbm.xml"></mapping> <!-- annotation mapping--> <mapping class="com.bxw.entity.Teacher"></mapping> </session-factory> </hibernate-configuration>
==========================分割线==========================
采用注解的方式:
entity:
package com.bxw.entity; import javax.persistence.Entity; import javax.persistence.Id; @Entity public class Teacher { private int id; private String title; private String sex; @Id public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
需要再hibernate.cfg.xml中配置相关项。
补:
如果类名与表名不一样,注解方式在@Entity与类名之间添加@Table(name="xxx"),xml方式在<class name="xxx">中添加属性name="xxx"。
注解方式中属性的注解默认为@Basic
如果属性与字段名不同,注解方式添加注解@Column(name="xxx"),xml方式在<property name="xxx">中添加属性column="xxx"
如果属性中有属性不想存在数据库中,使用@Transient
如果属性中存在时间类型,默认保存时间+日期。采用@Temporal(TemporalType.Date)只保存日期,@Temporal(TemporalType.Time)只保存时间
如果属性中存在枚举类型,使用@Enumerated(EnumType.String)注解,xml会很麻烦。@Enumerated(EnumType.ordinal)会存枚举的下标