首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

关于hibernate的一对多投射

2012-09-02 
关于hibernate的一对多映射两个类:People和City?package com.pk.poimport java.util.Setpublic class Ci

关于hibernate的一对多映射

两个类:People和City

?

package com.pk.po;

import java.util.Set;

public class City {
?private int id;
?private String name;
?private Set cityes;
?public Set getCityes() {
??return cityes;
?}
?public void setCityes(Set cityes) {
??this.cityes = cityes;
?}
?public int getId() {
??return id;
?}
?public void setId(int id) {
??this.id = id;
?}
?public String getName() {
??return name;
?}
?public void setName(String name) {
??this.name = name;
?}

}

?

package com.pk.po;

public class People {
?private int id;
?private String name;

?public int getId() {
??return id;
?}
?public void setId(int id) {
??this.id = id;
?}
?public String getName() {
??return name;
?}
?public void setName(String name) {
??this.name = name;
?}


}

?

映射文件(ORM):

?

City.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">
<hibernate-mapping package="com.pk.po">
?<class name="City">
??<id name="id">
???<generator 表示两表相关联

-->


??<set name="cityes" cascade="all" >
???<key column="cityid"></key>
???<one-to-many />
??</set>
?</class>
?
</hibernate-mapping>

?

?

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">
<hibernate-mapping package="com.pk.po">
?<class name="People" >
??<id name="id">
???<generator class="native"></generator>
??</id>
??<property name="name"></property>
??
?</class>
?
</hibernate-mapping>

?

配置文件

<!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">com.mysql.jdbc.Driver</property>
??<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_hql</property>
??<property name="hibernate.connection.username">root</property>
??<property name="hibernate.connection.password">root</property>
??<property name="show_sql">true</property>
??<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
??
??
??
??<mapping resource="com/pk/po/City.hbm.xml"/>
??<mapping resource="com/pk/po/People.hbm.xml"/>
?</session-factory>
</hibernate-configuration>

?

热点排行