@SpringBootTest
class UserRepositoryTest {
private final UserRepository userRepository;
@Autowired
public UserRepositoryTest(UserRepository userRepository) {
this.userRepository = userRepository;
}
@Test
void test() {
User user = new User("test", "test", "te[email protected]");
userRepository.save(user);
}
}
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotEmpty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.domain.Persistable;
import org.springframework.data.relational.core.mapping.Table;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import java.util.Collection;
import java.util.List;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Table("users")
@Entity
public class User implements UserDetails, Persistable<String> {
@Id
@NotEmpty(message = "Username is empty!")
private String username;
@NotEmpty(message = "Password is empty!")
private String password;
@NotEmpty(message = "Email is empty!")
@Email(message = "Email isn't valid")
private String email;
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return List.of(new SimpleGrantedAuthority("ROLE_USER"));
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
@Override
public String getId() {
return getUsername();
}
@Override
public boolean isNew() {
return getId() == null;
}
}
@EnableTransactionManagement
@EnableJpaRepositories(excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {UserRepository.class}))
@Configuration
public class DataConfig {
}
import com.w4t3rcs.todolist.model.entity.User;
import org.springframework.data.repository.CrudRepository;
public interface UserRepository extends CrudRepository<User, String> {
}
还有堆栈跟踪:org.springframework.data.relational.core.conversion.DbActionExecutionException:无法执行DbAction.UpdateRoot(entity=User(username=test,password=$2a$10$jbNHYFBd9AGD8LVtXyej4.7paMKX30IjZryMCsKs3xyhfIsbF0KXC,[email 受保护的]))
at org.springframework.data.jdbc.core.AggregateChangeExecutor.execute(AggregateChangeExecutor.java:118)
at org.springframework.data.jdbc.core.AggregateChangeExecutor.lambda$executeSave$0(AggregateChangeExecutor.java:61)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at org.springframework.data.relational.core.conversion.SaveBatchingAggregateChange.forEachAction(SaveBatchingAggregateChange.java:70)
at org.springframework.data.jdbc.core.AggregateChangeExecutor.executeSave(AggregateChangeExecutor.java:61)
at org.springframework.data.jdbc.core.JdbcAggregateTemplate.performSave(JdbcAggregateTemplate.java:491)
at org.springframework.data.jdbc.core.JdbcAggregateTemplate.save(JdbcAggregateTemplate.java:168)
at org.springframework.data.jdbc.repository.support.SimpleJdbcRepository.save(SimpleJdbcRepository.java:68)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:352)
at org.springframework.data.repository.core.support.RepositoryMethodInvoker$RepositoryFragmentMethodInvoker.lambda$new$0(RepositoryMethodInvoker.java:277)
at org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:170)
at org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:158)
at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:516)
at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:285)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:628)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:168)
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:143)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:385)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:249)
at jdk.proxy2/jdk.proxy2.$Proxy115.save(Unknown Source)
at com.w4t3rcs.todolist.UserRepositoryTest.test(UserRepositoryTest.java:26)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
原因:java.lang.IllegalStateException:未找到类 com.w4t3rcs.todolist.model.entity.User 所需的标识符属性 在 org.springframework.data.mapping.PersistentEntity.getRequiredIdProperty(PersistentEntity.java:135) 在 org.springframework.data.relational.core.mapping.BasicRelationalPersistentEntity.getIdColumn(BasicRelationalPersistentEntity.java:159) 在 org.springframework.data.jdbc.core.convert.SqlContext.getIdColumn(SqlContext.java:44) 在 org.springframework.data.jdbc.core.convert.SqlGenerator.getIdColumn(SqlGenerator.java:807) 在 org.springframework.data.jdbc.core.convert.SqlGenerator.createBaseUpdate(SqlGenerator.java:718) 在 org.springframework.data.jdbc.core.convert.SqlGenerator.createUpdateSql(SqlGenerator.java:692) 在 org.springframework.data.util.Lazy.getNullable(Lazy.java:135) 在 org.springframework.data.util.Lazy.get(Lazy.java:113) 在 org.springframework.data.jdbc.core.convert.SqlGenerator.getUpdate(SqlGenerator.java:369) 在org.springframework.data.jdbc.core.convert.DefaultDataAccessStrategy.update(DefaultDataAccessStrategy.java:136) 在org.springframework.data.jdbc.core.JdbcAggregateChangeExecutionContext.updateWithoutVersion(JdbcAggregateChangeExecutionContext.java:336) 在org.springframework.data.jdbc.core.JdbcAggregateChangeExecutionContext.executeUpdateRoot(JdbcAggregateChangeExecutionContext.java:129) 在 org.springframework.data.jdbc.core.AggregateChangeExecutor.execute(AggregateChangeExecutor.java:93) ... 36 更多
我尝试过将 @Id 类型设置为 Long 并尝试更改一些注释,但没有任何帮助。
在您的
Entity
中,我看到一些缺少的配置来修复它,只需将当前配置更改为下一个:
@Data
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "users")
@Entity
public class User implements UserDetails {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotEmpty(message = "Username is empty!")
@Column(unique = true)
private String username;
@NotEmpty(message = "Password is empty!")
private String password;
@NotEmpty(message = "Email is empty!")
@Email(message = "Email isn't valid")
@Column(unique = true)
private String email;
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return List.of(new SimpleGrantedAuthority("ROLE_USER"));
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
}
我还对
Entity
做了一些更改,我认为username
和email
应该是UNIQUE
。
我也认为你不需要
implements Persistable interface
。
以上所有信息/更改应该可以解决您的问题