Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server Grails,检索记录时强制转换错误_Sql Server_Database_Grails_Data Retrieval - Fatal编程技术网

Sql server Grails,检索记录时强制转换错误

Sql server Grails,检索记录时强制转换错误,sql-server,database,grails,data-retrieval,Sql Server,Database,Grails,Data Retrieval,我创建了一个新域并手动创建了表。 该表应仅包含一条记录,因此唯一的记录id为(1) 如果该记录不存在,则应自动创建该记录,但在尝试检索该记录时,我总是会遇到一个强制转换错误。不管我是否先手动创建记录,get、find或其他任何东西都会返回错误 失败的代码: def int id = 1 def WtStatus wts = WtStatus.get(id)?:new WtStatus(id:1).save(failOnError:true) 域: class WtStatus

我创建了一个新域并手动创建了表。 该表应仅包含一条记录,因此唯一的记录id为(1)

如果该记录不存在,则应自动创建该记录,但在尝试检索该记录时,我总是会遇到一个强制转换错误。不管我是否先手动创建记录,get、find或其他任何东西都会返回错误

失败的代码:

    def int id = 1
    def WtStatus wts = WtStatus.get(id)?:new WtStatus(id:1).save(failOnError:true)
域:

class WtStatus {
  def prodBufferService
  int     weekUpdated
  Date    dateUpdated

  def beforeInsert() {
      dateUpdated = new Date()
      weekUpdated = prodBufferService.getCurrentYearWeek()
  }
  def beforeUpdate() {
      dateUpdated = new Date()
  }
  static constraints = {
      dateUpdated     nullable:true
      weekUpdated     nullable:true
  }

  static mapping = {
      table   'wt_status'
      version true
      id              column: "id",              type:        'int'
  }
}

表的创建脚本:

CREATE TABLE [dbo].[wt_status](
    [id] [int] NOT NULL,
    [version] [bigint] NOT NULL,
    [date_updated] [datetime2](7) NULL,
    [week_updated] [int] NULL,
 CONSTRAINT [PK__wt_statu__3213E83F77809FC6] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
堆栈跟踪:

java.lang.Long cannot be cast to java.lang.Integer. Stacktrace follows:

java.lang.reflect.InvocationTargetException: null
    at org.grails.core.DefaultGrailsControllerClass$ReflectionInvoker.invoke(DefaultGrailsControllerClass.java:210)
    at org.grails.core.DefaultGrailsControllerClass.invoke(DefaultGrailsControllerClass.java:187)
    at org.grails.web.mapping.mvc.UrlMappingsInfoHandlerAdapter.handle(UrlMappingsInfoHandlerAdapter.groovy:90)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    at org.springframework.boot.web.filter.ApplicationContextHeaderFilter.doFilterInternal(ApplicationContextHeaderFilter.java:55)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:317)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:115)
    at grails.plugin.springsecurity.web.UpdateRequestContextHolderExceptionTranslationFilter.doFilter(UpdateRequestContextHolderExceptionTranslationFilter.groovy:64)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter.doFilter(GrailsAnonymousAuthenticationFilter.groovy:53)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:158)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:169)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter.doFilter(MutableLogoutFilter.groovy:62)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at grails.plugin.springsecurity.web.SecurityRequestHolderFilter.doFilter(SecurityRequestHolderFilter.groovy:58)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177)
    at org.grails.web.servlet.mvc.GrailsWebRequestFilter.doFilterInternal(GrailsWebRequestFilter.java:77)
    at org.grails.web.filters.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:67)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
    at org.hibernate.type.descriptor.java.IntegerTypeDescriptor.unwrap(IntegerTypeDescriptor.java:19)
    at org.hibernate.type.descriptor.sql.IntegerTypeDescriptor$1.doBind(IntegerTypeDescriptor.java:46)
    at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:74)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:253)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:248)
    at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:2051)
    at org.hibernate.loader.Loader.bindParameterValues(Loader.java:2020)
    at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1950)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1903)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1881)
    at org.hibernate.loader.Loader.doQuery(Loader.java:925)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:342)
    at org.hibernate.loader.Loader.doList(Loader.java:2622)
    at org.hibernate.loader.Loader.doList(Loader.java:2605)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2434)
    at org.hibernate.loader.Loader.list(Loader.java:2429)
    at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:109)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1787)
    at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:363)
    at org.hibernate.internal.CriteriaImpl.uniqueResult(CriteriaImpl.java:385)
    at org.grails.orm.hibernate.AbstractHibernateGormStaticApi$_get_closure1.doCall(AbstractHibernateGormStaticApi.groovy:79)
    at org.grails.orm.hibernate.GrailsHibernateTemplate.doExecute(GrailsHibernateTemplate.java:243)
    at org.grails.orm.hibernate.GrailsHibernateTemplate.execute(GrailsHibernateTemplate.java:187)
    at org.grails.orm.hibernate.GrailsHibernateTemplate.execute(GrailsHibernateTemplate.java:110)
    at org.grails.orm.hibernate.AbstractHibernateGormStaticApi.get(AbstractHibernateGormStaticApi.groovy:75)
    at org.grails.datastore.gorm.GormEntity$Trait$Helper.get(GormEntity.groovy:518)
    at com.buffer.OrdersAndStoreController$$EQIjps9c.list(OrdersAndStoreController.groovy:29)
    ... 37 common frames omitted

看起来太复杂了。我想你真正想要的是:

WtStatus wts = WtStatus.findOrSaveById( id )
依照


此外,还应明确定义
id
字段为
Integer
类型。否则,GORM默认情况下会将这些代码视为长代码。

看起来太复杂了。我想你真正想要的是:

WtStatus wts = WtStatus.findOrSaveById( id )
依照


此外,还应明确定义
id
字段为
Integer
类型。否则,GORM默认情况下将这些变量视为
Long

问题在于您使用的
ìd
变量的类型,即
int
。由于您没有在域中声明
id
属性,因此它将是一个
Long
(Grails默认值)。注意:在映射块中,您只定义与数据库的关系,这就是为什么属性在域中仍然是
Long

此外,如果要对变量使用另一种类型,则不需要使用
def
。试试这个:

Long id = 1
WtStatus wts = WtStatus.get(id)?:new WtStatus(id:1).save(failOnError:true)

问题在于您使用的
ìd
变量的类型,即
int
。由于您没有在域中声明
id
属性,因此它将是一个
Long
(Grails默认值)。注意:在映射块中,您只定义与数据库的关系,这就是为什么属性在域中仍然是
Long

此外,如果要对变量使用另一种类型,则不需要使用
def
。试试这个:

Long id = 1
WtStatus wts = WtStatus.get(id)?:new WtStatus(id:1).save(failOnError:true)

我完全照你说的做了,但我也犯了同样的错误。见我上面的修正案:见我上面的评论我是如何修正的。你的代码片段非常好。从现在开始我就用它了。我完全按照你说的做了,但我也犯了同样的错误。见我上面的修正案:见我上面的评论我是如何修正的。你的代码片段非常好。我会从现在开始使用它。我注意到你说的关于声明id的内容,所以:因为你的解决方案不起作用,我仍然得到相同的错误,我将id声明为int,整个过程开始起作用。我注意到你说的关于声明id的内容,所以:因为你的解决方案不起作用,我仍然得到相同的错误,我将id声明为int,整个过程开始工作。