Hibernate单向多对一配置:
多对一它的思想是在多的一方设置一的对象作为其属性。
比如学生和所在的班级为例。学生是多的一方,班级是一的一方。只要在学生类里面设置班级类的对像就完成了学生与班级之间的关联关系。
以学生和所在班级为例,这两个类是这样的:
班级类(是一的一方):
package com.ye.models;
/**
* 类功能简单描述:这个是单向的多对一映射<BR/>
* 类名:Class<BR/>
* 作者yenairong<BR/>
* 时间:2014年11月17日 下午1:00:17<BR/>
* @version 1.0.0 <BR/>
*/
public class Class {
private int classId;
private String className;
//set get方法
public int getClassId() {
return classId;
}
public void setClassId(int classId) {
this.classId = classId;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
}
学生类是这样的(多的一方):
package com.ye.models;
public class Student {
private int stuId;
private String studName;
private Class stuClass; //这里设置的是多对一。学生是多,班级是多
public int getStuId() {
return stuId;
}
public void setStuId(int stuId) {
this.stuId = stuId;
}
public String getStudName() {
return studName;
}
public void setStudName(String studName) {
this.studName = studName;
}
public Class getStuClass() {
return stuClass;
}
public void setStuClass(Class stuClass) {
this.stuClass = stuClass;
}
}
接下来配置相应的hbm.xml文件。班级的映射文件是这样的:
这个跟平常的映射没有什么不同。
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <!--如果让hibernate自动创建表结构,则column是什么。数据库里 面生成的就是对应的column的属性值,table对应于数据库的表名--> <class name="com.ye.models.Class" table="c_table"> <id name="classId" type="int"> <column name="class_id" /> <generator class="native"><!--因为使用的是MySql5.7版,它的主键生成策略是native--> </generator> </id> <property name="className" type="java.lang.String"> <column name="class_name" /> </property> </class> </hibernate-mapping>
学生的映射文件是这样的:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <!-- name对应于实体类里面的属性。column是数据库里面的的列名(不写情况是跟属性名一致) --> <class name="com.ye.models.Student" table="c_student"> <id name="stuId" type="int"> <column name="stu_id" /> <generator class="native"> </generator> </id> <property name="studName" type="java.lang.String"> <column name="stu_name" /> </property> <!-- 在这里配置多对一 ,class指定引用的类。column是对应于数据库里面的列名,如果让hibernate自动创建表结构,则column是什么。数据库里 面生成的就是对应的column的属性值--> <many-to-one name="stuClass" column="stu_class" class="com.ye.models.Class" ></many-to-one> </class> </hibernate-mapping>
之后配置配置文件:它的结构是:
<?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.password">123456</property> <property name="hibernate.connection.url">jdbc:mysql:///test</property> <property name="hibernate.connection.username">root</property> <!-- 数据库方言 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 是否显示sql语句 --> <property name="show_sql">true</property> <!-- 是否自动生成表结构,它有四个属性,一般常用的就是update --> <property name="hbm2ddl.auto">update</property> <!-- 映射文件 --> <mapping resource="com/ye/models/Class.hbm.xml"/> <mapping resource="com/ye/models/Student.hbm.xml"/> </session-factory> </hibernate-configuration>
这样子的