本文主要是介绍Hibernate从入门到精通(2)-component(组件),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Hibernate使用组件(component)
基于映射文件
hibernate.cfg.xml:
<?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"><hibernate-configuration><session-factory><property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property><property name="hibernate.connection.password">xukai</property><property name="hibernate.connection.url">jdbc:oracle:thin:@10.25.116.164:1521:orcl</property><property name="hibernate.connection.username">xukai</property><property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property><!-- <property name="hibernate.hbm2ddl.auto">create</property> --><property name="hibernate.show_sql">true</property><property name="hibernate.format_sql">true</property><strong><mapping resource="com/edu/entity/People.hbm.xml"/></strong></session-factory>
</hibernate-configuration>
People.java(getter.setter省略)
package com.edu.entity;import java.io.Serializable;public class People implements Serializable{private static final long serialVersionUID = 1L;private Integer peopleId;private String peopleName;private Address address;}
组件类: Address.java( getter.setter省略) package com.edu.entity;public class Address {private String parentName;private String childName;}
People.hbm.xml: <?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 2015-10-27 8:44:47 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping><class name="com.edu.entity.People" table="PEOPLE"><id name="peopleId" type="java.lang.Integer"><column name="PEOPLEID" /><generator class="sequence" ><param name="sequece">seq_people_id</param></generator></id><property name="peopleName" type="java.lang.String"><column name="PEOPLENAME" /></property><!-- Bean中的属性名 --><component name="address" class="com.edu.entity.Address"><!-- 表中的列名 --><property name="parentName" type="java.lang.String"><column name="PARENT_NAME" length="20"></column></property><!-- 表中的列名 --><property name="childName" type="java.lang.String"><column name="CHILD_NAME" length="20"></column></property></component></class>
</hibernate-mapping>
基于注解
hibernate.cfg.xml:
<?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"><hibernate-configuration><session-factory><property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property><property name="hibernate.connection.password">xukai</property><property name="hibernate.connection.url">jdbc:oracle:thin:@10.25.116.164:1521:orcl</property><property name="hibernate.connection.username">xukai</property><property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property><!-- <property name="hibernate.hbm2ddl.auto">create</property> --><property name="hibernate.show_sql">true</property><property name="hibernate.format_sql">true</property><strong><mapping class="com.edu.entity.People"/></strong></session-factory>
</hibernate-configuration>
package com.edu.entity;import java.io.Serializable;import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;@Entity
@Table(name="people")
public class People implements Serializable{private static final long serialVersionUID = 1L;@Id@SequenceGenerator(name="seq_gener",sequenceName="seq_people_id",allocationSize=1)@GeneratedValue(generator="seq_gener",strategy=GenerationType.SEQUENCE)@Column(name="people_id")private Integer peopleId;@Column(name="people_name")private String peopleName;@Embedded()@AttributeOverrides({@AttributeOverride(name="parentName",column=@Column(name="parent_name")),@AttributeOverride(name="childName",column=@Column(name="chile_name"))})private Address address;}
Address.java(省略getter.setter):
package com.edu.entity;import javax.persistence.Embeddable;//可嵌入的
@Embeddable
public class Address {private String parentName;private String childName;}
这篇关于Hibernate从入门到精通(2)-component(组件)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!