问题描述
让我们有一个包含一组间隔(joda时间)的域类。我可以使用org.joda.time.contrib.hibernate.PersistentInterval hibernate用户类型来将Interval映射到数据库表(通过类似于)。然而,我不知道如何实现映射,如果我有一组间隔,而不是一个间隔。
示例:
上面的实现失败,出现以下错误:
我认为解决此问题的方法是提取Interval以分隔扩展Interval的域类并指定其中的映射。但是,Interval是最后一堂课,所以不可能延续。
感谢您的建议。
我回复我自己的问题,也许这个答案对别人有用。 b $ b
到现在为止,我只找到一种方法来实现给定的模型 - 通过:
< hibernate-mapping package =mappingtest>
< class name =Activity>
< id name =id>
< generator class =native/>
< / id>
< set name =intervals>
< key column =activity_idnot-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 = {
}
}
我还必须将domain class从grails-app / domain移动到src / groovy目录,否则应用程序运行失败使用(grails-1.3.5):
上述第二个问题实现我发现当我通过以下方式打开脚手架(用于测试目的)时:
class ActivityController {
static scaffold = true
...
}
显示创建的活动失败错误:
但手动实现从DB和它的显示工作。
编辑:另外我找到了脚手架和DuplicateMappingException问题的解决方案。它们是由Activity.hbm.xml的无效位置导致的 - 程序包目录结构丢失。正确的位置是grails-app / conf / hibernate / mappingtest / Activity.hbm.xml。
I am wondering if is possible to implement following domain model.
Let's have a domain class which contains set of intervals (joda time). I can use org.joda.time.contrib.hibernate.PersistentInterval hibernate user type for mapping of Interval to database table (by similar way as in http://www.grails.org/JodaTime+Plugin). However I cannot figure out how to implement mapping if I have set of intervals, not only one interval.
Example:
Implementation above fails with following error:
I thought that work-around of this issue is to extract Interval to separate domain class extending Interval and specify mapping within it. However, Interval is final class so extending is not possible.
Thanks for your advices.
I am replying my own question, maybe this answer will be useful for someone.
Until now I have found only one way how to implement given model - by 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>
and domain class implementation:
class Activity {
Long id
Set intervals = []
static constraints = {
}
}
I also had to move domain class from grails-app/domain to src/groovy directory, otherwise application running failed with (grails-1.3.5):
Second problem with above implementation I have discovered is that when I turned on scaffolding (for testing purpose) by:
class ActivityController {
static scaffold = true
...
}
showing of created activity failed with error:
but manual implementation of getting activities from DB and its showing worked.
Edit: additionally I found solution of scaffolding and DuplicateMappingException issues. They were caused by invalid location of Activity.hbm.xml - package directory structure was missing. Correct location is grails-app/conf/hibernate/mappingtest/Activity.hbm.xml.
这篇关于Grails:域类映射(收集休眠用户类型)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!