Hibernate中配置文件
主配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration SYSTEM "classpath://org/hibernate/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!--数据库连接属性配置--> <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="hibernate.connection.password">tiger</property> <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property> <property name="hibernate.connection.username">c##scott</property> <!--数据库架构名 注意大写(使用hibernate tools 必须)--> <property name="hibernate.default_schema">C##SCOTT</property> <!--数据库方言--> <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property> <!--显示sql语句--> <property name="hibernate.show_sql">true</property> <!--对数据库表的操作--> <property name="hbm2ddl.auto">update</property> <property name="hibernate.search.autoregister_listeners">false</property> <property name="hibernate.validator.apply_to_ddl">false</property> <!--配置文件映射的映射文件--> <!-- <mapping resource="com/xx/dao/Userinfo.hbm.xml" /> --> <!-- <mapping resource="com/test/News.hbm.xml" /> --> <mapping resource="com/mapping/Factory.hbm.xml" /> <mapping resource="com/mapping/Product.hbm.xml" /> </session-factory> </hibernate-configuration>
多对一映射文件
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated Jan 16, 2016 11:43:24 AM by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping > <class name="com.mapping.Factory" table="FACTORY"> <id name="id" type="java.lang.Integer"> <column name="ID" /> <generator class="native" /> </id> <property name="name" type="java.lang.String"> <column name="NAME" /> </property> <!-- cascade="all" inverse="false" 级联删除 --> <!--多对一配置 属性 private Set<Product> products; --> <set name="products" table="product"> <key column="factoryId"/><!-- 外键字段 --> <one-to-many class="com.mapping.Product"/> </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"> <!-- Generated Jan 16, 2016 11:43:24 AM by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <class name="com.mapping.Product" table="PRODUCT"> <id name="id" type="java.lang.Integer"> <column name="ID" /> <generator class="native" /> </id> <property name="name" type="java.lang.String"> <column name="NAME" /> </property> <property name="price" type="double"> <column name="PRICE" /> </property> <!--多对一配置 属性 private Factory factory; --> <many-to-one name="factory" class="com.mapping.Factory" column="FACTORYID"/> </class> </hibernate-mapping>
反向工程需要的配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" > <hibernate-reverse-engineering> <table-filter match-name="USERINFO"/> </hibernate-reverse-engineering>
含有复合主键的映射文件
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping SYSTEM "classpath://org/hibernate/hibernate-mapping-3.0.dtd"> <!-- Generated Jan 12, 2016 7:56:21 PM by Hibernate Tools 4.3.1 --> <hibernate-mapping> <class name="com.xx.dao.Userinfo" table="USERINFO" optimistic-lock="version"> <!--复合主键 单独一个实现Serializable接口的类 --> <composite-id name="id" class="com.xx.dao.UserinfoId"> <key-property name="userid" type="short"> <column name="USERID" precision="4" scale="0" /> </key-property> <key-property name="username" type="string"> <column name="USERNAME" length="15" /> </key-property> </composite-id> <property name="userage" type="java.lang.Short" > <column name="USERAGE" precision="3" scale="0" /> </property> </class> </hibernate-mapping>