apache / apache/grails-core

Unintentional fetch when domain class contains dateCreated property or a closure event

Open
#14,546 0 comments 0 reactions 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

Grails fetches proxied domain class references from database unnecessarily when domain class includes the auto timestamped "dateCreated" field or any closure event method (beforeUpdate, afterUpdate, beforeInsert, etc). They are not fetched because of validation and setting validate to false during save has no effect.

Setting autoTimestamp to false in the domain class mapping and removing any closure event methods removes the problem.

The problem can be reproduced on at least grails 2.4.5, 2.5.5 and 2.5.6. It can not be reproduced on grails 3.3.8.

Tested on

### Task List

- [ x] Steps to reproduce provided
- [ x
[DateCreated2.zip](https://github.com/grails/grails-core/files/2460151/DateCreated2.zip)
] Stacktrace (if present) provided
- [ x] Example that reproduces the problem uploaded to Github
- [ x] Full description of the issue provided (see below)

### Steps to Reproduce

1. Create a domain class hierarchy where one domain (Review) references other domains (Product, Person) via member variables
2. Add a "dateCreated" property in the domain that references others (Review).
3. Add "beforeInsert", "afterUpdate" or any other closure event method in the domain that references others.
4. Create a new instance of the domain that references others (Review)
5. Set referenced domains by getting a hibernate proxy with the load() method.
6. Save the created domain instance.

### Expected Behaviour

Database is accessed only once to insert the newly created domain (Review). Other domains (Product, Person) referenced via hibernate proxies are not fetched.

### Actual Behaviour

All referenced domains are fetched with separate trips to the database.

### Environment Information

- **Operating System**:
Macos 10.13.6
- **Grails Version:**
2.5.6
- **JDK Version:**
1.7.0_80
- **Container Version (If Applicable):**

### Example Application

- TODO: link to github repository with example that reproduces the issue

Contributor guide

Open the contributing guide

Research direction

Start with the linked DateCreated2.zip reproduction and follow the listed steps using the affected Grails versions. Compare database access when dateCreated or closure event methods are present; done means saving Review performs only its insert without fetching the referenced Product or Person proxies.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy
Domain
backend, databases
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.