【发布时间】:2016-03-18 13:29:15
【问题描述】:
我在 Grails 中有以下代码:
public LibraryItem createLibraryItemWithValues(ProjectItem projectItem) {
def libraryitem = new LibraryItem ()
libraryitem.name = projectItem.name
libraryitem.itemtype = projectItem.itemtype.itemtype
libraryitem.description = projectItem.description
List<LibraryCategoryValue> libCatValues = new ArrayList<LibraryCategoryValue>();
for (def pcv : projectItem.categoryvalues) {
libCatValues.add(pcv.librarycategoryvalue);
}
if (libraryitem.id != null && !libraryitem.isAttached()) {
libraryitem.attach()
}
libCatValues.each{
it.addToLibraryitems(libraryitem)
}
return libraryitem.save()
}
类 LibraryCategoryValue
package ch.fhnw.accelerom.project.library
import ch.fhnw.accelerom.project.ItemType
import ch.fhnw.accelerom.project.TranslatableObject
class LibraryCategoryValue extends TranslatableObject {
SortedSet<LibraryItem> libraryitems
static belongsTo = [librarycategory:LibraryCategory]
static hasMany = [libraryitems: LibraryItem]
static constraints = {
}
}
我的问题是,当我在服务中运行此代码时,我得到了异常“IndexOutOfBoundsException”,detailMessage“Index:1,Size 0”和被抑制的异常“Collections$UnmodifiableRandomAccessList”,值为“size = 0”也不例外,当我在控制器中运行此代码时。异常来自命令 it.addToLibraryitems(libraryitem),respektive,当我评论这一行时,没有发生异常。谁能给我一个提示,问题可能是什么?
提前致谢。
编辑:
我现在用
初始化了类中的库项SortedSet<LibraryItem> libraryitems = []
但现在启动应用程序时出现以下错误
ERROR context.GrailsContextLoader - Error initializing the application: Error creating bean with name 'transactionManagerPostProcessor': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.hibernate.InstantiationException: could not instantiate test objectch.fhnw.accelerom.project.library.LibraryCategoryValue
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManagerPostProcessor': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.hibernate.InstantiationException: could not instantiate test objectch.fhnw.accelerom.project.library.LibraryCategoryValue
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.hibernate.InstantiationException: could not instantiate test objectch.fhnw.accelerom.project.library.LibraryCategoryValue
... 4 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.hibernate.InstantiationException: could not instantiate test objectch.fhnw.accelerom.project.library.LibraryCategoryValue
... 4 more
Caused by: org.hibernate.InstantiationException: could not instantiate test objectch.fhnw.accelerom.project.library.LibraryCategoryValue
... 4 more
Caused by: java.lang.reflect.InvocationTargetException
... 4 more
Caused by: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '[]' with class 'java.util.ArrayList' to class 'java.util.SortedSet' due to: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: java.util.SortedSet()
at ch.fhnw.accelerom.project.library.LibraryCategoryValue.<init>(LibraryCategoryValue.groovy:7)
... 4 more
编辑 2:
我还尝试使用
初始化库项SortedSet<LibraryItem> libraryitems = new TreeSet<LibraryItem>()
但是我又回到了第一个异常“IndexOutOfBoundsException”。我很想知道,当这段代码在控制器中时,为什么没有问题。在这种情况下,服务和控制器有什么区别?
【问题讨论】:
-
显示域名
LibraryCategoryValue的代码。此外,您在libCatValues.each{...}之前尝试过libraryitem.save()吗? -
libraryitem.save() 在 libCatValues.each{...} 之前没有任何区别。
-
您使用的是哪个版本的 Grails?
-
我使用的是 Grails 2.3.7
-
我建议用
[] as SortedSet初始化libraryitems。不确定这是否会导致您的问题,但可能是null的集合总是很危险
标签: grails indexoutofboundsexception