Skip to content

Support @GeneratedValue(strategy = GenerationType.IDENTITY) in @EmbeddedId to auto-update the ID after insert #3601

@deverlex

Description

@deverlex

Problem
Currently, Ebean does not support using @GeneratedValue(strategy = GenerationType.IDENTITY) inside @EmbeddedId. When using @EmbeddedId with an auto-generated primary key, the generated ID is not updated back into the entity after saving.

Additionally, calling refresh() on the entity results in the following error:

Bean not found during lazy load or refresh. Id:PartitionKey@0(orgId:78) type:class models.viorg.Transaction

This makes it impossible to retrieve the generated ID after saving an entity that uses @EmbeddedId.

Expected Behavior

  • When saving an entity with @EmbeddedId, the generated ID should be automatically set back into the entity.
  • refresh() should not be required to retrieve the generated ID.

Example Code
Current behavior (not working as expected)

@Embeddable
public class AId implements Serializable {
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id; // This should be auto-generated from DB

    private Long orgId;

    // Constructors, Getters, Setters...
}

@Entity
public class A {
    @EmbeddedId
    private AId id;
}
A a = new A();
a.setId(new AId(null, 100L)); // Setting only orgId, expecting id to be auto-generated
ebeanServer.save(a);

// Expected: a.getId().getId() should contain the generated ID
// Actual: a.getId().getId() remains null unless I manually refresh()

Expected behavior (similar to @id)

@Entity
public class B {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
}
B b = new B();
ebeanServer.save(b);
System.out.println(b.getId()); // ✅ ID is correctly updated

Why is this important?
This is inconsistent with how @id works

Other JPA implementations (such as Hibernate) also do not auto-update @EmbeddedId, but they provide refresh() as a workaround. It would be great if Ebean could handle this automatically.

Environment
Ebean version: 14.10.0
Database: MariaDB
JDK version: 17

Would it be possible to enhance Ebean to support this behavior for @EmbeddedId? 🚀

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions