`

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 class="native"></generator>
		</id>
		<property name="name"></property>
		<property name="age"></property>
		<property name="birthday" type="timestamp"></property>
		<set name="events" table="Person_Events">
			<key column="person_id"></key>
			<many-to-many column="event_id" class="com.hb.jo.Event"></many-to-many>
		</set>
		<set name="mails" 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 class="native"></generator>
		</id>
		<property name="date" type="timestamp" column="Date"></property>
		<property name="title" column="Title"></property>
	</class>
</hibernate-mapping>
分享到:
评论

相关推荐

    hibernate+中文api

    1.3.2. 单向Set-based的关联 1.3.3. 使关联工作 1.3.4. 值类型的集合 1.3.5. 双向关联 1.3.6. 使双向连起来 1.4. 第三部分 - EventManager web应用程序 1.4.1. 编写基本的servlet 1.4.2. 处理与渲染 1.4.3. ...

    hibernate 3.2 官方中文参考手册(HTML)

    1. Hibernate入门 1.1. 前言 1.2. 第一部分 - 第一个Hibernate应用程序 1.2.1. 第一个class 1.2.2. 映射文件 ...1.3.2. 单向Set-based的关联 1.3.3. 使关联工作 1.3.4. 值类型的集合 · · ·

    Hibernate参考文档

    目录 前言 1. 翻译说明 2. 版权声明 1. Hibernate入门 1.1. 前言 1.2. 第一部分 - 第一个Hibernate应用程序 1.2.1. 第一个class ...1.3.2. 单向Set-based的关联 1.3.3. 使关联工作 1.3.4. 值类型的集合

    Hibernate 中文手册

    - **单向 Set-based 的关联**:介绍单向关联的实现方式及其映射方法。 - **使关联工作**:说明如何确保关联关系能够正确地在对象之间建立。 - **值类型的集合**:讲解如何映射集合类型的属性,特别是基本数据类型...

    hibernate doc 中文版

    - **单向Set-based的关联:** - 如何在不引起循环引用的情况下,建立单向的关联关系。 - **使关联工作:** - 介绍如何通过Hibernate的级联操作来自动处理关联对象的持久化问题。 - **值类型的集合:** - 映射...

    hibernate 3.2中文手册 中文文档

    - **1.3.2 单向Set-based的关联** - 解释单向关联的实现方法,特别是如何使用集合类型进行映射。 - **1.3.3 使关联工作** - 提供实现这些关联的实际步骤,包括如何配置关联关系以确保数据的一致性和完整性。 - ...

    hibernate教程

    - **单向 Set-based 的关联**:定义一个实体类到另一实体类的一对多关系。 - **使关联工作**:配置关联规则,如inverse属性,确保关联的正确维护。 - **值类型的集合**:处理基本类型或自定义值类型集合的映射。 ...

    hibernate说明文档

    - **单向Set-based关联**:使用`set`元素定义一个实体对另一个实体的单向关联。 - **使关联工作**:配置关联的级联行为、外键等细节。 - **值类型的集合**:将非实体对象的集合映射到数据库中。 - **双向关联**...

Global site tag (gtag.js) - Google Analytics