0

こんにちは私は冬眠するのが初めてです。

通常のサービスとバンドルサービス(通常のサービスの組み合わせ)があるようなシナリオがあります

私は次のようなテーブルを作成しました

CREATE TABLE `dealer_service` (
`id_service` INT(11) NOT NULL AUTO_INCREMENT,
`service_name` VARCHAR(45) NULL DEFAULT NULL COMMENT ,
`short_desc` VARCHAR(45) NULL DEFAULT NULL COMMENT ,
`full_desc` VARCHAR(500) NULL DEFAULT NULL COMMENT ,
`cost` FLOAT NULL DEFAULT NULL COMMENT,
`currency` VARCHAR(45) NULL DEFAULT NULL COMMENT ,
`tax` FLOAT NULL DEFAULT NULL COMMENT ,
`discounts` FLOAT NULL DEFAULT NULL COMMENT ,

PRIMARY KEY (`id_service`),
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;

通常のサービスとバンドルサービス間のマッピング

CREATE TABLE `service_to_services` (
`service_id_main` INT(11) NOT NULL,
`service_id_sub` INT(11) NOT NULL,
PRIMARY KEY (`service_id_main`, `service_id_sub`)
 )
 COMMENT='maping table between service to service if service is bundle'
  COLLATE='utf8_general_ci'
 ENGINE=InnoDB;

Hibernateクラスは

import static javax.persistence.GenerationType.IDENTITY;

@Entity
@Table(name = "dealer_service")
public class DealerService implements java.io.Serializable {

private Integer                 idService;
private String                  serviceName;
private String                  shortDesc;
private String                  fullDesc;
private String                  offeredBy;
private String                  terms;
private String                  discriminator;
.
    .
    .

private Set<DealerService>      subServices;

//セッターとゲッター

@ManyToMany(cascade = { CascadeType.MERGE, CascadeType.PERSIST,
        CascadeType.ALL }, fetch = FetchType.EAGER)
@JoinTable( name = "service_to_services",
            joinColumns = { @JoinColumn(name = "service_id_main") },
            inverseJoinColumns = { @JoinColumn(name = "service_id_sub") })
public Set<DealerService> getSubServices() {
    return subServices;
}

public void setSubServices(Set<DealerService> subServices) {
    this.subServices = subServices;
}

}

問題は、サービスインシデントと呼ばれるもう1つのテーブルが必要なことです。pojoクラスは次のようになります。

@Entity
@Table(na me = "service_incident")
public class ServiceIncident implements java.io.Serializable {


private Set<DealerService>selectedServices=  new HashSet<DealerService>(0);


 @ManyToMany(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER)
@JoinTable( name = "work_list",
            joinColumns = { @JoinColumn(name = "service_incident_id") },
            inverseJoinColumns = { @JoinColumn(name = "dealer_service_id") })
public Set<DealerService> getSelectedServices() {
    return selectedServices;
}

public void setSelectedServices(Set<DealerService> selectedServices) {
    this.selectedServices = selectedServices;
}                                       0);
}

選択したサービスをサービスインシデントに挿入しようとしているときに、次のような例外が発生します

 org.springframework.dao.DuplicateKeyException: a different object with the same identifier value was already associated with the session: [cc.carcloud.domain.DealerService#2]; nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [cc.carcloud.domain.DealerService#2]
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:662)
at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:683)
at cc.carcloud.dao.GenericDaoImpl.save(GenericDaoImpl.java:46)
at cc.carcloud.serviceimpl.ServiceIncidentServiceImpl.add(ServiceIncidentServiceImpl.java:47)
at cc.carcloud.wscontroller.ServiceIncidentWSController.addServiceIncident(ServiceIncidentWSController.java:92)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:322)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:116)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.authentication.www.DigestAuthenticationFilter.doFilter(DigestAuthenticationFilter.java:209)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:184)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:155)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
Caused by: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [cc.carcloud.domain.DealerService#2]
at org.hibernate.engine.StatefulPersistenceContext.checkUniqueness(StatefulPersistenceContext.java:590)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:284)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:223)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:89)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
at org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:218)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:296)
at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:242)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:219)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
at org.hibernate.event.def.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:456)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:334)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:686)
at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:1)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
... 53 more 

誰かが私が間違っているところを助けてくれますか..

前もって感謝します。

4

1 に答える 1

1

メッセージ例外は、問題が何であるかを示します。を呼び出そうとしているsave()ため、デタッチされたオブジェクトをセッションにアタッチしようとしています。ただし、セッションにはすでに同じIDの添付オブジェクトが含まれています。

次のような状況で発生する可能性があります。

DealerService detachedDealerService = new DealerService();
detachedDealerService.setId(2);

DealerService attachedDealerService = session.get(DealerService.class, 2);
session.save(detachedDealerService);
// exception: the dealer service with ID 2 is already in the session

save()IDが自動的に生成される場合、すでにIDを持っているオブジェクトを呼び出すこと自体がエラーであることに注意してください。save()新しい永続オブジェクトを作成するために使用されます。

于 2012-07-24T07:31:21.427 に答える