将实体转移到其他包时出现 Not a managed type 错误?

There is Not a managed type error when transfer entity to other package?

有 spring+jpaRepositories 应用程序。

因此,PersistenceConfig:

@Configuration
@EnableTransactionManagement
@EnableJpaAuditing
@EnableJpaRepositories(basePackages = {"persistence"})
@PropertySource("classpath:application-${envConfig}.properties")
@ComponentScan(basePackages = {"persistence"})
public class PersistenceConfig {
...
}

有这样的目录树:

web
models
      User.java
persistence
      PersistenceConfig.java
      UserRepository.javav
services

如果在模型中找到实体 - 一切正常。 如果在持久性中定位实体 - 有错误消息

Caused by: java.lang.IllegalArgumentException: Not a managed type: class persistence.User

@Entity
@Audited
@Table(name = "User"})})
@NoArgsConstructor(access = AccessLevel.PUBLIC)
@Getter
@Setter
public class User {
@Id
private int id;
private String name;
}

您应该在 ComponentScan 中添加 models 包。

@ComponentScan(basePackages = {"persistence" , "models"})