双向多对一:在一方也配置,在多方也配置
Grade.java:
package com.lxj.entity;

import java.util.HashSet;
import java.util.Set;

public class Grade {
private int gid;
private String gname;
private String gdesc;

public Grade(){}

public Grade(String gname, String gdesc) {
this.gname = gname;
this.gdesc = gdesc;
}

private Set<Student> students = new HashSet<Student>();

public void setStudents(Set<Student> students){
this.students = students;
}

public Set<Student> getStudents(){
return students;
}

public int getGid() {
return gid;
}

public void setGid(int gid) {
this.gid = gid;
}

public String getGname() {
return gname;
}

public void setGname(String gname) {
this.gname = gname;
}

public void setGdesc(String gdesc) {
this.gdesc = gdesc;
}
public String getGdesc() {
return gdesc;
}



@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Grade grade = (Grade) o;

if (gid != grade.gid) return false;
if (gname != null ? !gname.equals(grade.gname) : grade.gname != null) return false;
if (gdesc != null ? !gdesc.equals(grade.gdesc) : grade.gdesc != null) return false;

return true;
}

@Override
public int hashCode() {
int result = gid;
result = 31 * result + (gname != null ? gname.hashCode() : 0);
result = 31 * result + (gdesc != null ? gdesc.hashCode() : 0);
return result;
}
}

**Grade.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>

<class name="com.lxj.entity.Grade" table="grade" schema="hibernate_study">
<id name="gid" column="gid" type="java.lang.Integer">
<generator class="increment"></generator>
</id>
<property name="gname" column="gname" type="java.lang.String" />
<property name="gdesc" column="gdesc" type="java.lang.String"/>
<!--配置单向的一对多关联关系-->
<set name="students" table="student">
<!--指定关联的外键-->
<key column="gid"></key>
<one-to-many class="com.lxj.entity.Student"></one-to-many>
</set>

</class>
</hibernate-mapping>


**Student.java:**
package com.lxj.entity;

public class Student {
private int sid;
private String sname;
private int sex;
// 多方定义一个一方的引用
private Grade grade;

public Grade getGrade() {
return grade;
}

public void setGrade(Grade grade) {
this.grade = grade;
}

public Student(String sname, int sex) {
this.sname = sname;
this.sex = sex;
}

public Student(){

}



public int getSid() {
return sid;
}

public void setSid(int sid) {
this.sid = sid;
}

public String getSname() {
return sname;
}

public void setSname(String sname) {
this.sname = sname;
}

public int getSex() {
return sex;
}

public void setSex(int sex) {
this.sex = sex;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Student student = (Student) o;

if (sid != student.sid) return false;
if (sex != student.sex) return false;
if (sname != null ? !sname.equals(student.sname) : student.sname != null) return false;

return true;
}

@Override
public int hashCode() {
int result = sid;
result = 31 * result + (sname != null ? sname.hashCode() : 0);
result = 31 * result + sex;
return result;
}
}




**Student.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>

<class name="com.lxj.entity.Student" table="student" schema="hibernate_study">
<id name="sid" column="sid" type="java.lang.Integer">
<generator class="increment"></generator>
</id>
<property name="sname" column="sname"/>
<property name="sex" column="sex"/>

<!--配置多对一关联关系-->
<!--
name:当前实体关联的字段
class:关联那个实体
column:关联的实体对应的字段
-->
<many-to-one name="grade" class="com.lxj.entity.Grade" column="gid"></many-to-one>
</class>
</hibernate-mapping>


**hibernate.cfg.xml:**

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>

<session-factory>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate_study</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>

<!-- 配置hibernate的基本信息 -->

<!-- 配置数据库方言dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
<!-- 指定自动生成数据表的策略 -->
<property name="hbm2ddl.auto">update</property>


<mapping resource="com/lxj/entity/Grade.hbm.xml"/>
<mapping resource="com/lxj/entity/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>


**HibernateUtil:**
package com.lxj.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
private static SessionFactory sessionFactory;
private static Session session;
private static Transaction transaction;

static {
Configuration configuration = new Configuration().configure();
sessionFactory = configuration.buildSessionFactory();
}

/**
* 获取session工厂
* @return
*/
public static SessionFactory getSessionFactory(){
return sessionFactory;
}

/**
* 获取session
* @return
*/
public static Session getSession(){
session = sessionFactory.openSession();
return session;
}

/**
* 关闭session
*/
public static void closeSession(){
if(session != null){
session.close();
}
}
}


TestManyToOne.java:

import com.lxj.entity.Grade;
import com.lxj.entity.Student;
import com.lxj.util.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

public class TestManyToOne {

@Test
public void save(){

Grade g = new Grade("java 1","1班");
Student student1 = new Student("女神",1);
Student student2 = new Student("男神",0);

//这里设置了两次关联关系导致性能下降,多执行了update可以通过**inverse**优化性能
g.getStudents().add(student1);
g.getStudents().add(student2);
student1.setGrade(g);
student2.setGrade(g);

Session session = HibernateUtil.getSession();
Transaction transaction = session.beginTransaction();
session.save(g);
session.save(student1);
session.save(student2);
transaction.commit();
HibernateUtil.closeSession();

}



}