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

Hibernate: merge detached entities with inheritance ignore null attributes on child classes #39654

Closed
antoinechamot opened this issue Mar 23, 2024 · 5 comments
Labels
area/hibernate-orm Hibernate ORM area/persistence OBSOLETE, DO NOT USE kind/bug Something isn't working

Comments

@antoinechamot
Copy link

Describe the bug

Using Hibernate entity inheritance ( Quarkus 3 ).

I have created a repository with a test class to reproduce the bug: https://github.com/antoinechamot/quarkus-bugfind. Launch tests (one must fail )

My entities are

@SuperBuilder
@NoArgsConstructor
@Setter
@Getter
@Inheritance(strategy = InheritanceType.JOINED)
@Entity(name = "shape")
public abstract class Shape {

    @Id
    @Builder.Default
    private UUID id = UUID.randomUUID();

    @Column(nullable = false, length = 100)
    private String name;

    @Column(length = 100)
    private String surname;
}
@Entity(name = "circle")
@SuperBuilder
@NoArgsConstructor
@Getter
@Setter
public class Circle extends Shape{

    private String color;
}

I have a simple repository with one method

@Override
@Transactional
public Circle update(Circle circle) {
    return getEntityManager().merge(circle);
}

For test i populate the database with some data:

INSERT INTO shape (id,name,surname) VALUES ('847dae35-7ae0-4041-8257-e23fb7c5b033','My circle','My surname');
INSERT INTO circle (id,color) VALUES ('847dae35-7ae0-4041-8257-e23fb7c5b033','red');
...

And then i run a test to update the circle color to null

    /**
     * Update Circle color to null : KO
     */
    @Test
    void testUpdateCircleColorNullKO(){
        Circle myCircle = Circle.builder()
                .id(UUID.fromString("847dae35-7ae0-4041-8257-e23fb7c5b033"))
                .name("My circle")
                .surname("My surname")
                .color(null)
                .build();

        repository.update(myCircle);

        Circle updatedCircle = repository.findById(UUID.fromString("847dae35-7ae0-4041-8257-e23fb7c5b033"));
        assertThat(updatedCircle.getColor()).isNull();
    }

Expected behavior

  • On merge, I expect hibernate to launch an update request to set the value in the color column of the circle table to null.
  • If I put null on surname for example, an update request is launched by hibernate.
  • It seems that the problem only occurs on attributes of the child class Circle and not on the parent class Shape

Actual behavior

  • On merge, null attributes of the child class Circle are ignored. Not update request is done on the circle table.
  • If I update the color to something not null like 'blue', it is correctly updated

How to Reproduce?

Repository : https://github.com/antoinechamot/quarkus-bugfind

Output of uname -a or ver

No response

Output of java -version

No response

Quarkus version or git rev

No response

Build tool (ie. output of mvnw --version or gradlew --version)

No response

Additional information

No response

@antoinechamot antoinechamot added the kind/bug Something isn't working label Mar 23, 2024
@quarkus-bot quarkus-bot bot added area/hibernate-orm Hibernate ORM area/persistence OBSOLETE, DO NOT USE labels Mar 23, 2024
Copy link

quarkus-bot bot commented Mar 23, 2024

/cc @gsmet (hibernate-orm), @yrodiere (hibernate-orm)

@yrodiere
Copy link
Member

Hello,

Is this different from #38722 ? Because that one is a bug in Hibernate ORM, not in Quarkus, and it's already being fixed upstream.

If it's not the same bug, you might want to check these already-reported issues that mention "merge": https://github.com/quarkusio/quarkus/issues?q=is%3Aissue+is%3Aopen+merge+label%3Aarea%2Fhibernate-orm

@antoinechamot
Copy link
Author

antoinechamot commented Mar 25, 2024

Hi,
It could be indeed related to #38722 as inheritance can be considered as "some case". No sure tho as I am not a specialist of hibernate ORM
Are you referring to this hibernate PR in progress on bytecode enhancement : hibernate/hibernate-orm#7912 ?

@yrodiere
Copy link
Member

I am referring to https://hibernate.atlassian.net/browse/HHH-17761 and the corresponding PR hibernate/hibernate-orm#7912 , yes.

@yrodiere
Copy link
Member

@antoinechamot I'll go ahead and close this as duplicate of #38722, which has been fixed on main (Quarkus 3.11). Feel free to comment here if it turns out this was a different problem.
Thanks for your feedback.

@yrodiere yrodiere closed this as not planned Won't fix, can't repro, duplicate, stale May 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/hibernate-orm Hibernate ORM area/persistence OBSOLETE, DO NOT USE kind/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants