hibernate 类生成表 的实现源码
来源:互联网 发布:淘宝管控交易风险保障 编辑:程序博客网 时间:2024/05/19 13:45
使用hibernate的schemaExport 工具类实现将实体类转换成数据库中的表。在工程设计中,应该先设计表结构而不是先生成实体类,但是这只是个方法,虽然不推荐使用,但是还是需要记下来。
第一步:
在test包中创建一个生成表的java类:
- package com.test;
- import org.hibernate.cfg.Configuration;
- import org.hibernate.tool.hbm2ddl.SchemaExport;
- public class 生成表 {
- /**
- * @param args
- */
- public static void main(String[] args) {
- Configuration cfg = new Configuration().configure();
- SchemaExport ex = new SchemaExport(cfg);
- ex.create(true, true);
- }
- }
第二步:
写一个创建session的类:
- package com.test;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
- public class HibernateSessionFactory {
- private static Configuration cfg = new Configuration().configure();
- private static SessionFactory factory = cfg.buildSessionFactory();
- private static ThreadLocal<Session> local = new ThreadLocal<Session>();
- public static Session getSession(){
- Session session = local.get(); //取
- if (session==null || session.isOpen()==false){
- session = factory.openSession();
- local.set(session); //存
- }
- return session;
- }
- }
第三步:
修改hibernate的配置文件相关属性、驱动。
- <?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="myeclipse.connection.profile">【这里是什么数据库就写什么】mysql</property>
- <property name="connection.url">
- 【数据库连接是什么数据库写什么数据库test
- 】jdbc:mysql://localhost:3306/test
- </property>
- <property name="dialect">
- 【是mysql就写mysql orc就orc】org.hibernate.dialect.MySQLDialect
- </property>
- <property name="connection.username">root</property>
- <property name="connection.password">admin</property>
- <property name="connection.driver_class">
- 【驱动要改 】com.mysql.jdbc.Driver
- </property>
- <property name="show_sql">true</property>
- <property name="format_sql">true</property>
- <mapping resource="com/pojos/TSaleformDetail3.hbm.xml" />
- <mapping resource="com/pojos/TSaleform3.hbm.xml" />
- </session-factory>
- </hibernate-configuration>
第四步 改pojo映射文件:
- <hibernate-mapping>
- <class name="com.pojos.TSaleform3" table="T_SALEFORM3" schema="test">【这地方的schema=“数据库名字”
- 】
本文章转自网络,仅供学习交流用
- hibernate 类生成表 的实现源码
- MyEclipse实现Hibernate生成带注解的po实体类
- AIDL自动生成源码分析以及根据自动生成的源码来自定义类实现IPC
- Hibernate类生成表以及一个hibernate数据库文件的配置
- hibernate 实体类生成表 表没有生成的原因
- 模拟hibernate实现的JAVA源码
- Hibernate 基于注解的SessionFactory类的生成以及自动生成表SchemaExport类的
- Hibernate继承关系映射(了解),实现反向生成数据库表的过程
- JPA使用Hibernate实现,使用UUID.主键的生成策略.
- hibernate自动生成表的代码
- hibernate自动生成表的功能
- Hibernate 实体不生成表的原因
- Hibernate通过Bean生成表的方法
- 采用Hibernate型式的UUID生成类
- hibernate自动生成id类的原因
- hibernate生成的顺序
- Hibernate的主键生成
- hibernate 生成表时不能父类的字段一起生成
- 关于HttpUtility.UrlEncode,HttpUtility.UrlDecode,Server.UrlEncode,Server.UrlDecode
- Ext布局学习整理
- DBCP,C3P0,Tomcat_JDBC druidDatasource 性能及稳定性测试
- IIS下架设网站账户权限配置
- 算法小题2→素数
- hibernate 类生成表 的实现源码
- 金山卫士开源软件之旅(八) netmon下netmon工程的解析---netmon.exe的启动过程分析
- 数据库之搜索效率及优化
- stream 、 string 、byte[] 间的相互转换 扩展方法
- 认识“数据可视化”和“信息可视化”
- 谷歌管理员工具提示robots文件”?User-agent: *”语法有误解决办法
- strace实现原理
- win7下android开发环境搭建(win7 64位)
- 今天你“导”了吗?