我如何使用findBy方法

时间:2014-03-29 06:49:16

标签: spring hibernate spring-mvc

这是我的主表     @Entity(名称=" EnumMaster")     公共课EnumMaster {         @ID         @ org.hibernate.annotations.Type(类型=" PG-UUID&#34)         private UUID EnumerationID;         私有字符串名称;         私有字符串文本;         私有字符串描述;

public EnumMaster(String parentId) {

        this.Name=parentId;
    }
getter setter
}

此表包含其值

Entity(name = "EnumValue")
public class EnumValue {

    @Id
    @org.hibernate.annotations.Type(type="pg-uuid")
    private UUID EnumValueID;
    @ManyToOne
    private EnumMaster enumMaster;
    private String Name;
    private String Text;
    private String Description;
    setter getters
}

控制器

@RequestMapping(value="/retrivefromPublicationDropDown",method=RequestMethod.GET,produces="application/json")
public @ResponseBody
    ArrayList getPublicationStatus(NativeWebRequest req){
        ArrayList<EnumValue> arrayList=new ArrayList<EnumValue>();
        try{
            **String parentID="ePublicationStatus";
            Collection<EnumValue> list=dropDownService.findByEnumMaster(parentID);**
            arrayList=this.populateStatus(list);

        }
        catch (Exception e) {
            logger.error(" *** Get Brands by partner ID", e);
        }
        return arrayList;

}

private ArrayList<EnumValue> populateStatus(Collection<EnumValue> list) {
    ArrayList<EnumValue>eval= new ArrayList<EnumValue>();
    for(EnumValue ev:list){
        eval.add(ev);
    }
    return eval;
}

服务

public Collection<EnumValue> findByEnumMaster(String parentId) {

        return dropDownRepository.findByEnumMaster(new EnumMaster(parentId));
    }

我真正想做的是通过在EnumMaster中传递名称来获取值并获取值 那么我怎么能使用findBy方法,因为它现在给我错误

错误: -

org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.dts.adminportal.dto.EnumMaster; nested exception is java.lang.IllegalStateException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.dts.adminportal.dto.EnumMaster
    at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:298) ~[spring-orm-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:106) ~[spring-orm-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:403) ~[spring-orm-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:58) ~[spring-tx-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213) ~[spring-tx-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:163) ~[spring-tx-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) ~[spring-aop-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    at org.springframework.data.jpa.repository.support.LockModeRepositoryPostProcessor$LockModePopulatingMethodIntercceptor.invoke(LockModeRepositoryPostProcessor.java:92) ~[spring-data-jpa-1.3.0.RELEASE.jar:na]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) ~[spring-aop-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90) ~[spring-aop-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) ~[spring-aop-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) ~[spring-aop-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    at com.sun.proxy.$Proxy81.findByEnumMaster(Unknown Source) ~[na:na]
    at com.dts.adminportal.service.DropDownServiceImpl.findByEnumMaster(DropDownServiceImpl.java:31) ~[classes/:na]
    at com.dts.adminportal.controller.DropDownController.getPublicationStatus(DropDownController.java:35) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_45]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.7.0_45]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.7.0_45]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.7.0_45]
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213) [spring-web-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126) [spring-web-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96) [spring-webmvc-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617) [spring-webmvc-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578) [spring-webmvc-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80) [spring-webmvc-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923) [spring-webmvc-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852) [spring-webmvc-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882) [spring-webmvc-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778) [spring-webmvc-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617) [servlet-api-6.0.29.jar:na]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) [servlet-api-6.0.29.jar:na]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) [catalina-6.0.29.jar:6.0.29]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) [catalina-6.0.29.jar:6.0.29]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) [catalina-6.0.29.jar:6.0.29]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) [catalina-6.0.29.jar:6.0.29]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) [catalina-6.0.29.jar:6.0.29]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [catalina-6.0.29.jar:6.0.29]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [catalina-6.0.29.jar:6.0.29]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) [catalina-6.0.29.jar:6.0.29]
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857) [coyote-6.0.29.jar:6.0.29]
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588) [coyote-6.0.29.jar:6.0.29]
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) [coyote-6.0.29.jar:6.0.29]
    at java.lang.Thread.run(Unknown Source) [na:1.7.0_45]
Caused by: java.lang.IllegalStateException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.dts.adminportal.dto.EnumMaster
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1211) ~[hibernate-entitymanager-3.6.3.Final.jar:3.6.3.Final]
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1147) ~[hibernate-entitymanager-3.6.3.Final.jar:3.6.3.Final]
    at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:255) ~[hibernate-entitymanager-3.6.3.Final.jar:3.6.3.Final]
    at org.hibernate.ejb.criteria.CriteriaQueryCompiler$3.getResultList(CriteriaQueryCompiler.java:260) ~[hibernate-entitymanager-3.6.3.Final.jar:3.6.3.Final]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_45]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.7.0_45]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.7.0_45]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.7.0_45]
    at org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:310) ~[spring-orm-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    at com.sun.proxy.$Proxy95.getResultList(Unknown Source) ~[na:na]
    at org.springframework.data.jpa.repository.query.JpaQueryExecution$CollectionExecution.doExecute(JpaQueryExecution.java:77) ~[spring-data-jpa-1.3.0.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:55) ~[spring-data-jpa-1.3.0.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:95) ~[spring-data-jpa-1.3.0.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:85) ~[spring-data-jpa-1.3.0.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:312) ~[spring-data-commons-1.5.0.RELEASE.jar:na]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) ~[spring-aop-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110) ~[spring-tx-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) ~[spring-aop-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155) ~[spring-tx-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    ... 37 common frames omitted
Caused by: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.dts.adminportal.dto.EnumMaster
    at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:243) ~[hibernate-core-3.6.3.Final.jar:3.6.3.Final]
    at org.hibernate.type.EntityType.getIdentifier(EntityType.java:456) ~[hibernate-core-3.6.3.Final.jar:3.6.3.Final]
    at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:130) ~[hibernate-core-3.6.3.Final.jar:3.6.3.Final]
    at org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:67) ~[hibernate-core-3.6.3.Final.jar:3.6.3.Final]
    at org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:571) ~[hibernate-core-3.6.3.Final.jar:3.6.3.Final]
    at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1716) ~[hibernate-core-3.6.3.Final.jar:3.6.3.Final]
    at org.hibernate.loader.Loader.doQuery(Loader.java:801) ~[hibernate-core-3.6.3.Final.jar:3.6.3.Final]
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274) ~[hibernate-core-3.6.3.Final.jar:3.6.3.Final]
    at org.hibernate.loader.Loader.doList(Loader.java:2533) ~[hibernate-core-3.6.3.Final.jar:3.6.3.Final]
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276) ~[hibernate-core-3.6.3.Final.jar:3.6.3.Final]
    at org.hibernate.loader.Loader.list(Loader.java:2271) ~[hibernate-core-3.6.3.Final.jar:3.6.3.Final]
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:452) ~[hibernate-core-3.6.3.Final.jar:3.6.3.Final]
    at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363) ~[hibernate-core-3.6.3.Final.jar:3.6.3.Final]
    at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196) ~[hibernate-core-3.6.3.Final.jar:3.6.3.Final]
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1268) ~[hibernate-core-3.6.3.Final.jar:3.6.3.Final]
    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102) ~[hibernate-core-3.6.3.Final.jar:3.6.3.Final]
    at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:246) ~[hibernate-entitymanager-3.6.3.Final.jar:3.6.3.Final]
    ... 53 common frames omitted

1 个答案:

答案 0 :(得分:0)

你的findByEnumMaster方法需要EnumValueMaster作为它的参数,而不是String(它是id的id)。

这个事实可以在this教程

中看到