apache / apache/grails-core

'embedded' class definition in same class as containing definition - creates table in DB as well as columns on master table

Open
#14,626 8 comments 1 reaction 0 assignees View on GitHub
relates-to: gorm
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

i had a problem i posted on stackoverflow relating to the documentation and what was happening - see http://stackoverflow.com/questions/42229853/grails-v3-2-6-gorm-embedded-class-in-same-domain-class-groovy-creates-dummy-db/42322967#42322967

in essence using grails 3.2.5 and now 3.2.6, if you create a domain class and in the same file create a embedded reference to 'subClass' you want to embed, then gorm is creating 'both' tables (parent, and embedded ) in DB and this led to set of tests that didnt work as expected.

when i dug in an looked its because if you save the embedded ref before the parent it goes in its own table and is then saved again in the embedded columns in the parent table.

I managed (i think ) to fix this by having to declare the embedded class as static sub class of the parent and then you only get the single DB table. however this is not what the documentation says.

so the new approach shown below ensures that only one table is created. The previous code had the GeoLocation class sitting below the venue in the same groovy file - but that generates two tables.

Either the documentation in section 5.2 is wrong /unclear, or the code isnt doing what the docs say.

I hope that's clear. If you need to see the code tree on Git its here

[coffeeShopApp](https://github.com/woodmawa/coffeeShopApp)

```
class Venue implements Serializable {

String name
LocalDate dateCreated
LocalDate lastVisited
LocalDate lastUpdated
GeoAddress location
Collection posts

//static hasOne = [location:GeoAddress] //, temp:TempLocation
static hasMany = [posts:Post] //but doesn't really own thats with user
static embedded =['location']

static constraints = {
lastVisited nullable:true
location nullable:true, unique:true
posts nullable:true
}
static mapping = {
location cascade: "all-delete-orphan", lazy:false, unique:true //eager fetch strategy
posts sorted: "desc", cascade:"save-update"

//comment out for now
//posts joinTable: [name:"venue_posts", key:"venue_id", column:"posts", type:Post]
}

static class GeoAddress {

String addressLine1
String addressLine2
String addressLine3
String town
String county
String country = "UK"
String postcode

//adds addTo/removeFrom methods to venue
static belongsTo = Venue

static constraints = {
addressLine1 nullable:true
addressLine2 nullable:true
addressLine3 nullable:true
town nullable:true
county nullable:true
country nullable:true
postcode nullable:true
}
}
}
```

Contributor guide

Open the contributing guide

Research direction

Start with documentation section 5.2 and reproduce the reported behavior in the linked coffeeShopApp, focusing on the Venue and nested GeoAddress classes. Compare Grails 3.2.5 and 3.2.6 to determine whether the documented embedded-class form or the implementation is wrong; done means the behavior and documentation agree and the resulting table count is verified.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.