我正在尝试实现用户详细信息服务以包含实体的审核数据。
UserEntity
类如下:
@Data
@Entity
@Audited
public class UserEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(unique = true,updatable = false)
private String uuid;
private String username;
private String firstName;
private String lastName;
}
我还有以下类,其中包含审计元数据。该课程将由其他实体进行扩展。
@Data
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
@Audited
public class Auditable {
@CreatedDate
private Long createdDate;
@LastModifiedDate
private Long lastModifiedDate;
@CreatedBy
private UserEntity createdBy;
@LastModifiedBy
private UserEntity modifiedBy;
}
这是我的用户详细信息服务。
@Configuration
@EnableJpaAuditing
public class SpringSecurityAuditorAware implements AuditorAware<UserEntity> {
@Autowired
private AccessToken accessToken;
@Autowired
private UserEntityRepository userRepo;
@Override
public UserEntity getCurrentAuditor() {
UserEntity user = userRepo.findByUuid(accessToken.getSubject());
return user;
}
}
现在,当我运行代码时,我收到以下错误消息。
Caused by: org.hibernate.MappingException: Could not determine type for: api.core.model.UserEntity, at table: business_entity, for columns: [org.hibernate.mapping.Column(created_by)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:431) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.envers.configuration.internal.metadata.AuditMetadataGenerator.addValueInFirstPass(AuditMetadataGenerator.java:219) ~[hibernate-envers-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.envers.configuration.internal.metadata.AuditMetadataGenerator.addValue(AuditMetadataGenerator.java:345) ~[hibernate-envers-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.envers.configuration.internal.metadata.AuditMetadataGenerator.addProperties(AuditMetadataGenerator.java:371) ~[hibernate-envers-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.envers.configuration.internal.metadata.AuditMetadataGenerator.generateFirstPass(AuditMetadataGenerator.java:662) ~[hibernate-envers-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.envers.configuration.internal.EntitiesConfigurator.configure(EntitiesConfigurator.java:94) ~[hibernate-envers-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.envers.boot.internal.EnversServiceImpl.doInitialize(EnversServiceImpl.java:152) ~[hibernate-envers-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.envers.boot.internal.EnversServiceImpl.initialize(EnversServiceImpl.java:117) ~[hibernate-envers-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.envers.boot.internal.AdditionalJaxbMappingProducerImpl.produceAdditionalMappings(AdditionalJaxbMappingProducerImpl.java:99) ~[hibernate-envers-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:288) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:847) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:874) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60) ~[spring-orm-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:353) ~[spring-orm-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:370) ~[spring-orm-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:359) ~[spring-orm-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
... 171 common frames omitted
如果删除UserEntity
字段,一切正常,审核数据createdDate
和lastModifiedDate
会正确记录在数据库中。
我还为UserEntity字段添加了@ManyToOne
注释,代码运行正常,但数据库没有字段createdBy
和modifiedBy
。我该如何解决这个问题?
答案 0 :(得分:0)
我们应该有一个名为Auditable_aud的表。