Hibernate多對多映射為第三張表manytomany【xml和註解】執行個體(十七)

來源:互聯網
上載者:User

1 這個方法比較不錯 【原先的多對多失敗了就是因為沒有進行第三張表映射造成的】

package com.sm.hibernate.pojo;import java.util.Set;public class IUser {private int id;private String name;private ICard iCard;private I2Card i2Card;private Set<ICourse> courses;public Set<ICourse> getCourses() {return courses;}public void setCourses(Set<ICourse> courses) {this.courses = courses;}public I2Card getI2Card() {return i2Card;}public void setI2Card(I2Card i2Card) {this.i2Card = i2Card;}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 ICard getiCard() {return iCard;}public void setiCard(ICard iCard) {this.iCard = iCard;}}

<?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="IUser" table="iuser"><id name="id"><generator class="native"></generator></id><property name="name" not-null ="true"></property><one-to-one name="iCard"></one-to-one><many-to-one name="i2Card" unique="true"></many-to-one><set name="courses" table="iuc"><key column="iUserId"></key><many-to-many class="ICourse" column="iCourseId"></many-to-many></set>    </class></hibernate-mapping>

package com.sm.hibernate.pojo;import java.util.Set;public class ICourse {private int id;private String name;private Set<IUser> iUsers;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 Set<IUser> getiUser() {return iUsers;}public void setiUser(Set<IUser> iUser) {this.iUsers = iUser;}}

<?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="ICourse" table="icourse"><id name="id"><generator class="native"></generator></id><property name="name" not-null ="true"></property><set name="iUsers" table="iuc"><key column="iCourseId"></key><many-to-many class="ICourse" column="iUserId"></many-to-many></set>    </class></hibernate-mapping>

3結果

九月 27, 2013 10:11:44 上午 org.hibernate.annotations.common.Version <clinit>INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}九月 27, 2013 10:11:44 上午 org.hibernate.Version logVersionINFO: HHH000412: Hibernate Core {4.1.1}九月 27, 2013 10:11:44 上午 org.hibernate.cfg.Environment <clinit>INFO: HHH000206: hibernate.properties not found九月 27, 2013 10:11:44 上午 org.hibernate.cfg.Environment buildBytecodeProviderINFO: HHH000021: Bytecode provider name : javassist九月 27, 2013 10:11:44 上午 org.hibernate.cfg.Configuration configureINFO: HHH000043: Configuring from resource: /hibernate.cfg.xml九月 27, 2013 10:11:44 上午 org.hibernate.cfg.Configuration getConfigurationInputStreamINFO: HHH000040: Configuration resource: /hibernate.cfg.xml九月 27, 2013 10:11:44 上午 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 10:11:44 上午 org.hibernate.cfg.Configuration addResourceINFO: HHH000221: Reading mappings from resource: com/sm/hibernate/pojo/Book.hbm.xml九月 27, 2013 10:11:44 上午 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 10:11:44 上午 org.hibernate.cfg.Configuration addResourceINFO: HHH000221: Reading mappings from resource: com/sm/hibernate/pojo/IUser.hbm.xml九月 27, 2013 10:11:44 上午 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 10:11:44 上午 org.hibernate.cfg.Configuration addResourceINFO: HHH000221: Reading mappings from resource: com/sm/hibernate/pojo/ICard.hbm.xml九月 27, 2013 10:11:44 上午 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 10:11:44 上午 org.hibernate.cfg.Configuration addResourceINFO: HHH000221: Reading mappings from resource: com/sm/hibernate/pojo/I2Card.hbm.xml九月 27, 2013 10:11:44 上午 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 10:11:44 上午 org.hibernate.cfg.Configuration addResourceINFO: HHH000221: Reading mappings from resource: com/sm/hibernate/pojo/ICourse.hbm.xml九月 27, 2013 10:11:44 上午 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 10:11:44 上午 org.hibernate.cfg.Configuration addResourceINFO: HHH000221: Reading mappings from resource: com/sm/hibernate/pojo/Category.hbm.xml九月 27, 2013 10:11:44 上午 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 10:11:44 上午 org.hibernate.cfg.Configuration doConfigureINFO: HHH000041: Configured SessionFactory: null九月 27, 2013 10:11:45 上午 org.hibernate.dialect.Dialect <init>INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect九月 27, 2013 10:11:45 上午 org.hibernate.tool.hbm2ddl.SchemaExport executeINFO: HHH000227: Running hbm2ddl schema export九月 27, 2013 10:11:45 上午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configureINFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)九月 27, 2013 10:11:45 上午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configureINFO: HHH000115: Hibernate connection pool size: 20九月 27, 2013 10:11:45 上午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configureINFO: HHH000006: Autocommit mode: false九月 27, 2013 10:11:45 上午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configureINFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/wanju]九月 27, 2013 10:11:45 上午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configureINFO: HHH000046: Connection properties: {user=root, password=****}    alter table book         drop         foreign key FK2E3AE949F733E    alter table icard         drop         foreign key FK5F61EF9DFFCD6A8    alter table iuc         drop         foreign key FK198B735A1D52C九月 27, 2013 10:11:45 上午 org.hibernate.tool.hbm2ddl.SchemaExport performERROR: HHH000389: Unsuccessful: alter table iuc drop foreign key FK198B735A1D52C    alter table iuc         drop         foreign key FK198B76096495C九月 27, 2013 10:11:45 上午 org.hibernate.tool.hbm2ddl.SchemaExport performERROR: Table 'wanju.iuc' doesn't exist九月 27, 2013 10:11:45 上午 org.hibernate.tool.hbm2ddl.SchemaExport performERROR: HHH000389: Unsuccessful: alter table iuc drop foreign key FK198B76096495C九月 27, 2013 10:11:45 上午 org.hibernate.tool.hbm2ddl.SchemaExport performERROR: Table 'wanju.iuc' doesn't exist    alter table iuc         drop         foreign key FK198B76F252F3C九月 27, 2013 10:11:45 上午 org.hibernate.tool.hbm2ddl.SchemaExport performERROR: HHH000389: Unsuccessful: alter table iuc drop foreign key FK198B76F252F3C九月 27, 2013 10:11:45 上午 org.hibernate.tool.hbm2ddl.SchemaExport performERROR: Table 'wanju.iuc' doesn't exist    alter table iuser         drop         foreign key FK5FE8FB4D3B53C79    drop table if exists book    drop table if exists category    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    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 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)    )    alter table book         add index FK2E3AE949F733E (categoryId),         add constraint FK2E3AE949F733E         foreign key (categoryId)         references category (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)九月 27, 2013 10:11:47 上午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl stopINFO: HHH000030: Cleaning up connection pool [jdbc:mysql://localhost:3306/wanju]九月 27, 2013 10:11:47 上午 org.hibernate.tool.hbm2ddl.SchemaExport executeINFO: HHH000230: Schema export complete


註解版看的稍微簡單:

package com.bjsxt.hibernate;import java.util.HashSet;import java.util.Set;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.JoinTable;import javax.persistence.ManyToMany;import javax.persistence.OneToMany;import javax.persistence.Table;@Entitypublic class Teacher {private int id;private String name;private Set<Student> students = new HashSet<Student>();@Id@GeneratedValuepublic 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;}@ManyToMany@JoinTable(name="t_s",joinColumns={@JoinColumn(name="teacher_id")},inverseJoinColumns={@JoinColumn(name="student_id")})public Set<Student> getStudents() {return students;}public void setStudents(Set<Student> students) {this.students = students;}}

package com.bjsxt.hibernate;import java.util.HashSet;import java.util.Set;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.ManyToMany;@Entitypublic class Student {private int id;private String name;private Set<Teacher> teachers = new HashSet<Teacher>();@ManyToMany(mappedBy="students")public Set<Teacher> getTeachers() {return teachers;}public void setTeachers(Set<Teacher> teachers) {this.teachers = teachers;}@Id@GeneratedValuepublic 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;}}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.