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();
}
}
}
}
- hibernate由关系导出到数据库
- 由Hibernate配置文件到数据库表的转换
- Hibernate映射数据库关系
- C# 由DataGridView导出到Excel
- 从关系数据库到非关系数据库
- hibernate自动导出数据库表
- 数据库表导出hibernate实体
- Hibernate 中把一对多关系的数据保存到数据库
- SqlServer导出关系图到.sql文件
- 导出数据库数据到Excel
- 导出数据库数据到Excel
- 导出数据库数据到Excel
- 数据库数据导出到excel
- 数据库导出到EXCEL向导
- 数据库导出到EXCEL向导
- 数据库导出到xml中
- Java数据库导出到excel
- 数据库导出模型到PDM
- s2sh的注册与登录系列七
- How to convert djvu to pdf in Ubuntu
- 用GAE搞了一个站。
- struts2,spring,ibatis初探
- struts2,spring,ibatis,hibernate
- hibernate由关系导出到数据库
- css精灵
- 【推荐】十个开源的Javascript框架
- 全面阐述overflow:hidden属性
- openbsd 安装向导和优化
- C#项目如何制作安装程序
- CSS+DIVweb排版的定义及常见的浏览器兼容问题
- [编程学习]C++ 函数模板学习总结
- Gps应用基础知识