Hibernate關聯映射

來源:互聯網
上載者:User

標籤:format   bsp   source   1.0   ssi   private   table   center   close   

1.單項多對一關聯

首先建立實體類

public class Qx {

private int jdid;
private String jdname;
// Getters & Setters ...

}

public class Jd{

private int qxid;
private String qxname;

private Qx qx

 // Getters & Setters ..;

}

<!--  Qx類對應檔 - -> 

<?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>
<class name="cn.yunhe.entity.Qx" table="tb_qx">
<id name="qxid" column="qxid" type="java.lang.Integer">
<generator class="native"/>
</id>
<property name="qxname" column="qxname" type="java.lang.String"/>

<set name="jds" inverse="false" cascade="all"> <!--反轉-->
<key column="qxid"/>
<one-to-many class="cn.yunhe.entity.Jd"/>
</set>
</class>

</hibernate-mapping>

<!-- Jd對應檔 - ->

<?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>
<class name="cn.yunhe.entity.Jd" table="tb_jd">
<id name="jdid" column="jdid" type="java.lang.Integer">
<generator class="native"/>
</id>
<property name="jdname" column="jdname" type="java.lang.String"/>

<!--多對一-->
<many-to-one name="qx" class="cn.yunhe.entity.Qx" column="qx_id"/>

</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="hbm2ddl.auto">update</property> <!--正向工程-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/head</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">076634</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>

 

<mapping resource="hbm/Jd.hbm.xml"/>
<mapping resource="hbm/Qx.hbm.xml"/>
<mapping resource="hbm/Course.hbm.xml"/>
<mapping resource="hbm/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>

 

2:雙對多

public class Course {

private int courseid;
private String courseName;

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

 

public class Student {

//學生

private int studentid;
private String studentname;
private Set<Course> courses = new HashSet<Course>();

 

<?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>
<class name="cn.yunhe.entity.Course" table="course">
<id name="courseid" column="courseid" type="java.lang.Integer">
<generator class="native"/>
</id>
<property name="coursename" column="coursename"/>




<set name="students" inverse="true" table="r_course">
<!-- 首先對應自己的外鍵 cours——id -->
<key>
<column name="course_id" precision="11" scale="0"/>
</key>

<!--多對多-->
<many-to-many entity-name="cn.yunhe.entity.Student">
<column name="stu_id" precision="11" scale="0"/>
</many-to-many>
</set>
</class>
</hibernate-mapping>

 

 

 

<?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>
<class name="cn.yunhe.entity.Student" table="student">
<id name="studentid" column="studentid">
<generator class="native"/>
</id>

<property name="studentname" column="studentname" type="java.lang.String"/>




<set name="courses" table="r_course">
<key>
<column name="stu_id"/>
</key>

<!-- 多對多 -->
<many-to-many entity-name="cn.yunhe.entity.Course">
<column name="course_id"/>
</many-to-many>
</set>

</class>
</hibernate-mapping>

 

測試類別

 

package cn.yunhe.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import cn.yuhe.entity.Course;
import cn.yuhe.entity.Student;


public class Demo {

Session session = null;
Transaction tr = null;

@Before
public void setUp() {
Configuration config = new Configuration().configure();
SessionFactory factory = config.buildSessionFactory();
session = factory.openSession();
tr = session.beginTransaction();
}


@Test
public void manyTomany(){

Course course1 = new Course();
Course course2 = new Course();

course1.setCourseName("java");
course2.setCourseName("c");

session.save(course1);
session.save(course2);

Student stu1 = new Student();
Student stu2 = new Student();

stu1.setStudentName("小樣");
stu2.setStudentName("集雨");

stu1.getCourses().add(course1);
stu1.getCourses().add(course2);
stu2.getCourses().add(course1);
stu2.getCourses().add(course2);

session.save(stu1);
session.save(stu2);
tr.commit();
}

@After
public void tearDown() {
session.close();
}


}

 

Hibernate關聯映射

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.