首页 > 代码库 > hibernate.cfg.xml
hibernate.cfg.xml
<?xml version=‘1.0‘ encoding=‘utf-8‘?><!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD//EN""http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!--声明Hibernate配置文件的开始--> <hibernate-configuration> <!--表明以下的配置是针对session-factory配置的, SessionFactory是Hibernate中的一个类,这个类主要负责保存HIbernate的配置信息,以及对Session的操作--> <session-factory> <!--配置数据库的驱动程序,Hibernate在连接数据库时,需要用到数据库的驱动程序--> <property name="hibernate.connection.driver_class"> oracle.jdbc.driver.OracleDriver </property> <property name="hibernate.connection.url"> jdbc:oracle:thin:@192.168.1.249:1521:orcl </property> <!--连接数据库的用户名--> <property name="hibernate.connection.username">scott</property> <!--连接数据库的密码--> <property name="hibernate.connection.password">sc1234</property> <!--hibernate.dialect 是Hibernate使用的数据库方言,就是要用Hibernate连接哪种类型的数据库服务器。--> <property name="dialect"> org.hibernate.dialect.OracleDialect </property> <property name="hibernate.temp.use_jdbc_metadata_defaults">false</property> <!--hibernate.hbm2ddl.auto 指定由java代码生成数据库脚本,进而生成具体的表结构的具体方式 --> <!-- <property name="hbm2ddl.auto">update</property> --> <!--是否在后台显示Hibernate生成的查询数据库的SQL语句,开发时设置为true,便于查询错误,程序运行时可以在Eclipse的控制台显示Hibernate执行的Sql语句。项目部署后可以设置为false,提高运行效率--> <property name="show_sql">true</property> <!--指定映射文件为"com/reiyen/hibernate/domain/User.hbm.xml"--> <mapping resource="com/seecen/config/User.hbm.xml"></mapping> <mapping resource="com/seecen/config/Dept.hbm.xml"></mapping> </session-factory> </hibernate-configuration>
hibernate.cfg.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"><!-- Mapping file autogenerated by MyEclipse Persistence Tools--><hibernate-mapping> <class name="com.seecen.entity.Dept" table="DEPT" schema="SCOTT"> <id name="deptno" type="java.lang.Long"> <column name="deptno" precision="22" scale="0" /> <generator class="assigned"></generator> </id> <property name="dname" type="java.lang.String"> <column name="DNAME" length="30" /> </property> <set name="users" inverse="true"> <key column="deptno"></key> <one-to-many class="com.seecen.entity.User"/> </set> </class></hibernate-mapping>
<?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"><!-- Mapping file autogenerated by MyEclipse Persistence Tools--><hibernate-mapping> <class name="com.seecen.entity.User" table="T_USER" schema="SCOTT"> <id name="id" type="java.lang.Long"> <column name="ID" precision="22" scale="0" /> <generator class="sequence"> <param name="sequence">SEQUENCE_USERID</param> </generator> </id> <property name="userName" type="java.lang.String"> <column name="USERNAME" length="30" /> </property> <property name="pwd" type="java.lang.String"> <column name="PWD" length="30" /> </property> <many-to-one name="dept" class="com.seecen.entity.Dept" column="deptno" cascade="save-update"> </many-to-one> </class></hibernate-mapping>
package com.seecen.dao;import java.util.Iterator;import java.util.List;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.boot.registry.StandardServiceRegistryBuilder;import org.hibernate.cfg.Configuration;import org.hibernate.service.ServiceRegistry;import com.seecen.entity.Dept;import com.seecen.entity.User;public class UserDao { private static ServiceRegistry serviceRegistry = null; private static Configuration cfg = null; private static SessionFactory sessionFactory = null; static { cfg = new Configuration().configure(); serviceRegistry = new StandardServiceRegistryBuilder().applySettings( cfg.getProperties()).build(); sessionFactory = cfg.buildSessionFactory(serviceRegistry); } public static SessionFactory getSessionFactory() { return sessionFactory; } public static void main(String[] args) { Session session = null; Transaction ts = null; try { session = UserDao.getSessionFactory().openSession(); ts = session.beginTransaction(); // // User u = (User)session.load(User.class, 30L);// System.out.println(u.getUserName()); // User user = (User) session.get(User.class,450L);// // user.setUserName("aa");// session.update(user); // //添加,瞬时状态// User user = new User();// user.setUserName("大圣小兵");// session.save(user); // //修改,持久状态// User u = (User)session.load(User.class, 491L);// u.setUserName("大圣大兵");// u.setPwd("1324");// session.update(u); // 删除// User u = (User)session.load(User.class, 491L);// session.delete(u); // 迭代器迭代 Dept d = (Dept)session.load(Dept.class, 10L); System.out.println(d.getDname()); Iterator<User> u = d.getUsers().iterator(); while(u.hasNext()){ System.out.println(u.next().getUserName()); } ts.commit(); } catch (Exception e) { ts.rollback(); e.printStackTrace(); } finally { session.close(); sessionFactory.close(); } System.out.println("--end--"); }}
hibernate.cfg.xml
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。