smeeb
smeeb

Reputation: 29517

Hibernate/JPA @AttributeOverrides not playing nicely with Groovy

Please note: full SSCCE and reproducible source code is here on my GitHub repo. Like the README says, just clone and run ./gradlew clean build to reproduce the error that I'm seeing.


I'm designing the data model for a Groovy (not Java) Spring Boot app that will use Hibernate/JPA to read/write entities to/from MySQL. All of my entities will extend an abstract BaseEntity which provides a PK as well as another identifier ("refId"). For instance:

@Canonical
@Entity
@MappedSuperclass
abstract class BaseEntity {
  @Id
  Long id

  String refId
}

@Canonical
@Entity
@AttributeOverrides({
    @AttributeOverride(name = "id", column=@Column(name="customer_id")),
    @AttributeOverride(name = "refId", column=@Column(name="customer_ref_id"))
})
class Customer extends BaseEntity {
  @Column(name = "customer_name")
  String name

  @Column(name = "customer_fav_food")
  String favoriteFood
}

As you can see, because each entity extends BaseEntity, and because I want each subclass/entity/table to have its own column name for the id and refId fields, I need to use that AttributeOverrides declaration in each subclass.

However AttributeOverrides is causing a compiler issue that I can't reproduce in plain ole' Java. At compile-time it complains with an unexpected token error at n=@Column(name="customer_id")),.

Can anyone reproduce and figure out what's going on (and what the fix is)?

Upvotes: 2

Views: 163

Answers (1)

dic19
dic19

Reputation: 17971

However AttributeOverrides is causing a compiler issue that I can't reproduce in plain ole' Java.

Unlike Java's compiler, Groovy's compiler takes { ... } as a Closure instead of an Array. You should use [ ... ] instead, just like this:

@Canonical
@Entity
@AttributeOverrides([
    @AttributeOverride(name = "id", column=@Column(name="customer_id")),
    @AttributeOverride(name = "refId", column=@Column(name="customer_ref_id"))
])
class Customer extends BaseEntity {
    // ...    
}

Upvotes: 2

Related Questions