0

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)

XDR
  • 4,070
  • 3
  • 30
  • 54
  • What kind of embedded objects? Classes from `src/groovy`? Do you want these objects persisted to a database or are they transients? – tylerwal Jan 30 '16 at 22:51
  • Classes from `src/groovy` that I want persisted, but that don't need to be separate entities, and that I will also want to use as normal embedded object properties; e.g., for embedded object class `EmbeddedObject`, sometimes I will need a property of type `EmbeddedObject`, and sometimes a property of type `List`. I can't get the latter to work. – XDR Jan 30 '16 at 23:56
  • Whoever down voted this question, please explain why, and maybe I can improve the question. In case this was because I didn't specify whether I wanted the embedded objects persisted or transient: I didn't mention which because I thought that embedded objects are always persisted (i.e. there is no such thing as a transient embedded object; it would just be a transient). – XDR Jan 31 '16 at 00:01

1 Answers1

1

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.

tylerwal
  • 1,858
  • 2
  • 15
  • 20
  • Thanks for all the investigation. I won't mark your post as the answer just yet, though, in case someone else knows how to do this in GORM, though, from the JIRA ticket, it looks like it's impossible in GORM. The reason why I want `@ElementCollection` is because I want to use one class both as a normal embedded object, but sometimes I need to have it in a List property instead of as a normal single-value property. I can't do that without `@ElementCollection`. Also, sometimes, certain data should not be accessible by itself: the referencing class might have code to keep it consistent. – XDR Jan 31 '16 at 11:07