apache / apache/grails-core

Double saved

Open
#14,564 0 comments 0 reactions 0 assignees View on GitHub
relates-to: grails-data-neo4j
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

I want to save an object, but first I need to get its `ID` to update another property of the same object. I am doing this:

```
if (instance.validate()) {
instance.save() // now I have an id
instance.code = String.format("%014d", instance.id)
instance.save()
}
```

The domain object is
```
class Product {

final static String DEFAULT_CODE = "XXXX-XXXX-XXXX"

String code = DEFAULT_CODE
String description

static constraints = {
code size: 14..14
}
}
```

But when I get the object from the data base, the `code` property doesn't have the new value. I created an integration test to show this:

```
void "test double saved"() {
given:
RestBuilder rest = new RestBuilder()

when: "save a new product"
RestResponse response = rest.post("http://localhost:${serverPort}/product") {
json([
description: "screw"
])
}

then: "the code will be different to default value"
response.status == 201
Product product = Product.getAll().first()
product
product.code != Product.DEFAULT_CODE

cleanup:
Product.withNewSession {
Product.withNewTransaction {
Product.getAll()*.delete()
}
}
}
```
The second call to save() is ignored.

Here is the sample app: https://github.com/AmaliaMV/neo4j-doubleSaved

The versions I am using are:

```
grailsVersion=3.3.5
gormVersion=6.1.10.BUILD-SNAPSHOT
grailsNeo4jPluginVersion=6.2.0.BUILD-SNAPSHOT
```

However, I checked that this is happening too if I use hibernate instead of neo4j.

Contributor guide

Open the contributing guide

Research direction

Start with the sample app and the integration test shown in the issue, reproducing the POST to /product and inspecting Product.getAll().first(). Trace the two save calls in the Grails/GORM persistence path, then verify that the generated code is persisted when using both the reported Neo4j and Hibernate configurations.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy, neo4j
Domain
backend, database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.