hibernate由关系导出到数据库

本文介绍了一对多关系中使用Hibernate进行数据库映射的方法,包括Classes与Student类的设计及XML配置,演示了如何通过Hibernate创建对应的数据库表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.hp.shangtongwei.hibernate;

 

import java.util.Set;

public class Classes {
 
 private int id;
 
 private String name;
 
 private Set students;
 
 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 getStudents() {
  return students;
 }

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

 

package com.hp.shangtongwei.hibernate;

 

public class Student {
 
 private int id;
 
 private String name;
 
 private Classes classes;

 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 Classes getClasses() {
  return classes;
 }

 public void setClasses(Classes classes) {
  this.classes = classes;
 }
}

 

<?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 package="com.hp.shangtongwei.hibernate">
 <class name="Classes" table="t_classes">
  <id name="id">
   <generator class="native"/>
  </id>
  <property name="name"/>
  <set name="students" inverse="true" cascade="all">
   <key column="classesid"/>
   <one-to-many class="Student"/>
  </set>
 </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>
 <class name="com.hp.shangtongwei.hibernate.Student" table="t_student">
  <id name="id">
   <generator class="native"/>
  </id>
  <property name="name"/>
  <many-to-one name="classes" column="classesid"/>
 </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">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>
      
    <session-factory>
     <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_one2many_1</property>
  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.connection.password">root</property>
  <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="hibernate.show_sql">true</property>
  
  <mapping resource="com/hp/shangtongwei/hibernate/Classes.hbm.xml"/>
  <mapping resource="com/hp/shangtongwei/hibernate/Student.hbm.xml"/>
    </session-factory>

</hibernate-configuration>

 

 

package com.hp.shangtongwei.hibernate;

 

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class ExportDB {

 public static void main(String[] args) {
  
  //读取hibernate.cfg.xml文件
  Configuration cfg = new Configuration().configure();
  
  SchemaExport export = new SchemaExport(cfg);
  
  export.create(true, true);
 }
}

 

 package com.hp.shangtongwei.hibernate;

 

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

public class HibernateUtils {

 private static SessionFactory factory;
 
 static {
  try {
   Configuration cfg = new Configuration().configure();
   factory = cfg.buildSessionFactory();
  }catch(Exception e) {
   e.printStackTrace();
  }
 }
 
 public static SessionFactory getSessionFactory() {
  return factory;
 }
 
 public static Session getSession() {
  return factory.openSession();
 }
 
 public static void closeSession(Session session) {
  if (session != null) {
   if (session.isOpen()) {
    session.close();
   }
  }
 }
}

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值