2

I'm creating / registering a new user using a dedicated repository:

@Service
public class RegistrationService {

    @Autowired
    private AppUserRepository appUserRepository;

    @Transactional
    public UserCredentialsDto register(UserCredentialsDto userCredentialsDto) {

        String salt = BCrypt.gensalt();
        AppUser appUser = new AppUser();
        appUser.setUsername(userCredentialsDto.getUsername());
        appUser.setPassword(BCrypt.hashpw(userCredentialsDto.getPassword(), salt));

        this.appUserRepository.add(appUser);
        return userCredentialsDto;
    }

}

The repository fetches the current Session from the SessionFactory which I use to save() the AppUser

@Entity 
public class AppUser externds AbstractTimestampEntity { 
     /* .. */ 
}

as you can see:

@Repository
public class AppUserRepository {

    @Autowired
    private SessionFactory sessionFactory;

    public void add(AppUser appUser) {
        Session session = sessionFactory.getCurrentSession();
        session.save(appUser);
    }

}

However, I am getting

org.postgresql.util.PSQLException: ERROR: null value in column "created" violates not-null constraint
  Detail: Failing row contains (1, null, null, null, $2a$10$0GEFgVryvwG3Dv5LN1tg6evPpl4wQ9uJOWiMKMyZq8HVQXCMRt.kS, null, test).
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2284) ~[postgresql-9.4.1208-jdbc42-atlassian-hosted.jar:9.4.1208]
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2003) ~[postgresql-9.4.1208-jdbc42-atlassian-hosted.jar:9.4.1208]
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:200) ~[postgresql-9.4.1208-jdbc42-atlassian-hosted.jar:9.4.1208]
    at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:424) ~[postgresql-9.4.1208-jdbc42-atlassian-hosted.jar:9.4.1208]
    at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:161) ~[postgresql-9.4.1208-jdbc42-atlassian-hosted.jar:9.4.1208]
    at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:133) ~[postgresql-9.4.1208-jdbc42-atlassian-hosted.jar:9.4.1208]
    at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105) ~[commons-dbcp-1.4.jar:1.4]
    at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105) ~[commons-dbcp-1.4.jar:1.4]
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:175) ~[hibernate-core-5.2.11.Final.jar:5.2.11.Final]
    ..

which is because created, a field provided by AbstractTimestampEntity, is null even though it should have been set to the current date/time before the commit:

@MappedSuperclass
public abstract class AbstractTimestampEntity {

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "created", nullable = false)
    @Type(type="org.hibernate.type.ZonedDateTimeType")
    private ZonedDateTime created;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "updated", nullable = false)
    @Type(type="org.hibernate.type.ZonedDateTimeType")
    private ZonedDateTime updated;

    @PrePersist
    final protected void onCreate() {
        updated = created = ZonedDateTime.now();
    }

    @PreUpdate
    final protected void onUpdate() {
        updated = ZonedDateTime.now();
    }

}

What am I missing here? Why is @PrePersist not working?

Stefan Falk
  • 23,898
  • 50
  • 191
  • 378

1 Answers1

1

It seems that using the Session API is not working with the JPA callbacks you're using (@PrePersist, @PreUpdate). Hibernate needs to be configured using a JPA EnityManager.

Possible helpful SO post. And another helpful external article about the issue.

David Artmann
  • 4,272
  • 1
  • 16
  • 24