Skip to content

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

Closed
@rdehuyss

Description

@rdehuyss

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());
    }
}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions