熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Hibernate多對多雙向關聯(xml配置)

2013-11-23 20:15:57  來源: Java開源技術 

  

  

  Rolejava

  package commanymanybean;

  import javautilSet;

  public class Role {

  private int id;

  private String name;

  private Set<User> users;

  public int getId() {

  return id;

  }

  public void setId(int id) {

  thisid = id;

  }

  public String getName() {

  return name;

  }

  public void setName(String name) {

  thisname = name;

  }

  public Set<User> getUsers() {

  return users;

  }

  public void setUsers(Set<User> users) {

  thisusers = users;

  }

  }

  Userjava

  package commanymanybean;

  import javautilSet;

  public class User {

  private int id;

  private String name;

  private Set<Role> roles;

  public int getId() {

  return id;

  }

  public void setId(int id) {

  thisid = id;

  }

  public String getName() {

  return name;

  }

  public void setName(String name) {

  thisname = name;

  }

  public Set<Role> getRoles() {

  return roles;

  }

  public void setRoles(Set<Role> roles) {

  thisroles = roles;

  }

  }

  映射文件

  Rolehbmxml

  <?xml version= encoding=UTF?>

  <!DOCTYPE hibernatemapping PUBLIC

  //Hibernate/Hibernate Mapping DTD //EN

  hiber/hibernatemappingdtd>

  <hibernatemapping package=commanymanybean>

  <class name=Role table=roles catalog=Hibernate_ManyMany>

  <id name=id column=id>

  <generator class=native/>

  </id>

  <property name=name column=name/>

  <set name=users table=user_role cascade=all>

  <key>

  <column name=role_id/>

  </key>

  <manytomany column=user_id class=User/>

  </set>

  </class>

  </hibernatemapping>

  Userhbmxml

  <?xml version= encoding=UTF?>

  <!DOCTYPE hibernatemapping PUBLIC

  //Hibernate/Hibernate Mapping DTD //EN

  hiber/hibernatemappingdtd>

  <hibernatemapping package=manymanybean>

  <class name=User table=users catalog=Hibernate_ManyMany>

  <id name=id column=id>

  <generator class=native/>

  </id>

  <property name=name column=name/>

  <set name=roles table=user_role cascade=all>

  <key>

  <column name=user_id/>

  </key>

  <manytomany column=role_id class=Role/>

  </set>

  </class>

  </hibernatemapping>

  導出的sql

  create table Hibernate_ManyManyroles (

  id integer not null auto_increment

  name varchar()

  primary key (id)

  )

  create table Hibernate_ManyManyusers (

  id integer not null auto_increment

  name varchar()

  primary key (id)

  )

  create table user_role (

  role_id integer not null

  user_id integer not null

  primary key (user_id role_id)

  )

  alter table user_role

  add index FKBFADABF (role_id)

  add constraint FKBFADABF

  foreign key (role_id)

  references Hibernate_ManyManyroles (id)

  alter table user_role

  add index FKBFAFFDF (user_id)

  add constraint FKBFAFFDF

  foreign key (user_id)

  references Hibernate_ManyManyusers (id)

  測試代碼

  @Test

  public void insert(){

  Session session=HibernateSessionFactorygetSession();

  Transaction transaction=sessionbeginTransaction();

  try {

  transactionbegin();

  User user=new User();

  usersetName(張三);

  Role role=new Role();

  rolesetName(管理員);

  Set<Role> roles=new HashSet<Role>();

  rolesadd(role);

  usersetRoles(roles);

  sessionpersist(user);

  mit();

  } catch (HibernateException e) {

  eprintStackTrace();

  transactionrollback();

  }

  }

  @Test

  public void select(){

  Session session=HibernateSessionFactorygetSession();

  User user=(User) sessionget(Userclass );

  Systemoutprintln(usergetName());

  Set<Role> roles=usergetRoles();

  for (Iterator<Role> iterator = erator(); iteratorhasNext();) {

  Role role = (Role) iteratornext();

  Systemoutprintln(rolegetName());

  }

  }


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28194.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.