Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/maven/org.springframework.cloud-s…
Browse files Browse the repository at this point in the history
…pring-cloud-dependencies-2024.0.0
  • Loading branch information
alexandrepa authored Jan 8, 2025
2 parents 4a9f885 + d757e8f commit af43af4
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 73 deletions.
12 changes: 6 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@
<java.version>17</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<lombok.version>1.18.34</lombok.version>
<testcontainer.version>1.20.3</testcontainer.version>
<lombok.version>1.18.36</lombok.version>
<testcontainer.version>1.20.4</testcontainer.version>
<mockserver.version>5.15.0</mockserver.version>
<cucumber.version>7.20.1</cucumber.version>
<commonstext.version>1.12.0</commonstext.version>
<commonstext.version>1.13.0</commonstext.version>
<zookeeper.version>3.9.3</zookeeper.version>
<sonar.organization>decathlon</sonar.organization>
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
Expand Down Expand Up @@ -72,7 +72,7 @@
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.11.3</version>
<version>5.11.4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand All @@ -87,7 +87,7 @@
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.3.5</version>
<version>3.4.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down Expand Up @@ -225,7 +225,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.10.1</version>
<version>3.11.2</version>
<configuration>
<source>17</source>
</configuration>
Expand Down
4 changes: 2 additions & 2 deletions tzatziki-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.11.3</version>
<version>5.11.4</version>
<exclusions>
<exclusion>
<groupId>org.apiguardian</groupId>
Expand Down Expand Up @@ -116,7 +116,7 @@
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>33.3.1-jre</version>
<version>33.4.0-jre</version>
<exclusions>
<exclusion>
<groupId>org.checkerframework</groupId>
Expand Down
2 changes: 1 addition & 1 deletion tzatziki-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.17.0</version>
<version>2.18.0</version>
</dependency>
<dependency>
<groupId>com.decathlon.tzatziki</groupId>
Expand Down
4 changes: 2 additions & 2 deletions tzatziki-jackson/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>33.3.1-jre</version>
<version>33.4.0-jre</version>
<exclusions>
<exclusion>
<groupId>org.checkerframework</groupId>
Expand All @@ -55,7 +55,7 @@
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.11.3</version>
<version>5.11.4</version>
<exclusions>
<exclusion>
<groupId>org.apiguardian</groupId>
Expand Down
128 changes: 120 additions & 8 deletions tzatziki-spring-jpa/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,18 @@ public class User {
You can now insert rows with:
```gherkin
Given that the users table will contain:
| id | firstName | lastName |
| 1 | Darth | Vader |
| firstName | lastName |
| Darth | Vader |
# or alternatively
Given that the User entities will contain:
"""
- id: 1
firstName: Darth
- firstName: Darth
lastName: Vader
"""
# single rows work as well
Given that the users table will contain:
"""
id: 1
firstName: Darth
lastName: Vader
"""
Expand All @@ -120,7 +118,6 @@ Given that the UserRepository repository will contain:
"""
[
{
"id": 1,
"firstName": "Darth",
"lastName": "Vader"
}
Expand All @@ -131,8 +128,123 @@ Given that the UserRepository repository will contain:
Adding `only` to the step will also empty the table before inserting the data:
```gherkin
But when the users table will contain only:
| id | firstName | lastName |
| 1 | Han | Solo |
| firstName | lastName |
| Han | Solo |
```

### Important Note on Generated IDs

Starting with Hibernate 6.6+ and Spring Boot 3.4+, you can no longer manually specify values for IDs that are generated by the framework.

#### Example
Suppose you have an entity mapped as follows:

```java
@Getter
@Entity
@NoArgsConstructor
@Table(name = "users")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

@Column(name = "first_name")
private String firstName;
}
```

In this example, Hibernate **will not allow you** to insert data with explicit `id` values like this:

```gherkin
Given the users table will contain:
| id | firstName | lastName |
| 42 | Darth | Vader |
```

The test above fails with the following exception:

```
org.springframework.orm.ObjectOptimisticLockingFailureException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)
```
This exception occurs because Hibernate expects the database to assign the primary key when `GenerationType.IDENTITY` is used.
Providing an explicit `id` value conflicts with Hibernate’s assumptions, leading to a failure.

#### Correct Approach

To avoid this issue, ensure that the input data does not include the `id` column, as shown below:

```gherkin
Given the users table will contain:
| firstName | lastName |
| Darth | Vader |
```

### Inserting Data into Parent and Child Tables
When working with parent and child tables where the child table has a foreign key referencing the parent table, you may need to ensure correct key mapping during data insertion.

When the primary key of the parent table is auto-generated, you may not know its exact value at the time of insertion into the child table.

However, since the primary key values in the parent table follow a predictable sequence starting at 1, you can infer the foreign key values.

This behavior is consistent because Tzatziki resets the sequence at the beginning of each scenario.

The following code demonstrates an example where the `User` entity references the `Group` entity:

```java
@NoArgsConstructor
@Getter
@Entity
@Table(name = "groups")
public class Group {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Integer id;

@Column(name = "name")
String name;

@OneToMany(mappedBy = "group")
List<User> users;
}
```
```java
@NoArgsConstructor
@Getter
@Entity
@Table(name = "users")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Integer id;

@Column(name = "first_name")
String firstName;

@Column(name = "last_name")
String lastName;

@ManyToOne
@JoinColumn(name = "group_id")
Group group;
}

```
The example below inserts the data properly:

```gherkin
Given that the groups table will contain:
| name |
| admins |
| guests |
And the users table will contain:
| firstName | lastName | group.id |
| Chuck | Norris | 1 |
| Uma | Thurman | 2 |
| Jackie | Chan | 2 |
```

## Asserting data
Expand Down
4 changes: 2 additions & 2 deletions tzatziki-spring-jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>10.20.1</version>
<version>11.1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-database-postgresql</artifactId>
<version>10.20.1</version>
<version>11.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Loading

0 comments on commit af43af4

Please sign in to comment.