Auto Date for Creation and Update

#1

Hi,

is there a way with objectdb to use annotations so that - automatically - the creation Date gets  updated  once only and the change Date on each change?

It seems for hibernate there Are annotations for it for Dates and also prepersist and preupdate. How about with objectdb?

Kind regards,

Thomas

 

 

 

#2

You may be able to update these time/date fields in a JPA lifecycle event methods.

ObjectDB Support
#3

Hi,

Using the proposed solution I get a null in for the field "created" in the database.
I can confirm that the "onPrePersist" is called. Is java.sql.Timestamp not supported?

Any ideas are welcome :)

kind Regard,
Thomas

/** * Date and time when book author has been created. */
@Temporal(TemporalType.TIMESTAMP)
private Timestamp created;

/**
  * Adjusting the field to UTC date and time before being persisted.
  */
@PrePersist
public void onPrePersist() {
   this.created = Timestamp.valueOf(LocalDateTime.now(ZoneOffset.UTC));
}
#4

Timestamp is supported. If you submit a minimal runnable test case that demonstrates the issue (see format in the posting instructions), we may be able to look into it.

ObjectDB Support
#5

You can clone https://github.com/Nachtfeuer/comfortable-data
It's a git respository. On branch " timestamp-with-prepersist" you
should find the Author entity and a small Test that is trying to persist.

The persisted author has "null" on the field "created".
The return value is logged where you can see something like
"Author(fullName=c309b1d3-1e05-45c2-b972-769decdb3b73, created=null)"

You can run the test again and again and everytime it will be a new author
to simplify the test.

Also your database explorer tells the same.

  • https://github.com/Nachtfeuer/comfortable-data/blob/timestamp-with-prepersist/src/main/java/comfortable/data/model/Author.java
  • https://github.com/Nachtfeuer/comfortable-data/blob/timestamp-with-prepersist/src/test/java/comfortable/data/database/AuthorPersistenceTest.java

PS: I'm using Netbeans 11 and with right mouse button on that unittest choosing "Test File" runs the unittest.

#6

Unfortunately we cannot investigate issues in large projects, but only in minimal runnable test cases that isolate the relevant issue.

As suggested in post #4 above, you may demonstrate the issue, if you want us to look into it, with a single top class program, following the format in the posting instructions.

ObjectDB Support
#7

We are on a point where it would be MUCH more simple when you would have
been used my project as proposed. The code after here works fine BUT I'm using
JpaRepository and that's from the Spring Framework and that is a point where
the "one file application" is simply not the right approach (nor realistic)

At least ... that's what I can give you as result from your demand:

  • Without JpaRepository it does work
  • With JpaRepository it does not work

If you are not willing to accept my effort thenn close the ticket and I give up objectdb
choosing another database. I'm sorry but this ticket took me now much more time
for something that was told to be working ...

package comfortable.data.sandbox;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.UUID;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Persistence;
import javax.persistence.PrePersist;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
class Test {
    @Id
    private String message;
    private Timestamp created;
    
    @PrePersist
    private void onPrePersist() {
        this.created = Timestamp.valueOf(LocalDateTime.now(ZoneOffset.UTC));
    }
}

/**
 * Testing objectdb.
 */
public class ObjectdbTest {
    public static void main(String[] arguments) {
        final var factory = Persistence.createEntityManagerFactory("test.odb");
        final var manager = factory.createEntityManager();
        
        final var uuid = UUID.randomUUID();
        final var test = Test.builder().message(uuid.toString()).build();

        final var transaction = manager.getTransaction();
        transaction.begin();
        manager.persist(test);
        transaction.commit();
    }
}
#8

Your new post shows that @PrePersist is invoked as expected when you call persist.

It is unclear what methods in JPA the JpaRepository uses. Maybe merge is used instead of persist and it may affect which event methods are invoked. You may want to check @PostPersist and other callbacks as well.

> it would be MUCH more simple when you would have been used my project as proposed.

Clearly it would be more simple for you, but unfortunately we cannot offer this service of debugging and investigating issues in user projects (obviously as part of our free support), as we do not have unlimited resources. We hope for your understanding and good luck with whatever you choose.

ObjectDB Support

Reply