hibernate由关系导出到数据库

来源:互联网 发布:知乎网站源码 编辑:程序博客网 时间:2024/06/17 01:18

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();
   }
  }
 }
}

 

 

 

原创粉丝点击