peacess

          統(tǒng)計

          留言簿(14)

          積分與排名

          閱讀排行榜

          評論排行榜

          hibernate 多對多關系在mysql、oracle中的不同

          用戶(User)與角色(Role)多對多關系(UserRole)
          下面是表的代碼(配置為annotation)
          User:

          package sub;
          import java.io.Serializable;
          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;
          @Entity
          public class User implements Serializable
          {
              /**
               * 
               */
              private static final long serialVersionUID = 1L;
              private Long id;
              private Set<Role> roles;
              @Id
              @GeneratedValue
              public Long getId()
              {
                  return id;
              }
              public void setId(Long id)
              {
                  this.id = id;
              }
              @ManyToMany
              @JoinTable(name="UserRole",joinColumns={@JoinColumn(name="user_ID")},inverseJoinColumns={@JoinColumn(name="role_ID")})
              public Set<Role> getRoles()
              {
                  return roles;
              }
              public void setRoles(Set<Role> roles)
              {
                  this.roles = roles;
              }
          }

          Role:

          package sub;
          import java.io.Serializable;
          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;
          @Entity
          public class Role implements Serializable
          {
              /**
               * 
               */
              private static final long serialVersionUID = 1L;
              private Long id;
              private Set<User> users;
              @Id
              @GeneratedValue
              public Long getId()
              {
                  return id;
              }
              public void setId(Long id)
              {
                  this.id = id;
              }
              @ManyToMany
              @JoinTable(name="UserRole",joinColumns={@JoinColumn(name="role_ID")},inverseJoinColumns={@JoinColumn(name="user_ID")})
              public Set<User> getUsers()
              {
                  return users;
              }
              public void setUsers(Set<User> users)
              {
                  this.users = users;
              }
          }

          UserRoleId:

          package sub;
          import java.io.Serializable;
          import javax.persistence.Embeddable;
          @Embeddable
          public class UserRoleId implements Serializable
          {
              /**
               * 
               */
              private static final long serialVersionUID = 1L;
              private Long userId;
              private Long roleId;
              public UserRoleId(Long userId, Long roleId)
              {
                  this.userId = userId;
                  this.roleId = roleId;
              }
              public UserRoleId()
              {
                  this(null,null);
              }
              @Override
              public boolean equals(Object obj)
              {
                  if(obj == this)return true;
                  if(obj instanceof UserRoleId)
                  {
                      final UserRoleId other = (UserRoleId) obj;
                      Long userT = getUserId();
                      Long userOther = other.getUserId();
                      Long roleT = getRoleId();
                      Long roleOther = other.getRoleId();

                      return ((userT == userOther) || (userT != null &&
                              userOther != null && userT.equals(userOther))) &&
                              ((roleT == roleOther) || (roleT != null &&
                                      roleOther != null && roleT.equals(
                                              roleOther)));
                  }
                  return false;
              }
              @Override
              public int hashCode()
              {
                  int re = 17;
                  re += (userId == null?0:userId.hashCode());
                  re += (roleId == null?0:roleId.hashCode());
                  return 37*re;
              }
              public Long getUserId()
              {
                  return userId;
              }
              public void setUserId(Long userId)
              {
                  this.userId = userId;
              }
              public Long getRoleId()
              {
                  return roleId;
              }
              public void setRoleId(Long roleId)
              {
                  this.roleId = roleId;
              }
          }

          UserRole:

          package sub;
          import java.io.Serializable;
          import javax.persistence.AttributeOverride;
          import javax.persistence.AttributeOverrides;
          import javax.persistence.Column;
          import javax.persistence.EmbeddedId;
          import javax.persistence.Entity;
          import javax.persistence.FetchType;
          import javax.persistence.JoinColumn;
          import javax.persistence.ManyToOne;
          @Entity
          public class UserRole implements Serializable
          {
              /**
               * 
               */
              private static final long serialVersionUID = 1L;
              private UserRoleId id;
              private Role role;
              private User user;
              @EmbeddedId
              @AttributeOverrides( {
                      @AttributeOverride(name = "userId", column = @Column(name = "user_ID", unique = false, nullable = false, insertable = true, updatable = true)),
                      @AttributeOverride(name = "roleId", column = @Column(name = "role_ID", unique = false, nullable = false, insertable = true, updatable = true)) })
              public UserRoleId getId()
              {
                  return id;
              }
              @ManyToOne(cascade = {})
              @JoinColumn(name = "role_ID", unique = false, nullable = false, insertable = false, updatable = false)
              public Role getRole()
              {
                  return role;
              }
              @ManyToOne(cascade = {}, fetch = FetchType.EAGER)
              @JoinColumn(name = "user_ID", unique = false, nullable = false, insertable = false, updatable = false)
              public User getUser()
              {
                  return user;
              }
              public void setId(UserRoleId id)
              {
                  this.id = id;
              }
              public void setRole(Role role)
              {
                  this.role = role;
              }
              public void setUser(User user)
              {
                  this.user = user;
              }
          }

              在mysql數(shù)據(jù)庫中,保存UserRole時出錯。而完全相同的代碼在oracle數(shù)據(jù)中正確。
          操作如下:
                  User user;
                  Role role;
                  //經(jīng)過多步操作以后,user與role都保存到數(shù)據(jù)庫,與之相關聯(lián)的session已關閉
                  Session s = null;//取得s的代碼沒有寫出來
                  s.beginTransaction();
                  UserRole userRole = new UserRole();
                  userRole.setId(new UserRoleId(user.getId(),role.getId()));
                  userRole.setUser(user);
                  userRole.setRole(role);
                  s.persist(userRole);
                  s.getTransaction().commit();
          //一提交就出錯,提示user沒有保存,但是奇怪的是為什么在oracle中完全正確。
          //最后在mysql中作如下的修改可以正確保存
          //把 userRole.setUser(user)與userRole.setRole(role)刪除
          有那位知道是什么問題,請告之

          posted on 2007-06-11 23:19 中東 閱讀(2150) 評論(1)  編輯  收藏 所屬分類: hibernate

          評論

          # re: hibernate 多對多關系在mysql、oracle中的不同 2007-06-12 08:12 flybean

          Output the SQLs  回復  更多評論   


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 师宗县| 西贡区| 浠水县| 射洪县| 星子县| 浑源县| 辽源市| 临海市| 德清县| 通化县| 隆林| 茶陵县| 会理县| 文成县| 垣曲县| 玉溪市| 鹰潭市| 平安县| 乌什县| 项城市| 华宁县| 准格尔旗| 宁明县| 崇文区| 九台市| 大余县| 都江堰市| 九龙城区| 任丘市| 沧州市| 怀柔区| 高阳县| 石家庄市| 长治市| 新宾| 贞丰县| 南川市| 株洲市| 嘉定区| 西青区| 芦山县|