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

Entity with nested entity using @Embedded - nested entity not persisted #1565

Closed
rdehuyss opened this issue Jul 16, 2023 · 4 comments
Closed
Assignees
Labels
type: bug A general bug

Comments

@rdehuyss
Copy link

rdehuyss commented Jul 16, 2023

Hi there,

I'm currently creating an app with Spring Boot and Spring Data JDBC. When I use the @Embedded for a nested entity, the nested object is not persisted. For entities that have all data in the table, everything is returned correctly. (e.g. if I debug the test and do companyRepository.findAll(), the Company objects that have been persisted via data.sql do have an Address).

I'm using:

  • Spring Boot 3.1.1
  • Spring Data JDBC 3.1.1
  • OpenJDK 17
  • Postgres as database

Below a reproducible test case:

schema.sql

drop table if exists companies;
create table companies (
                        "id" uuid PRIMARY KEY,
                        "version" int,
                        "name" varchar not null,
                        "vat_number" varchar,
                        "address.first_line" varchar,
                        "address.second_line" varchar,
                        "address.third_line" varchar,
                        "address.country" varchar
);

data.sql

insert into companies("id", "version", "name", "vat_number", "address.first_line", "address.second_line", "address.third_line", "address.country") values ('8f19401a-e6f1-4a73-9244-464a2ad2f69a', 1, 'Company 1', 'BE0123456', 'Line 1', 'Line 2', null, 'BE');
insert into companies("id", "version", "name", "vat_number", "address.first_line", "address.second_line", "address.third_line", "address.country") values ('8f19401a-e6f1-4a73-9244-464a2ad2f69b', 1, 'Company 2', 'BE0123457', 'Line 1', 'Line 2', null, 'BE');

Company record

@Table(name = "companies")
public record Company (
        @Id UUID id,
        @Version int version,
        String name,
        String vatNumber,
        @Embedded(onEmpty = Embedded.OnEmpty.USE_NULL, prefix = "address.") Address address
) {
    public Company(String name, String vatNumber, Address address) {
        this(UUID.randomUUID(), 0, name, vatNumber, address);
    }

    public Company(String name, Address address) {
        this(UUID.randomUUID(), 0, name, null, address);
    }
}

Address record

public record Address(String firstLine, String secondLine, String thirdLine, String country) {

    public Address(String firstLine, String secondLine, String country) {
        this(firstLine, secondLine, null, country);
    }
}

CompanyRepository

@Repository
public interface CompanyRepository extends CrudRepository<Company, UUID> {

    Company getById(UUID id);
}

Repository Test

@SpringBootTest
class CompanyRepositoryTest {

    @Autowired
    CompanyRepository companyRepository;

    @Test
    public void saveAndGetCompany() {
        Company company = new Company("Other company", "BE98765", new Address("First Line", "Second Line", "BE"));
        companyRepository.save(company);
        Company actualCompany = companyRepository.getById(company.id());

        assertNotNull(actualCompany.address());
    }
}
@rdehuyss rdehuyss changed the title Entity with Nested Entity using @Embedded - nested entity not persisted Entity with nested entity using @Embedded - nested entity not persisted Jul 16, 2023
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jul 16, 2023
@rdehuyss
Copy link
Author

After further investigation, the issue is apparently the prefix = "address." with a dot. If the dot is changed to an underscore (prefix = "address_"), it works.

So, I don't know whether this is intended behaviour?

@schauder schauder added the type: bug A general bug label Jul 17, 2023
@schauder
Copy link
Contributor

This is most certainly not the intended behaviour. Do you get an exception, or anything?

@rdehuyss
Copy link
Author

No - that's why I thought to report it.

@mp911de mp911de removed the status: waiting-for-triage An issue we've not yet triaged label Jul 17, 2023
schauder added a commit that referenced this issue Jul 17, 2024
Make test methods package private.

See #1565
schauder added a commit that referenced this issue Jul 17, 2024
JdbcRepositoryEmbeddedIntegrationTests no longer runs for all databases, since it doesn't tests anything specific to differen RDBMSs.
Therefore the test also got renamed.

See #1565
schauder added a commit that referenced this issue Jul 17, 2024
Make test methods package private.

See #1565
schauder added a commit that referenced this issue Jul 17, 2024
JdbcRepositoryEmbeddedIntegrationTests no longer runs for all databases, since it doesn't tests anything specific to differen RDBMSs.
Therefore the test also got renamed.

See #1565
schauder added a commit that referenced this issue Jul 17, 2024
Make test methods package private.

See #1565
schauder added a commit that referenced this issue Jul 17, 2024
JdbcRepositoryEmbeddedIntegrationTests no longer runs for all databases, since it doesn't tests anything specific to differen RDBMSs.
Therefore the test also got renamed.

See #1565
@schauder
Copy link
Contributor

Thanks, that's fixed.

@schauder schauder added this to the 3.2.9 (2023.1.9) milestone Jul 17, 2024
schauder added a commit that referenced this issue Jul 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

4 participants