首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

hibernate单向set-based联系关系

2012-09-20 
hibernate单向set-based关联person有多个event,一个event有多个person参与,明显的多对多关联,这篇只建立简

hibernate单向set-based关联

person有多个event,一个event有多个person参与,明显的多对多关联,这篇只建立简单的one2many关联,下节继续many2many关联。

com.hb.jo包下实体类Person、Event及相应的映射描述文件。我们建立person-event的一对多关联,通过一个中间表使用两个表id关联。

?

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><!-- Database connection settings -->        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>        <property name="connection.url">jdbc:mysql://127.0.0.1:3306/htest</property>        <property name="connection.username">root</property>        <property name="connection.password">123456</property>        <!-- JDBC connection pool (use the built-in) -->        <property name="connection.pool_size">1</property>        <!-- SQL dialect -->        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>        <!-- Enable Hibernate's automatic session context management -->        <property name="current_session_context_class">thread</property>        <!-- Disable the second-level cache  -->        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>        <!-- Echo all executed SQL to stdout -->        <property name="show_sql">true</property>        <!-- Drop and re-create the database schema on startup -->        <property name="hbm2ddl.auto">update</property>        <mapping resource="com/hb/jo/event.hbm.xml"/><mapping resource="com/hb/jo/person.hbm.xml"/> </session-factory></hibernate-configuration>
?

?

Person.java

?

package com.hb.jo;import java.util.Date;import java.util.HashSet;import java.util.Set;public class Person {private long id;private String name;private int age;private Date birthday;private Set<Event> events = new HashSet<Event>();private Set<String> mails = new HashSet<String>();public Set<String> getMails() {return mails;}public void setMails(Set<String> mails) {this.mails = mails;}public Set<Event> getEvents() {return events;}public void setEvents(Set<Event> events) {this.events = events;}public long getId() {return id;}public void setId(long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}}

Event.java

?

?

package com.hb.jo;import java.util.Date;public class Event {private long id;private String title;private Date date;//因为hibernate通过java的反射机制创建对象,所以必须提供无参的构造public Event(){}public long getId() {return id;}public void setId(long id) {this.id = id;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public Date getDate() {return date;}public void setDate(Date date) {this.date = date;}}

?

person.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><class name="com.hb.jo.Person" table="Person"><id name="id" column="person_id"><generator type="timestamp"></property><set name="events" table="Person_Events"><key column="person_id"></key><many-to-many column="event_id" table="Person_Mail"><key column="person_id"></key><element column="person_mail" type="string"></element></set></class></hibernate-mapping>

?event.hmb.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><class name="com.hb.jo.Event" table="Events"><id name="id" column="event_id"><generator type="timestamp" column="Date"></property><property name="title" column="Title"></property></class></hibernate-mapping>

热点排行