@PrePersist在Hibernate+Spring环境中无法使用@MappedSuperclass



我正在使用专用存储库创建/注册一个新用户:

@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;
    }
}

存储库从我用来save() AppUserSessionFactory中获取当前Session

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

如您所见:

@Repository
public class AppUserRepository {
    @Autowired
    private SessionFactory sessionFactory;
    public void add(AppUser appUser) {
        Session session = sessionFactory.getCurrentSession();
        session.save(appUser);
    }
}

但是,我得到了

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]
    ..

这是因为created,一个由AbstractTimestampEntity提供的字段,即使它应该设置为提交前的当前日期/时间,也是null的:

@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();
    }
}

我在这里错过了什么?为什么@PrePersist不起作用?

似乎

使用 Session API 不适用于您正在使用的 JPA 回调(@PrePersist@PreUpdate )。Hibernate需要使用JPA EnityManager进行配置。

可能有用的SO帖子。还有一篇关于这个问题的有用的外部文章。

相关内容

  • 没有找到相关文章

最新更新