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

BaseEntityListener - @PostUpdate not working #46112

Open
Paul6552 opened this issue Feb 5, 2025 · 2 comments
Open

BaseEntityListener - @PostUpdate not working #46112

Paul6552 opened this issue Feb 5, 2025 · 2 comments
Labels
area/hibernate-orm Hibernate ORM kind/bug Something isn't working triage/regression

Comments

@Paul6552
Copy link
Contributor

Paul6552 commented Feb 5, 2025

Describe the bug

We have code, which worked for many years. But suddenly we noticed that something was no longer working. Unfortunately, the code only runs once a year, so we don't know if other code changes caused it to stop working. Maybe it was the change from Quarkus 3.8 to 3.15.2. Unfortunately, we don't know.

We have the following Java-classes:

public class BaseEntityListener {

    @PrePersist
    public void prePersist(Object persistedEntity) {
        if ( persistedEntity instanceof BaseEntity entity ) {
            if (entity.getCreatedOn() == null) {
                LocalDateTime now = LocalDateTime.now();
                entity.setCreatedOn(now);
                //entity.setModifiedOn(now);
            }
        }
    }

    @PostUpdate
    public void postUpdate(Object persistedEntity) {
        if ( persistedEntity instanceof BaseEntity entity ) {
            entity.setModifiedOn(LocalDateTime.now());
            System.out.println("BaseEntityListener::postUpdate");
        }
    }
}
@MappedSuperclass
@Data
@EntityListeners( BaseEntityListener.class )
@EqualsAndHashCode(callSuper = false)
public abstract class BaseEntity extends PanacheEntityBase implements Serializable {

    @Basic
    @Column( name = "CREATED_ON" )
    private LocalDateTime createdOn;
    @Basic
    @Column( name = "MODIFIED_ON" )
    private LocalDateTime modifiedOn;
}
@Entity
@Data
@NoArgsConstructor
@Table( uniqueConstraints = @UniqueConstraint( columnNames={ "bewertungsid", "wertungsnameid" } ) )
@EqualsAndHashCode(callSuper = true)
public class Wertung extends BaseEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Basic
    @Column(name = "wert")
    private int wert;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "bewertungsid")
    private Bewertung bewertung;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="wertungsnameid",referencedColumnName="id")
    private Wertungsname wertungsname;
}

This is the function we call:

    @Transactional
    public void modifyBewertung( ModifyBewertungDTO modifyBewertungDTO) {
        Wertung byId = wertungDao.findById( 306L );
        byId.setWert( 9999 );
    }

The wertung is updated succesfully in the database. "Wert" has the value 9999 but modified_on is still null although the BaseEntityListener was running.
If I create a new Wertung, the BaseEntityListener is working as expected and writes the createdOn and modifiedOn (@PrePersist) successfully into the database.

I checked the upgrade guide from version 3.8.4 to 3.15.2 but couldn`t find anything about EntityListeners.
Maybe someone can help me out. Thanks

//EDIT
When I change @PostUpdate to @PreUpdate, the code is working and I get the localDateTime into the modifiedOn column.

Expected behavior

Write LocalDateTime into the database column modifiedOn.

Actual behavior

LocalDateTime is not written into the database column modifiedOn

How to Reproduce?

Copy the classes above and run the code :/

Output of uname -a or ver

Linux

Output of java -version

21.0.5

Quarkus version or git rev

3.15.2

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

maven 3.9.9

Additional information

No response

@Paul6552 Paul6552 added the kind/bug Something isn't working label Feb 5, 2025
@gsmet
Copy link
Member

gsmet commented Feb 5, 2025

I think it would help if you could assemble a small Maven project reproducing the issue.

Ideally we would just trigger the issue by updating Quarkus.

@Paul6552
Copy link
Contributor Author

Paul6552 commented Feb 5, 2025

@gsmet Ok, here we go:

demo.zip

  1. Download this zip and unzip
  2. Go to application.properties and change the settings for the database connection to any database you have.
  3. Then activate 1.Step in application properties
    quarkus.hibernate-orm.database.generation=drop-and-create
  4. Run with maven command "compile quarkus:dev -Ddebug=5020"
  5. Stop program (There is an error in the console but this is fine). The first run is only for creating the database myEntity.
  6. open the database myEntity and input everything from import.sql under resources
  7. Change application.properties again. Change to step 2:
    quarkus.hibernate-orm.database.generation=update
    Otherwise quarkus would again destroy our data in the database
  8. RUN the program. The scheduler runs and updates the entity with id 2. The update works. The field is updated but NOT the modifiedOn! BaseEntityListener @postupdate is not working correctly.

Only for testing you can change "BaseENtityListener" from @postupdate to @PreUpdate. After the change to preUpdate the update with the time into the database works but not with PostUpdate.

Good night from vienna

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/hibernate-orm Hibernate ORM kind/bug Something isn't working triage/regression
Projects
None yet
Development

No branches or pull requests

3 participants