Hibernate NonUniqueObjectException:具有相同标识符值的其他对象已与会话关联

时间:2016-05-26 16:21:42

标签: java hibernate jpa save embeddable

我知道有很多人遇到同样的问题,但是我尝试了很多解决方案并且总是遇到同样的问题,这就是为什么我会再问一次。

SQL

CREATE TABLE "ZONE"(
  "IZONE_ID"   NUMBER(*,0) NOT NULL,
  "SZONE_NAME" VARCHAR2(20) NOT NULL UNIQUE,
  CONSTRAINT PK_ZONES PRIMARY KEY ("IZONE_ID") 
);

CREATE TABLE "ZONE_STATION"(
  "IZONE_ID" NUMBER(*,0) NOT NULL,
  "SSTATION_CODE" VARCHAR2(3 CHAR) NOT NULL,
  CONSTRAINT PK_ZONE_STATION PRIMARY KEY ("IZONE_ID","SSTATION_CODE"),
  CONSTRAINT ZONE_FK FOREIGN KEY ("IZONE_ID") REFERENCES "ZONE" ("IZONE_ID") ON DELETE CASCADE ENABLE
);

在我的cas中我有3个班级

zone.java

@Entity

@Table(name = "ZONE")

@SequenceGenerator(name="ZONE_SEQ", sequenceName="ZONE_SEQ", allocationSize=1)
public class Zone implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ZONE_SEQ")
    @Column(name = "IZONE_ID", nullable = false, unique = true)
    private Long id;

    @Column(name = "SZONE_NAME", length = 20)
    private String zoneName;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "IZONE_ID")
    private Set<ZoneStation> stationList;

//getter + setter

ZoneStation.java

@Entity
@Table(name = "ZONE_STATION")
public class ZoneStation implements Serializable {

    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private ZoneStationId id;

和ZoneStationId.java

@Embeddable
public class ZoneStationId implements Serializable {

    private static final long serialVersionUID = 1L;

    @Column(name = "IZONE_ID")
    private Long zoneId;

    @Column(name = "SSTATION_CODE", length = 3)
    private String stationCode;

ZoneDetail是如何包含来自jsp创建区域的信息的类:

ZoneDetail zoneDetail = new ZoneDetail(); zoneDetail.initFromScreen(params);

和 ZoneDetail.java

public class ZoneDetail implements java.io.Serializable { private java.lang.String code; private java.lang.String name; public static final int NB_DETAIL_STATION = 10; public String[] listStation = null;

和initFromScreen

public void initFromScreen(Hashtable params) {

    setCode((String) ToolsRequest.getParam(params, "zoneCode"));
    setName((String) ToolsRequest.getParam(params, "zoneName"));

    for (int i = 0; i < NB_DETAIL_STATION; i++) {
        if ((String) ToolsRequest.getParam(params, "StationsznameStationszname" + i) != null) {
            String StationName = 
                ((String) ToolsRequest.getParam(params, "StationsznameStationszname" + i));
            setListStation(i, StationName);
        }
        }
    }

我尝试创建一个新区域:

Zone insertZone = new Zone();
    Set<ZoneStation> zoneStationSet =  new HashSet<ZoneStation>();
    //initialisae the Set
    ListDetailsStation = Arrays.asList(zoneDetail.getListStation());
      if (ListDetailsStation.size() > 0){
         Iterator i = ListDetailsStation.iterator();
            while(i.hasNext()){         
                ZoneStationId detailId =  new ZoneStationId();
                ZoneStation zoneStation = new ZoneStation();
                detailId.setStationCode(i.next().toString());
                zoneStation.setId(detailId);
                //zoneStation.setZone(insertZone); 
                zoneStationSet.add(zoneStation);    
            } //end While
        } //end if

insertZone.setZoneName(zoneDetail.getName()); //OK
insertZone.setStationList(zoneStationSet); //OK
zoneDAO.saveOrUpdate(insertZone);

之后进行调试时
zoneDAO.saveOrUpdate(insertZone);

我有 Hibernate NonUniqueObjectException 真的2天我尝试diffrents解决方案,我不检查我的错误在哪里

对于堆栈跟踪:

org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.airfrance.vtruck.entity.ZoneStation#ZoneStationId [zoneId=null, stationCode=]]
    at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:190)
    at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:143)
    at org.hibernate.ejb.event.EJB3SaveOrUpdateEventListener.saveWithGeneratedId(EJB3SaveOrUpdateEventListener.java:62)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:117)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
    at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:685)
    at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:677)
    at org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:252)
    at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:392)
    at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:335)
    at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:204)
    at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:425)
    at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:362)
    at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:338)
    at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:204)
    at org.hibernate.engine.Cascade.cascade(Cascade.java:161)
    at org.hibernate.engine.Cascade.cascade(Cascade.java:127)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.cascadeOnUpdate(DefaultSaveOrUpdateEventListener.java:376)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:350)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:246)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:112)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
    at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:685)
    at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:677)
    at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:673)
    at com.afklm.jraf.provider.persistence.hibernate.AbstractDAOGenericImpl.saveOrUpdateNoFlush(AbstractDAOGenericImpl.java:640)
    ... 71 more

感谢您的回复并抱歉我的英语不好;)

0 个答案:

没有答案