Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[205][Java] Force usage of FetchType LAZY for collections on JPA entities #325

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Added

- [#315](https://github.com/green-code-initiative/ecoCode/pull/315) Add rule EC530 for javascript
- [#48](https://github.com/green-code-initiative/ecoCode-java/pull/48) Add Java rule EC205 (Avoid using a FetchType other than FetchType.LAZY)

### Changed

Expand Down
2 changes: 1 addition & 1 deletion RULES.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ Some are applicable for different technologies.

| Rule key | Name | Description | Reference/Validation | Java | Php | JS | Python | Rust | C# | HTML |
|----------|---------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------|-----|----|--------|------|----|------|
| CRJVM205 | Force usage of FetchType LAZY for collections on JPA entities | | | 🚧 | ❓ | ❓ | ❓ | ❓ | ❓ | 🚫 |
| CRJVM206 | Avoid N+1 selects problem | | | 🚧 | ❓ | ❓ | ❓ | ❓ | ❓ | 🚫 |
| CRJVM207 | Customer data must have end-of-life information | | | 🚧 | ❓ | ❓ | ❓ | ❓ | ❓ | ❓ |
| CRJVM??? | Persistence Java : Avoid Joints on non indexed columns | | | 🚧 | ❓ | ❓ | ❓ | ❓ | ❓ | 🚫 |
Expand Down Expand Up @@ -67,6 +66,7 @@ Some are applicable for different technologies.
| EC87 | Use collection indexer | Collection indexers should be used instead of Linq, when available | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | ✅ | 🚫 |
| EC88 | Dispose resource asynchronously | Resources that implement `IAsyncDisposable` should be disposed asynchronously | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | ✅ | 🚫 |
| EC203 | Detect unoptimized file formats | When it is possible, to use svg format image over other image format | | 🚀 | 🚀 | 🚀 | ✅ | 🚀 | 🚀 | 🚫 |
| EC205 | Force usage of FetchType LAZY for collections on JPA entities | Using FetchType.EAGER can lead to performance issues and should be avoided. Always use FetchType.LAZY instead. | | ✅ | ❓ | ❓ | ❓ | ❓ | ❓ | 🚫 |
| EC404 | Avoid list comprehension in iterations | Use generator comprehension instead of list comprehension in for loop declaration | | 🚫 | 🚫 | 🚫 | ✅ | 🚫 | 🚫 | 🚫 |
| | Use official social media sharing buttons | These JavaScript plugins are very resource-intensive: to work, they require a large number of requests and download heavy files. It is better to prefer direct links. | [cnumr best practices (3rd edition) BP_019](https://github.com/cnumr/best-practices/blob/main/chapters/BP_019_fr.md) | 🚫 | 🚫 | 🚀 | 🚫 | 🚫 | 🚫 | 🚀 |
| | Non-grouped similar CSS declarations | When multiple Document Object Model (DOM) elements have common CSS properties, declare them together in the same style sheet. This method reduces the weight of CSS. | [cnumr best practices (3rd edition) BP_025](https://github.com/cnumr/best-practices/blob/main/chapters/BP_025_fr.md) | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | 🚫 | 🚫 |
Expand Down
19 changes: 19 additions & 0 deletions ecocode-rules-specifications/src/main/rules/EC205/EC205.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"title": "Avoid using a FetchType other than FetchType.LAZY",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"jpa",
"fetch",
"lazy",
"eco-design",
"performance",
"memory",
"ecocode"
],
"defaultSeverity": "Minor"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#Avoid using a FetchType other than FetchType.LAZY
Using FetchType.EAGER can lead to performance issues and should be avoided. Always use FetchType.LAZY instead.

## Noncompliant Code Example

```java
@OneToMany(fetch = FetchType.EAGER, mappedBy = "firstEntities_id")
@Column(name = "firstEntities")
private Collection<SomeEntity> firstEntities;
```

```java
@OneToMany
@Column(name = "otherEntity_id")
private Collection<SomeEntity> otherEntities;
```
```java
@ManyToMany
@Column(name = "otherEntity_id")
private Collection<SomeEntity> otherEntities;
```

## Compliant Solution

```java
@OneToMany(fetch = FetchType.LAZY, mappedBy = "otherEntity_id")
@Column(name = "otherEntity_id")
private Collection<SomeEntity> otherEntities;
```

```java
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "otherEntity_id")
@Column(name = "otherEntity_id")
private Collection<SomeEntity> otherEntities;
```
Loading