Grails: domain class mapping (collection of hibernate user types)

后端 未结 1 1681
野趣味
野趣味 2021-01-23 05:48

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.contri

相关标签:
1条回答
  • 2021-01-23 06:40

    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):

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

    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:

    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

    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.

    0 讨论(0)
提交回复
热议问题