`
chaoyi
  • 浏览: 290186 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Hibernate 多对多(MaryToMary)

 
阅读更多

例子:学生与科目的多对多

学生实体类

package entity;

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

public class Pupil {
	private int id;
	private String name;
	private Set<Subject> subject=new HashSet<Subject>();
	
	public Pupil() {
	}
	public Pupil(String name) {
		this.name = 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;
	}
	public Set<Subject> getSubject() {
		return subject;
	}
	public void setSubject(Set<Subject> subject) {
		this.subject = subject;
	}	
}

 

学生实体类的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>
	<class name="entity.Pupil">
		<id name="id">
			<generator class="sequence">
				<param name="sequence">seq_pupil</param>
			</generator>
		</id>
		<property name="name"></property>
		<set name="subject" table="p_s">
			<key column="p_id" />
			<many-to-many class="entity.Subject" column="s_id" />
		</set>
	</class>
</hibernate-mapping>

 

科目实体类

package entity;

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

public class Subject {
	private int id;
	private String name;
	private Set<Pupil> pupil=new HashSet<Pupil>();
	public Subject() {
	}
	public Subject(String name) {
		this.name = 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;
	}
	public Set<Pupil> getPupil() {
		return pupil;
	}
	public void setPupil(Set<Pupil> pupil) {
		this.pupil = pupil;
	}
}

 

科目实体类的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>
	<class name="entity.Subject">
		<id name="id">
			<generator class="sequence">
				<param name="sequence">seq_subject</param>
			</generator>
		</id>
		<property name="name"></property>
		<set name="pupil" table="p_s">  
        	<key column="s_id"/>  
        	<many-to-many class="entity.Pupil" column="p_id"/>  
    	</set> 
	</class>
</hibernate-mapping>

 

使用hibernate.cfg.xml映射其的XML路径

<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
	<!-- 数据库URL -->
	<property name="connection.url">
		jdbc:oracle:thin:@localhost:1521:oracle11
	</property>
	<!-- 数据库用户 -->
	<property name="connection.username">A_hr</property>
	<!-- 数据库用户密码 -->
	<property name="connection.password">123456</property>
	<!-- 数据库 JDBC 驱动 -->
	<property name="connection.driver_class">
		oracle.jdbc.driver.OracleDriver
	</property>
	<!-- 是否将运行期生成的 SQL 输出到日志以供调试 -->
	<property name="show_sql">true</property>
	<!-- 每个数据库都有其对应的 Dialect 以匹配其平台特征 -->
	<property name="dialect">
		org.hibernate.dialect.Oracle10gDialect
	</property>
	<property name="hbm2ddl.auto">create</property>
	<mapping resource="entity/Pupil.hbm.xml" />
	<mapping resource="entity/Subject.hbm.xml" />
</session-factory>
</hibernate-configuration>

 

进行数据的操作

package test;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import entity.Pupil;
import entity.Subject;

import util.HibernateSessionFactory;

public class MaryToMaryTest {
	public static void main(String[] args) {
		maryToMary();
	}

	private static void maryToMary() {
		Session session = HibernateSessionFactory.getSession();
		Transaction tx = null;
		Pupil pupil1 = new Pupil("吴者然");
		Pupil pupil2 = new Pupil("景临境");
		Subject subject1 = new Subject("Java");
		Subject subject2 = new Subject("php");
		subject1.getPupil().add(pupil1);
		subject1.getPupil().add(pupil2);
		subject2.getPupil().add(pupil1);
		try {
			tx = session.beginTransaction();
			session.save(pupil1);
			session.save(pupil2);
			session.save(subject1);
			session.save(subject2);
			tx.commit();
			System.out.println("保存成功!!!");
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		}finally{
			HibernateSessionFactory.closeSession();
		}
	}
}

 

效果图:

 

在数据库查询这三条语句

select * from pupil;
select * from subject;
select * from p_s;

 

 

 



 

 

 

  • 大小: 43.2 KB
  • 大小: 10.4 KB
  • 大小: 10 KB
  • 大小: 10.6 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics