gpt4 book ai didi

hibernate - Grails:域类映射( hibernate 用户类型的集合)

转载 作者:行者123 更新时间:2023-12-02 13:59:20 25 4
gpt4 key购买 nike

我想知道是否可以实现以下域模型。

让我们有一个域类,其中包含一组间隔(joda 时间)。我可以使用 org.joda.time.contrib.hibernate.PersistentInterval hibernate 用户类型将 Interval 映射到数据库表(通过与 http://www.grails.org/JodaTime+Plugin 类似的方式)。但是,如果我有一组间隔,而不仅仅是一个间隔,我就无法弄清楚如何实现映射。

例子:

class Activity {  
...
Set intervals = []
...
static hasMany = [
intervals: org.joda.time.Interval
]

// This is incorrect implementation, I have set of intervals
// and this would be correct if I had only one interval
// How to implement mapping in this case?
static mapping = {
intervals type: PersistentInterval, {
column name: "start"
column name: "end"
}
}

}



上面的实现失败并出现以下错误:

2010-10-23 18:30:25,483 [main] ERROR context.GrailsContextLoader - Error executing bootstraps: Error creating bean with name 'messageSource': 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.MappingException: Foreign key (FK4FDC5B1E5107CA0:activity_intervals [start,end])) must have same number of columns as the referenced primary key (activity [id]) org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'messageSource': 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.MappingException: Foreign key (FK4FDC5B1E5107CA0:activity_intervals [start,end])) must have same number of columns as the referenced primary key (activity [id]) at org.grails.tomcat.TomcatServer.start(TomcatServer.groovy:212)



我认为解决此问题的方法是提取 Interval 以分离扩展 Interval 的域类并在其中指定映射。但是,Interval 是最终类,因此无法扩展。

谢谢你的建议。

最佳答案

我正在回答我自己的问题,也许这个答案对某人有用。

到目前为止,我只找到了一种实现给定模型的方法 - 来自 Hibernate XML mapping files :

<hibernate-mapping package="mappingtest">  
<class name="Activity">
<id name="id">
<generator class="native"/>
</id>
<set name="intervals">
<key column="activity_id" not-null="true"/>
<element type="org.joda.time.contrib.hibernate.PersistentInterval">
<column name="startDate"/>
<column name="endDate"/>
</element>
</set>
</class>
</hibernate-mapping>

和域类实现:
class Activity {    
Long id
Set intervals = []

static constraints = {
}
}

我还必须将域类从 grails-app/domain 移动到 src/groovy 目录,否则应用程序运行失败(grails-1.3.5):

...
org.hibernate.DuplicateMappingException: Duplicate class/entity mapping mappingtest.Activity
...



我发现上述实现的第二个问题是,当我通过以下方式打开脚手架(用于测试目的)时:
class ActivityController {
static scaffold = true
...
}

显示创建的事件失败并出现错误:

Exception Message: No such property: id for class: org.joda.time.Interval Possible solutions: end Caused by: Error evaluating expression [i.id] on line [38]: No such property: id for class: org.joda.time.Interval Possible solutions: end



但是手动实现从 DB 获取事件及其显示工作。

编辑:另外我找到了脚手架和 DuplicateMappingException 问题的解决方案。它们是由 Activity.hbm.xml 的无效位置引起的 - 缺少包目录结构。正确的位置是 grails-app/conf/hibernate/mappingtest/Activity.hbm.xml。

关于hibernate - Grails:域类映射( hibernate 用户类型的集合),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4012853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com