Skip to content

[BUG] @Embeddable records are not deserialized properly in Map values #584

@lrasku

Description

@lrasku

Which JNoSQL project the issue refers to?

JNoSQL (Core)

Bug description

The Jakarta NoSQL specification gives this example of using embeddable classes as map value types:

@Entity
public class Computer {
    @Id
    private String name;

    @Column
    private Map<String, Program> programs;
}

@Embedded // sic
public class Program {
    @Id
    private String name;

    @Column
    private Map<String, String> socialMedia;
}

However, when I try this (with record types at least), the objects are inserted into the database correctly, but reading them from it fails with an error like java.lang.ClassCastException: class java.util.HashMap cannot be cast to class org.example.Program (java.util.HashMap is in module java.base of loader 'bootstrap'; org.example.Program is in unnamed module of loader 'app').

JNoSQL Version

JNoSQL version 1.1.6

Steps To Reproduce

A minimal Java code fragment is:

@Entity
public record Computer(
    @Id String name,
    @Column Map<String, Program> programs
) {}

@Embeddable
public record Program(
    @Id String name,
    @Column Map<String, String> socialMedia
) {}

// ...

var program = new Program(
    "Renamer",
    Map.of("twitter", "x")
);
var computer = new Computer(
    "Computer",
    Map.of("Renamer", program)
);
template.insert(computer);

// Fails with:
// Exception in thread "main" java.lang.ClassCastException: class java.util.HashMap cannot be cast to class org.example.Program (java.util.HashMap is in module java.base of loader 'bootstrap'; org.example.Program is in unnamed module of loader 'app')
//   at org.example.EmbeddedFailure.main(EmbeddedFailure.java:27)
template.find(Computer.class, "Computer").get().programs().get("Renamer").socialMedia();

The stack trace does not seem to contain any relevant information, since the error only happens at the point of conversion, when the code has already exited the JNoSQL stack.

Expected Results

The program should exit silently and successfully, the conversion done properly.

Code example, screenshot, or link to a repository

See the attached zip file for a docker-compose project that demonstrates the problem.

  1. Run the (unzipped) example with docker compose up --build -V.
  2. The app-1 container will exit with a ClassCastException after the database initialization, which may take a few seconds. In the logs, this should look like:

app-1 | Mar 26, 2025 10:43:56 PM org.jboss.weld.environment.se.WeldContainer shutdown
app-1 | INFO: WELD-ENV-002001: Weld SE container 81c670ac-0db9-45ce-88ab-39c4d6a720fc shut down
app-1 | Exception in thread "main" java.lang.ClassCastException: class java.util.HashMap cannot be cast to class org.example.Program (java.util.HashMap is in module java.base of loader 'bootstrap'; org.example.Program is in unnamed module of loader 'app')
app-1 | at org.example.EmbeddedFailure.main(EmbeddedFailure.java:27)

embeddable-example.zip

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions