Hibernate继承映射之使用多子表方式实例

1

package com.sm.hibernate.pojo;

import java.util.Set;

public class User {

	private int id;
	private String name;
	private int qq;
	private Grade grade;
	private Set<Course> courses;
	
	public Set<Course> getCourses() {
		return courses;
	}
	public void setCourses(Set<Course> courses) {
		this.courses = courses;
	}
	public Grade getGrade() {
		return grade;
	}
	public void setGrade(Grade grade) {
		this.grade = grade;
	}
	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;
	}
	public int getQq() {
		return qq;
	}
	public void setQq(int qq) {
		this.qq = qq;
	}
}

事实证明 用名字 User2.hbm.xml也是可以的,只要 hibernate.cfg.xml里面引入改为User2.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.sm.hibernate.pojo">
	<class name="User" table="hib_user">
		<id name="id">
			<generator class="native"></generator>
		</id>
		<property name="name"></property>
		<property name ="qq"></property>
		<many-to-one name = "grade"  cascade="all" column="gradeid" class ="Grade">
		</many-to-one>
		
		<set name="courses" table="hib_uc">
			<key column="userid"></key>
			<many-to-many column="courseid" class="Course"></many-to-many>
		</set>
		<joined-subclass name="Guest" table="user_guest">
			<key column="guestId"></key>
			<property name="email"/>
		</joined-subclass>
		
    </class>
</hibernate-mapping>

2 别的情况和 上篇文章的 继承关系映射一样的。

package com.sm.hibernate.pojo;

public class Guest extends User{

	private String email;

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}
	
}


3 结果

九月 27, 2013 1:52:53 下午 org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
九月 27, 2013 1:52:53 下午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.1}
九月 27, 2013 1:52:53 下午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
九月 27, 2013 1:52:53 下午 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
九月 27, 2013 1:52:53 下午 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
九月 27, 2013 1:52:53 下午 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
九月 27, 2013 1:52:53 下午 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
九月 27, 2013 1:52:53 下午 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com/sm/hibernate/pojo/Course.hbm.xml
九月 27, 2013 1:52:53 下午 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
九月 27, 2013 1:52:54 下午 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com/sm/hibernate/pojo/User2.hbm.xml
九月 27, 2013 1:52:54 下午 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
九月 27, 2013 1:52:54 下午 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com/sm/hibernate/pojo/Grade.hbm.xml
九月 27, 2013 1:52:54 下午 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
九月 27, 2013 1:52:54 下午 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com/sm/hibernate/pojo/Book.hbm.xml
九月 27, 2013 1:52:54 下午 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
九月 27, 2013 1:52:54 下午 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com/sm/hibernate/pojo/IUser.hbm.xml
九月 27, 2013 1:52:54 下午 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
九月 27, 2013 1:52:54 下午 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com/sm/hibernate/pojo/ICard.hbm.xml
九月 27, 2013 1:52:54 下午 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
九月 27, 2013 1:52:54 下午 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com/sm/hibernate/pojo/I2Card.hbm.xml
九月 27, 2013 1:52:54 下午 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
九月 27, 2013 1:52:54 下午 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com/sm/hibernate/pojo/ICourse.hbm.xml
九月 27, 2013 1:52:54 下午 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
九月 27, 2013 1:52:54 下午 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com/sm/hibernate/pojo/Category.hbm.xml
九月 27, 2013 1:52:54 下午 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
九月 27, 2013 1:52:54 下午 org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
九月 27, 2013 1:52:54 下午 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
九月 27, 2013 1:52:54 下午 org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000227: Running hbm2ddl schema export
九月 27, 2013 1:52:54 下午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
九月 27, 2013 1:52:54 下午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20
九月 27, 2013 1:52:54 下午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: false
九月 27, 2013 1:52:54 下午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/wanju]
九月 27, 2013 1:52:54 下午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=root, password=****}

    alter table book 
        drop 
        foreign key FK2E3AE949F733E

    alter table hib_uc 
        drop 
        foreign key FKB76DA40CDA23F8

    alter table hib_uc 
        drop 
        foreign key FKB76DA40CF393D18

    alter table hib_user 
        drop 
        foreign key FK929519C9F09DB8C2

    alter table icard 
        drop 
        foreign key FK5F61EF9DFFCD6A8

    alter table iuc 
        drop 
        foreign key FK198B735A1D52C

    alter table iuc 
        drop 
        foreign key FK198B76096495C

    alter table iuc 
        drop 
        foreign key FK198B76F252F3C

    alter table iuser 
        drop 
        foreign key FK5FE8FB4D3B53C79

    alter table user_guest 
        drop 
        foreign key FK72AA3864571C2FE5
九月 27, 2013 1:52:55 下午 org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: alter table user_guest drop foreign key FK72AA3864571C2FE5

    drop table if exists book
九月 27, 2013 1:52:55 下午 org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: Table 'wanju.user_guest' doesn't exist

    drop table if exists category

    drop table if exists hib_course

    drop table if exists hib_grade

    drop table if exists hib_uc

    drop table if exists hib_user

    drop table if exists i2card

    drop table if exists icard

    drop table if exists icourse

    drop table if exists iuc

    drop table if exists iuser

    drop table if exists user_guest

    create table book (
        id integer not null auto_increment,
        name varchar(200) not null,
        author varchar(50) not null,
        categoryId integer,
        primary key (id)
    )

    create table category (
        id integer not null auto_increment,
        name varchar(200) not null,
        primary key (id)
    )

    create table hib_course (
        id integer not null auto_increment,
        name varchar(255),
        primary key (id)
    )

    create table hib_grade (
        id integer not null auto_increment,
        name varchar(255),
        classNum integer,
        primary key (id)
    )

    create table hib_uc (
        courseid integer not null,
        userid integer not null,
        primary key (userid, courseid)
    )

    create table hib_user (
        id integer not null auto_increment,
        name varchar(255),
        qq integer,
        gradeid integer,
        primary key (id)
    )

    create table i2card (
        id integer not null auto_increment,
        name varchar(255) not null,
        primary key (id)
    )

    create table icard (
        id integer not null,
        name varchar(255) not null,
        primary key (id)
    )

    create table icourse (
        id integer not null auto_increment,
        name varchar(255) not null,
        primary key (id)
    )

    create table iuc (
        iUserId integer not null,
        iCourseId integer not null,
        primary key (iCourseId, iUserId)
    )

    create table iuser (
        id integer not null auto_increment,
        name varchar(255) not null,
        i2Card integer unique,
        primary key (id)
    )

    create table user_guest (
        guestId integer not null,
        email varchar(255),
        primary key (guestId)
    )

    alter table book 
        add index FK2E3AE949F733E (categoryId), 
        add constraint FK2E3AE949F733E 
        foreign key (categoryId) 
        references category (id)

    alter table hib_uc 
        add index FKB76DA40CDA23F8 (courseid), 
        add constraint FKB76DA40CDA23F8 
        foreign key (courseid) 
        references hib_course (id)

    alter table hib_uc 
        add index FKB76DA40CF393D18 (userid), 
        add constraint FKB76DA40CF393D18 
        foreign key (userid) 
        references hib_user (id)

    alter table hib_user 
        add index FK929519C9F09DB8C2 (gradeid), 
        add constraint FK929519C9F09DB8C2 
        foreign key (gradeid) 
        references hib_grade (id)

    alter table icard 
        add index FK5F61EF9DFFCD6A8 (id), 
        add constraint FK5F61EF9DFFCD6A8 
        foreign key (id) 
        references iuser (id)

    alter table iuc 
        add index FK198B735A1D52C (iUserId), 
        add constraint FK198B735A1D52C 
        foreign key (iUserId) 
        references icourse (id)

    alter table iuc 
        add index FK198B76096495C (iUserId), 
        add constraint FK198B76096495C 
        foreign key (iUserId) 
        references iuser (id)

    alter table iuc 
        add index FK198B76F252F3C (iCourseId), 
        add constraint FK198B76F252F3C 
        foreign key (iCourseId) 
        references icourse (id)

    alter table iuser 
        add index FK5FE8FB4D3B53C79 (i2Card), 
        add constraint FK5FE8FB4D3B53C79 
        foreign key (i2Card) 
        references i2card (id)

    alter table user_guest 
        add index FK72AA3864571C2FE5 (guestId), 
        add constraint FK72AA3864571C2FE5 
        foreign key (guestId) 
        references hib_user (id)
九月 27, 2013 1:52:58 下午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH000030: Cleaning up connection pool [jdbc:mysql://localhost:3306/wanju]
九月 27, 2013 1:52:58 下午 org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

静山晚风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值