Hibernate环境的配置
hibernate.cfg.xml的配置
<?xml version=‘1.0‘ encoding=‘utf-8‘?><!DOCTYPE hibernate-configuration PUBLIC ???????"-//Hibernate/Hibernate Configuration DTD 3.0//EN" ???????"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration> ???<session-factory> ???????<!-- Database connection settings --> ??????<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> ???????<property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property> ???????<property name="connection.username">scott</property> ???????<property name="connection.password">tiger</property> ???????????????<!-- JDBC connection pool (use the built-in) --> ??????<!-- ?<property name="connection.pool_size">10</property> --> ???????????????<!-- 数据库连接最小连接数 --> ???????<property name="c3p0.min_size">10</property> ???????<!-- 数据库连接最大连接数 --> ???????<property name="c3p0.max_size">100</property> ???????<property name="c3p0.timeout">3000</property> ???????<property name="hibernate.temp.use_jdbc_metadata_defaults">false</property> ????????????<!-- SQL dialect 方言--> ???????<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> ???????<!-- Echo all executed SQL to stdout(控制台) --> ???????<property name="show_sql">true</property> ???????<!-- 格式化sql语句输出查看 --> ???????<property name="format_sql">true</property> ???????????????????????<!-- Drop and re-create the database schema on startup --> ???????<property name="hbm2ddl.auto">update</property> ????????????????<!-- 实体类对应的映射文件 --> ???????<mapping resource="com/zhiyou/entity/User.hbm.xml"/> ???</session-factory></hibernate-configuration>
每一个实体类对应一个映射文件
如User类,拥有username,password,role属性
对应的映射文件User.hbm.xml
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC ???"-//Hibernate/Hibernate Mapping DTD 3.0//EN" ???"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.zhiyou.entity"> ???<!-- class:指定实体类 ??name:实体类名称 ???table:数据库中的表名称 --> ???<class name="User" table="user1"> ???<!-- id主键 --> ???????<id name="name" column="user_name"> ??????????????<!-- 主键生成策略 ???assigned:用户自己指定主键 ?????????????????sequence:指定oracle数据库的序列 ???????????????uuid:产生32位字符串 ???????????????native:自动根据数据库选择主键生成策略 ???????????????--> ??????????<!-- ?<generator class="sequence"> ???????????参数名 ???????????<param name="sequence">seq_test</param> ???????????</generator> --> ???????????????????????<generator class="native"></generator> ???????????????????</id> ???????<property name="pwd" column="password"/> ???????<property name="role" column="role"/> ???</class></hibernate-mapping>
需要注意的是,实体类的映射文件需要在hibernate.cfg.xml进行配置。
一对多
Hibernate,一对多,多对一
原文地址:https://www.cnblogs.com/sonder/p/9175438.html