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.