例子:学生与科目的多对多
学生实体类
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;
相关推荐
hibernate多对多关系映射案例,配有对应的jar包。
Hibernate多对多关联添加及查询示例
这里包含了hibernate多对一单向关联关系实现源码,希望对你有用。
Hibernate一对一,一对多,多对多实例
NULL 博文链接:https://1028826685.iteye.com/blog/1536060
详尽说明了hibernate的多对多原理。
Hibernate关于注解的一对多,多对多,一对一案例。。。。
Hibernate的多对一和一对多操作实例
NULL 博文链接:https://gaoquanyang.iteye.com/blog/1213765
hibernate 一对多和多对一的理解 自己的个人看法 大家也来看看
hibernate一对多案例,里边用到的jar包我上传过
Hibernate多对多案例笔记Hibernate多对多案例笔记Hibernate多对多案例笔记Hibernate多对多案例笔记
hibernate多对一映射(XML)版
hibernate关联映射注解多对多单向关联、
加入了hibernate框架的javaWeb项目,里面包含了一对多的典型配置
该实例实现了Hibernate的多对多关联映射,附带了数据库代码,容易理解
hibernate(多对多关系映射)实现存储,内附sql脚本和详细的注释,适合初学者
hibernate多对多的关系实例hibernate多对多的关系实例hibernate多对多的关系实例