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 logVersionINFO: 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 buildBytecodeProviderINFO: HHH000021: Bytecode provider name : javassist九月 27, 2013 1:52:53 下午 org.hibernate.cfg.Configuration configureINFO: HHH000043: Configuring from resource: /hibernate.cfg.xml九月 27, 2013 1:52:53 下午 org.hibernate.cfg.Configuration getConfigurationInputStreamINFO: HHH000040: Configuration resource: /hibernate.cfg.xml九月 27, 2013 1:52:53 下午 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntityWARN: 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 addResourceINFO: HHH000221: Reading mappings from resource: com/sm/hibernate/pojo/Course.hbm.xml九月 27, 2013 1:52:53 下午 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntityWARN: 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 addResourceINFO: HHH000221: Reading mappings from resource: com/sm/hibernate/pojo/User2.hbm.xml九月 27, 2013 1:52:54 下午 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntityWARN: 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 addResourceINFO: HHH000221: Reading mappings from resource: com/sm/hibernate/pojo/Grade.hbm.xml九月 27, 2013 1:52:54 下午 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntityWARN: 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 addResourceINFO: HHH000221: Reading mappings from resource: com/sm/hibernate/pojo/Book.hbm.xml九月 27, 2013 1:52:54 下午 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntityWARN: 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 addResourceINFO: HHH000221: Reading mappings from resource: com/sm/hibernate/pojo/IUser.hbm.xml九月 27, 2013 1:52:54 下午 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntityWARN: 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 addResourceINFO: HHH000221: Reading mappings from resource: com/sm/hibernate/pojo/ICard.hbm.xml九月 27, 2013 1:52:54 下午 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntityWARN: 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 addResourceINFO: HHH000221: Reading mappings from resource: com/sm/hibernate/pojo/I2Card.hbm.xml九月 27, 2013 1:52:54 下午 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntityWARN: 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 addResourceINFO: HHH000221: Reading mappings from resource: com/sm/hibernate/pojo/ICourse.hbm.xml九月 27, 2013 1:52:54 下午 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntityWARN: 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 addResourceINFO: HHH000221: Reading mappings from resource: com/sm/hibernate/pojo/Category.hbm.xml九月 27, 2013 1:52:54 下午 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntityWARN: 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 doConfigureINFO: 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 executeINFO: HHH000227: Running hbm2ddl schema export九月 27, 2013 1:52:54 下午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configureINFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)九月 27, 2013 1:52:54 下午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configureINFO: HHH000115: Hibernate connection pool size: 20九月 27, 2013 1:52:54 下午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configureINFO: HHH000006: Autocommit mode: false九月 27, 2013 1:52:54 下午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configureINFO: 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 configureINFO: 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 performERROR: 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 performERROR: 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 stopINFO: HHH000030: Cleaning up connection pool [jdbc:mysql://localhost:3306/wanju]九月 27, 2013 1:52:58 下午 org.hibernate.tool.hbm2ddl.SchemaExport executeINFO: HHH000230: Schema export complete