我使用专用存储库创建/注册新用户:
@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;
}
}
存储库从我使用的Session
获取当前SessionFactory
save()
AppUser
@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
无效?
答案 0 :(得分:1)
似乎使用Session
API无法使用您正在使用的JPA回调(@PrePersist
,@PreUpdate
)。需要使用JPA EnityManager配置Hibernate。
可能helpful SO post。 关于这个问题的另一个有用的external article。