1、Java实体bean类
package com.project.pojo; import java.io.Serializable; public class Student implements Serializable{ private int id ; private String name; 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; } }
package com.project.pojo; import java.io.Serializable; public class Subject implements Serializable{ private int id ; private String name ; 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; } }
package com.project.pojo; import java.io.Serializable; /** * 如果组合索引是类的属性时,该类必须实现Serializable * @author Administrator * */ public class Result implements Serializable{ private Student student; private Subject subject; private double score; public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } public Subject getSubject() { return subject; } public void setSubject(Subject subject) { this.subject = subject; } public double getScore() { return score; } public void setScore(double score) { this.score = score; } }
2、hbm.xml配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.project.pojo"> <class name="Student" table="t_student"> <id name="id"> <generator class="native"></generator> </id> <property name="name" /> </class> </hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.project.pojo"> <class name="Subject" table="t_subject"> <id name="id"> <generator class="native"></generator> </id> <property name="name" /> </class> </hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.project.pojo"> <class name="Result" table="t_result"> <!-- 组合索引映射 --> <composite-id> <key-many-to-one name="student" column="stuid" class="Student" /> <key-many-to-one name="subject" column="subid" class="Subject"/> </composite-id> <property name="score" /> </class> </hibernate-mapping>
3、hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 1、数据库连接信息 --> <!-- 指定数据方言 --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://192.168.1.59:3306/hibernate?characterEncoding=UTF8</property> <property name="connection.username">root</property> <property name="connection.password">1234</property> <!-- 2、通用配置信息 --> <!-- 打印sql语句 --> <property name="show_sql">true</property> <!-- 格式化sql语句 --> <property name="format_sql">true</property> <!-- 映射文件信息 --> <mapping resource="com/project/pojo/Result.hbm.xml" /> <mapping resource="com/project/pojo/Student.hbm.xml" /> <mapping resource="com/project/pojo/Subject.hbm.xml" /> </session-factory > </hibernate-configuration>
4、测试
package com.project.test; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.tool.hbm2ddl.SchemaExport; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.project.pojo.Result; import com.project.pojo.Student; import com.project.pojo.Subject; import com.project.util.HibernateUtil; public class HibernateTest { Session session = null; Transaction ts = null; @Before public void setUp(){ session = HibernateUtil.getSession(); ts = session.beginTransaction(); } @After public void tearDown(){ HibernateUtil.closeSession(); } @Test public void testCreateDB(){ Configuration cfg = new Configuration().configure(); //使得hibernate映射信息转换为数据库识别的dll语言 SchemaExport se = new SchemaExport(cfg); //第一个参数:是否打印dll语句 //第二个参数:是否将dll到数据库中执行 se.create(true, true); } @Test public void testInit(){ try { Student stu = (Student) session.get(Student.class, 1); Subject sub = (Subject) session.get(Subject.class, 1); Result r = new Result(); r.setStudent(stu); r.setSubject(sub); r.setScore(70.5); session.save(r); ts.commit(); } catch (Exception e) { if(ts!=null)ts.rollback(); e.printStackTrace(); } } @Test public void testSelect(){ Student stu = (Student) session.get(Student.class, 1); Subject sub = (Subject) session.get(Subject.class, 1); Result r = new Result(); r.setStudent(stu); r.setSubject(sub); r = (Result) session.get(Result.class, r); System.out.println(r.getScore()); } }