SSH多对多插入问题

huangliao6252 2015-08-15
两个实体分别是User和Good 他们是多对多 然后用拆分为两个多对一
User{
  private int user_id;
  get 和set 方法
}
Good{
  private int good_id;
  get 和 set方法
}
拆分为两个多对一 增加一个items实体
对应代码


@Entity
@Table(name="items")
@IdClass(value=ItemsPk.class)
public class Items {
   private Good good;
   private S_User user;
   private int num;
   @Id
   @ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="good_id",referencedColumnName="good_id")
   public Good getGood() {
  return good;
  }
  public void setGood(Good good) {
this.good = good;
  }

  @Id
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="user_id",referencedColumnName="user_id")
  public S_User getUser() {
return user;
  }
  public void setUser(S_User user) {
this.user = user;
  }
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
用联合主键
public class ItemsPk implements Serializable{

/**
*
*/
private static final long serialVersionUID = 1L;
       private Good good;
   private S_User user;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="good_id",referencedColumnName="good_id")
public Good getGood() {
return good;
}
public void setGood(Good good) {
this.good = good;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="user_id",referencedColumnName="user_id")
public S_User getUser() {
return user;
}
public void setUser(S_User user) {
this.user = user;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((good == null) ? 0 : good.hashCode());
result = prime * result + ((user == null) ? 0 : user.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ItemsPk other = (ItemsPk) obj;
if (good == null) {
if (other.good != null)
return false;
} else if (!good.equals(other.good))
return false;
if (user == null) {
if (other.user != null)
return false;
} else if (!user.equals(other.user))
return false;
return true;
}

}

在调用     Good good=goodService.FindGood(good_Id);
Items item=new Items();
item.setGood(good);
item.setUser((S_User)session.get("user"));
                       hibernateTemplate.save(item);时候出错了
异常信息Caused by: org.hibernate.HibernateException: identifier of an instance of com.dzsc.entity.Items was altered from com.dzsc.entity.ItemsPk@20774e32 to com.dzsc.entity.ItemsPk@1f59e06b

然后我改了hashcode的代码
public int hashCode() {
    int result = (int) good.getGood_id();     
         result = 31 * result + (int) (good.getGood_id() >>> 32);     
         result = 31 * result + (int)user.getUser_id();
         return 31 * result + (int) (user.getUser_id() >>> 32);
    }
就会出现不一样的异常
java.lang.NullPointerException
at com.dzsc.entity.ItemsPk.hashCode(ItemsPk.java:41)
at java.lang.Object.toString(Object.java:237)
at java.lang.String.valueOf(String.java:2847)
at java.lang.StringBuffer.append(StringBuffer.java:232)
at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:58)
at org.hibernate.event.def.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:164)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:120)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:196)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
调试发现 每次调用完hashcode代码之后item.user就变为null了 而item.good没有改变导致 hibernateTemplate.save(item);时候出错了,为何出现这种原因有什么解决的方法  希望大家帮忙一下 谢谢
Global site tag (gtag.js) - Google Analytics