`
jia106329
  • 浏览: 62256 次
  • 性别: Icon_minigender_2
  • 来自: tj
社区版块
存档分类
最新评论

Hibernate 多对多

阅读更多
oracle10        
Myeclipse自动生成mapping

  
    _____________        __________________
   |             |      |                  |       _____________
   |   EVENTS    |      |   PERSON_EVENT   |      |             |
   |_____________|      |__________________|      |    PERSON   |
   |             |      |                  |      |_____________|
   | *EVENT_ID   | <--> | *EVENT_ID        |      |             |
   |  EVENT_DATE |      | *PERSON_ID       | <--> | *PERSON_ID  |
   |  TITLE      |      |__________________|      |  AGE        |
   |_____________|                                |  FIRSTNAME  |
                                                  |  LASTNAME   |
                                                  |_____________|
 


表events
create table events(
event_id number primary key,
event_date date,
title varchar(10)
)


序列EVENTIDSEQ
create sequence EVENTIDSEQ
minvalue 1
maxvalue 9999999
start with 21
increment by 1
nocache;


触发器Eventsinsert
create or replace trigger Eventsinsert
before insert on events
for each row 
begin
if :new.event_id is null then
select eventidseq.nextval into :new.event_id from dual;
end if;
end


表person
create table person(
       person_id number primary key,
       age number(2),
       firstname varchar(10),
       lastname varchar(10)
)


序列PERONIDSEQ
create sequence PERONIDSEQ
minvalue 1
maxvalue 99999
start with 1
increment by 1
nocache;


触发器PersonInsert
create or replace trigger PersonInsert
before insert on person for each row
begin
if :new.person_id is null then
select eventidseq.nextval into :new.person_id from dual;
end if;
end;


表person_event
create table person_event(
       event_id number,
       person_id number
)

alter table person_event add(constraint event_pk foreign key(event_id) references events(event_id))

alter table person_event add (constraint peron_pk foreign key(person_id) references person(person_id))



Event.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
    <class name="Event" table="EVENTS" schema="TEST">
        <id name="eventId" type="java.lang.Long">
            <column name="EVENT_ID" precision="22" scale="0" />
            <generator class="sequence">
            	<param name="sequence">EVENTIDSEQ</param>
            </generator>
        </id>
        <property name="eventDate" type="java.util.Date">
            <column name="EVENT_DATE" length="7" />
        </property>
        <property name="title" type="java.lang.String">
            <column name="TITLE" length="10" />
        </property>
        <set name="participants" table="PERSON_EVENT" inverse="true" cascade="save-update">
        	<key column="EVENT_ID"/>
        	<many-to-many column="PERSON_ID" class="Person"/>
        </set>
    </class>
</hibernate-mapping>


Person.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
    <class name="Person" table="PERSON" schema="TEST">
        <id name="personId" type="java.lang.Long">
            <column name="PERSON_ID" precision="22" scale="0" />
            <generator class="sequence">
            	<param name="sequence">PERONIDSEQ</param>
            </generator>
        </id>
        <property name="age" type="java.lang.Long">
            <column name="AGE" precision="2" scale="0" />
        </property>
        <property name="firstname" type="java.lang.String">
            <column name="FIRSTNAME" length="10" />
        </property>
        <property name="lastname" type="java.lang.String">
            <column name="LASTNAME" length="10" />
        </property>
		<set name="events" table="PERSON_EVENT" cascade="save-update" lazy="false">
			<key column="PERSON_ID"/>
			<many-to-many column="EVENT_ID" class="Event"/>
		</set>
    </class>
</hibernate-mapping>


PersonEvent.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
    <class name="PersonEvent" table="PERSON_EVENT" schema="TEST">
        <composite-id name="id" class="PersonEventId">
            <key-many-to-one name="events" class="Event">
                <column name="EVENT_ID" precision="22" scale="0" />
            </key-many-to-one>
            <key-many-to-one name="person" class="Person">
                <column name="PERSON_ID" precision="22" scale="0" />
            </key-many-to-one>
        </composite-id>
        <many-to-one name="person" class="Person" insert="false" update="false">
            <column name="PERSON_ID" precision="22" scale="0" />
        </many-to-one>
        <many-to-one name="events" class="Event" insert="false" update="false">
            <column name="EVENT_ID" precision="22" scale="0" />
        </many-to-one>
    </class>
</hibernate-mapping>


Event.java
// default package

import java.util.Date;
import java.util.HashSet;
import java.util.Set;


/**
 * Event generated by MyEclipse - Hibernate Tools
 */

public class Event  implements java.io.Serializable {


    // Fields    

     private Long eventId;
     private Date eventDate;
     private String title;
     private Set participants = new HashSet();


    // Constructors

    public Set getParticipants() {
		return participants;
	}


	public void setParticipants(Set participants) {
		this.participants = participants;
	}


	/** default constructor */
    public Event() {
    }

    
    /** full constructor */
    public Event(Date eventDate, String title, Set personEvents) {
        this.eventDate = eventDate;
        this.title = title;
         
    }

   
    // Property accessors

    public Long getEventId() {
        return this.eventId;
    }
    
    public void setEventId(Long eventId) {
        this.eventId = eventId;
    }

    public Date getEventDate() {
        return this.eventDate;
    }
    
    public void setEventDate(Date eventDate) {
        this.eventDate = eventDate;
    }

    public String getTitle() {
        return this.title;
    }
    
    public void setTitle(String title) {
        this.title = title;
    }
}


Person.java
// default package

import java.util.HashSet;
import java.util.Set;


/**
 * Person generated by MyEclipse - Hibernate Tools
 */

public class Person  implements java.io.Serializable {


    // Fields    

     private Long personId;
     private Long age;
     private String firstname;
     private String lastname;
     private Set events = new HashSet(0);
       
	/** default constructor */
    public Person() {
    }

    
    /** full constructor */
    public Person(Long age, String firstname, String lastname, Set personEvents) {
        this.age = age;
        this.firstname = firstname;
        this.lastname = lastname;
        this.events = personEvents;
    }

   
    // Property accessors

    public Long getPersonId() {
        return this.personId;
    }
    
    public void setPersonId(Long personId) {
        this.personId = personId;
    }

    public Long getAge() {
        return this.age;
    }
    
    public void setAge(Long age) {
        this.age = age;
    }

    public String getFirstname() {
        return this.firstname;
    }
    
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return this.lastname;
    }
    
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }


	public Set getEvents() {
		return events;
	}


	public void setEvents(Set events) {
		this.events = events;
	} 
}


PersonEvent.java
// default package



/**
 * personEvent generated by MyEclipse - Hibernate Tools
 */

public class PersonEvent  implements java.io.Serializable {


    // Fields    

     private PersonEventId id;
     private Person person;
     private Event events;


    // Constructors

    /** default constructor */
    public PersonEvent() {
    }

    
    /** full constructor */
    public PersonEvent(Person person, Event events) {
        this.person = person;
        this.events = events;
    }

   
    // Property accessors

    public PersonEventId getId() {
        return this.id;
    }
    
    public void setId(PersonEventId id) {
        this.id = id;
    }

    public Person getPerson() {
        return this.person;
    }
    
    public void setPerson(Person person) {
        this.person = person;
    }

    public Event getEvents() {
        return this.events;
    }
    
    public void setEvents(Event events) {
        this.events = events;
    }
}


PersonEventId
// default package



/**
 * personEventId generated by MyEclipse - Hibernate Tools
 */

public class PersonEventId  implements java.io.Serializable {


    // Fields    

     private Event events;
     private Person person;


    // Constructors

    /** default constructor */
    public PersonEventId() {
    }

    
    /** full constructor */
    public PersonEventId(Event events, Person person) {
        this.events = events;
        this.person = person;
    }

   
    // Property accessors

    public Event getEvents() {
        return this.events;
    }
    
    public void setEvents(Event events) {
        this.events = events;
    }

    public Person getPerson() {
        return this.person;
    }
    
    public void setPerson(Person person) {
        this.person = person;
    }
   



   public boolean equals(Object other) {
         if ( (this == other ) ) return true;
		 if ( (other == null ) ) return false;
		 if ( !(other instanceof PersonEventId) ) return false;
		 PersonEventId castOther = ( PersonEventId ) other; 
         
		 return ( (this.getEvents()==castOther.getEvents()) || ( this.getEvents()!=null && castOther.getEvents()!=null && this.getEvents().equals(castOther.getEvents()) ) )
 && ( (this.getPerson()==castOther.getPerson()) || ( this.getPerson()!=null && castOther.getPerson()!=null && this.getPerson().equals(castOther.getPerson()) ) );
   }
   
   public int hashCode() {
         int result = 17;
         
         result = 37 * result + ( getEvents() == null ? 0 : this.getEvents().hashCode() );
         result = 37 * result + ( getPerson() == null ? 0 : this.getPerson().hashCode() );
         return result;
   }   
}


Test.java
import java.util.Date;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class Test {
	public static void main(String[] arg) {
		SessionFactory sf = new Configuration().configure().buildSessionFactory();
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		Event event = new Event();
		event.setEventDate(new Date());
		
		Person person = new Person();
		person.setFirstname("dddd");
		person.getEvents().add(event);
		
		event.getParticipants().add(person);
		session.save(event);
		session.getTransaction().commit();
	}
}


Hibernate: select EVENTIDSEQ.nextval from dual
Hibernate: select PERONIDSEQ.nextval from dual
Hibernate: insert into TEST.EVENTS (EVENT_DATE, TITLE, EVENT_ID) values (?, ?, ?)
Hibernate: insert into TEST.PERSON (AGE, FIRSTNAME, LASTNAME, PERSON_ID) values (?, ?, ?, ?)
Hibernate: insert into PERSON_EVENT (PERSON_ID, EVENT_ID) values (?, ?)
分享到:
评论

相关推荐

    hibernate多对多

    【标题】:“Hibernate多对多” 在Java编程领域,Hibernate是一个非常重要的对象关系映射(ORM)框架,它简化了数据库操作,使开发者能够用Java对象来操作数据库数据。"Hibernate多对多"指的是在Hibernate中处理两...

    Hibernate多对多实例+数据库代码

    标题"Hibernate多对多实例+数据库代码"揭示了我们将在讨论一个使用Hibernate框架实现的多对多关系映射的实际案例。在这个实例中,开发人员将两个或多个实体之间的复杂关联转化为简单易懂的数据库操作。描述中提到...

    Hibernate多对多关联添加及查询示例

    本示例主要关注的是Hibernate中的多对多关联映射及其添加与查询方法。 多对多关联是数据库设计中的一种常见关系类型,指的是两个实体之间可以存在多个对应关系。例如,在学生与课程的关系中,一个学生可以选修多门...

    hibernate 多对多映射实例,学生 选课

    在这个“hibernate 多对多映射实例,学生 选课”项目中,我们将探讨如何使用Hibernate处理多对多关系,以及如何构建一个学生选课的系统。 首先,我们要理解多对多关系。在现实生活中,学生和课程之间就是一个典型的...

    hibernate 多对多全注解(单向关联、双向关联)

    hibernate关联映射注解多对多单向关联、

    hibernate多对多关系

    hibernate多对多关系的增删查改 修改有问题

    hibernate多对多单向关联

    多对多单向关联 &lt;br&gt;需要注意映射规则: &lt;set name="users" table="t_user_role"&gt;&lt;br&gt; &lt;key column="roleid"/&gt;&lt;br&gt; &lt;many-to-many class="com.bjsxt.hibernate.User" column="userid"/&gt; &lt;/set&gt;

    hibernate3.5多对多自动建表

    用hibernate3.5 xml文件映射,junit实现多对多自动建表,下载后解压用myeclipse导入,这里要注意:需要junit的包。这个是学hibernate的关键,建议新手作为重点,大虾就请多多指教了

    hibernate多对多双向关联

    博文链接:https://shaqiang32.iteye.com/blog/201317

    hibernate一对多项目

    本项目“hibernate一对多项目”旨在演示如何在JavaWeb应用中使用Hibernate处理一对多的关系映射。这里我们将深入探讨 Hibernate 的一对多关系、配置以及在实际项目中的应用。 首先,一对多关系在数据库中很常见,...

    hibernate 映射关系学习入门 多对多实体映射

    本教程将带你入门Hibernate的多对多实体映射,帮助你理解如何通过源码来实现这种复杂的关系映射。 一、Hibernate概述 Hibernate是一个对象关系映射(ORM)框架,它允许我们将Java对象(实体)与数据库表进行映射,...

    hibernate 一对多和多对一的理解

    hibernate 一对多和多对一的理解 自己的个人看法 大家也来看看

    Hibernate 多表连接分页查询示范项目

    总结,"Hibernate 多表连接分页查询示范项目"是一个实用的示例,它展示了如何在实际应用中利用 Hibernate Criteria API 实现复杂查询和分页,对于学习和掌握 Hibernate 的高级特性和数据库操作技巧非常有帮助。...

    hibernate 多表查询

    hibernate 多表查询 hibernate 多表不同于一般的sql嵌套查询,嵌套查询是不支持的,所以必须要配置好才能实现多表!只要配置好了,还是特别好用的

    Hibernate 一对一,一对多,多对多例子

    “Hibernate 一对一,一对多,多对多例子” 指的是使用Hibernate框架实现数据库中不同关联关系的示例。Hibernate是一个Java持久化框架,它提供了对象关系映射(ORM)功能,使得开发人员可以方便地在Java对象和关系...

    Hibernate 多数据库支持解决方案

    总之,为了实现Hibernate对多数据库的支持,需要精心设计和配置,以确保代码的可移植性,同时兼顾性能和数据一致性。以上各点提供了实现这一目标的基础,但实际项目中可能还需要解决更多特定于数据库的问题。通过...

    hibernate关联映射详解

    包含《多对多双向关联映射》《多对一单向关联映射》《多对一双向关联映射》《一对多单向关联映射》等文档,并有图解及例子,非常适合新手学习,尤其是刚刚接触hibernate,对映射关系不清楚的。。。。

    JAVA框架核心技术笔记2 struts hibernate spring

    "33-37Hibernate1对N案例笔记.pdf"和"38-43Hibernate多对多案例笔记.pdf"则深入探讨了这些关联在实际项目中的应用。 Spring框架是一个全面的后端解决方案,不仅包含MVC框架,还有面向切面编程(AOP)、依赖注入(DI...

    Hibernate1对多1对1多对多关联映射例子源码含xml配置

    在hibernate中,通常配置对象关系映射关系有两种,一种是基于xml的方式,另一种是基于annotation的注解方式,熟话说,萝卜青菜,可有所爱,每个人都有自己喜欢的配置方式,这个是xml配置的例子

    hibernate4多租户项目

    在多租户场景下,Hibernate 4提供了对多租户支持,允许根据不同的租户ID动态切换到对应的数据库Schema,从而实现数据的隔离。 1. **多租户实现方式** Hibernate 4提供了两种主要的多租户实现方式:基于Schema的多...

Global site tag (gtag.js) - Google Analytics