Grails 2.5.3 domain class with List of embedded objects

772 Views Asked by At

In a Grails 2.5.3 domain class with, how can I create a List property of embedded objects (not domain objects, and not basic types like String, Integer, etc.)?

I've tried many combinations of static embedded, static hasMany, etc., but none seem to work.

Most of the time, I get the following exception when running run-app:

org.hibernate.MappingException

Missing type or column

for column[list_property_embedded_class]

on domain[DomainClass]

referencing[EmbeddedClassFQCN]

Where the values in the [] are replaced by my actual class / column names.

(I re-spaced the exception message to make it more readable; it's actually a one-liner)

1

There are 1 best solutions below

1
On

If you resort to using traditional hibernate rather than GORM, then what you are looking for would be the @ElementCollection annotation. From Hibernate's examples:

@Entity
public class User {
   [...]
   public String getLastname() { ...}

   @ElementCollection
   @CollectionTable(name="Addresses", joinColumns=@JoinColumn(name="user_id"))
   @AttributeOverrides({
      @AttributeOverride(name="street1", column=@Column(name="fld_street"))
   })
   public Set<Address> getAddresses() { ... } 
}

@Embeddable
public class Address {
   public String getStreet1() {...}
   [...]
}

I tried created a test project with a single domain class and a list of a class from src/groovy and wasn't successful. I tried different variations of applying annotations from just @ElementCollection to everything in their example without successful. My assumption is that Hibernate annotations simply don't work with Grails domain classes; this is further backed by Grails documentation that has annotations being applied to a POJO in src/java. To me, I don't see that as a worthwhile option.

As to having a GORM equivalent of @ElementCollection, there is an unresolved JIRA ticket open within Grails for that exact feature: GRAILS-10095.

I question the need or benefit of an embedded object collection; unlike a traditional embedded object (static embedded that is supported) that results in a truly embedded with additional columns within the domain class's SQL table, an embedded collection would involve having another table. The people working on Hibernate are smarter than me, so I'm sure there is a perfectly good reason.