我在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 = {
}
}
我的问题是,我得到了异常&#34; IndexOutOfBoundsException&#34;使用detailMessage&#34;索引:1,大小0&#34;和suppressExceptions&#34;集合$ UnmodifiableRandomAccessList&#34;具有价值&#34;尺寸= 0&#34;当我在服务中运行此代码但没有异常,当我在控制器中运行此代码时。异常来自命令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>()
然后我回到第一个异常&#34; IndexOutOfBoundsException&#34;。我很疑惑,为什么没有问题,这个代码在控制器中。在这种情况下,服务和控制器之间的区别是什么?
答案 0 :(得分:0)
尝试使用交易服务方法:
import grails.transaction.Transactional
class LibraryService {
@Transactional
public LibraryItem createLibraryItemWithValues(ProjectItem projectItem) {
def libraryitem = new LibraryItem()
libraryitem.name = projectItem.name
libraryitem.itemtype = projectItem.itemtype.itemtype
libraryitem.description = projectItem.description
projectItem.categoryvalues*.librarycategoryvalue.each {
it.addToLibraryitems(libraryitem)
}
return libraryitem.save()
}
}
由于您要创建新的LibraryItem
,因此在您保存之前,它的ID始终为空。在大多数情况下,您不需要使用attach()
,因此我删除了所有代码。