Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 启动程序时,创建名为';实体管理工厂&x27;在类路径资源中定义_Java_Spring_Spring Boot_Spring Security_Spring Data Jpa - Fatal编程技术网

Java 启动程序时,创建名为';实体管理工厂&x27;在类路径资源中定义

Java 启动程序时,创建名为';实体管理工厂&x27;在类路径资源中定义,java,spring,spring-boot,spring-security,spring-data-jpa,Java,Spring,Spring Boot,Spring Security,Spring Data Jpa,出现此错误时,我在网站上阅读以修复它,我需要添加各种依赖项。我已经尽我所能添加了所有内容,但它并没有消失。请告诉我怎么了 我的实现 implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-security' implementation 'org.springframework.boot:s

出现此错误时,我在网站上阅读以修复它,我需要添加各种依赖项。我已经尽我所能添加了所有内容,但它并没有消失。请告诉我怎么了

我的实现

implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
// postgresql
implementation group: 'org.postgresql', name: 'postgresql', version: '42.2.19'

implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.16'
implementation group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.0'
implementation group: 'org.hibernate', name: 'hibernate-entitymanager', version: '5.4.30.Final'

//lombok
compileOnly 'org.projectlombok:lombok:1.18.20'
annotationProcessor 'org.projectlombok:lombok:1.18.20'

testCompileOnly 'org.projectlombok:lombok:1.18.20'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.20'

// spring-security-taglibs
implementation group: 'org.springframework.security', name: 'spring-security-taglibs', version: '5.4.2'

// javax.servlet
compileOnly group: 'javax.servlet', name: 'javax.servlet-api', version: '3.0.1'
类用户

import lombok.Data;
import org.springframework.data.annotation.Id;

import javax.persistence.*;
import java.util.Set;

@Data
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column
private String username;

@Column
private String password;

@Transient
@Column
private String passwordConfirm;

@Column
@ManyToMany(fetch = FetchType.EAGER)
private Set<Role> roles;
}
日志

应用程序属性

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/main_bd
spring.datasource.username=mysql
spring.datasource.password=mysql

spring.jpa.show-sql=true
spring.jpa.generate-ddl=false
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
类用户服务

@Service
public class UserService implements UserDetailsService {

@PersistenceContext
private EntityManager em;
@Autowired
UserRepository userRepository;
@Autowired
RoleRepository roleRepository;
@Autowired
BCryptPasswordEncoder bCryptPasswordEncoder;


@Override
public UserDetails loadUserByUsername(String username) throws 
UsernameNotFoundException {
    User user = userRepository.findByUsername(username);
    if (user == null) {
        throw new UsernameNotFoundException("Пользователь не найден");
    }
    return (UserDetails) user; // ПРОВЕРИТЬ!
}

public User findUserById(Long userId) {
    Optional<User> userFromDb = userRepository.findById(userId);
    return userFromDb.orElse(new User());
}

public List<User> allUsers() {
    return userRepository.findAll();
}

public boolean saveUser(User user) {
    User userFromDB = userRepository.findByUsername(user.getUsername());

    if (userFromDB != null) {
        return false;
    }

    user.setRoles(Collections.singleton(new Role(1L, "ROLE_USER")));
    user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
    userRepository.save(user);
    return true;
}

public boolean deleteUser(Long userId) {
    if (userRepository.findById(userId).isPresent()) {
        userRepository.deleteById(userId);
        return true;
    }
    return false;
}

}
@服务
公共类UserService实现UserDetailsService{
@持久上下文
私人实体管理者;
@自动连线
用户存储库用户存储库;
@自动连线
角色还原性角色还原性;
@自动连线
BCryptPasswordEncoder BCryptPasswordEncoder;
@凌驾
public UserDetails loadUserByUsername(字符串用户名)抛出
UsernameNotFoundException{
User=userRepository.findByUsername(用户名);
if(user==null){
抛出新用户名NotFoundException(“Пззаааааааааааааааааааа;
}
return(UserDetails)user;//!
}
公共用户findUserById(长用户ID){
可选userFromDb=userRepository.findById(userId);
返回userFromDb.orElse(newuser());
}
公开名单{
返回userRepository.findAll();
}
公共布尔存储用户(用户){
User userFromDB=userRepository.findByUsername(User.getUsername());
if(userFromDB!=null){
返回false;
}
user.setRoles(Collections.singleton(新角色(1L,“角色\用户”));
user.setPassword(bCryptPasswordEncoder.encode(user.getPassword());
userRepository.save(用户);
返回true;
}
公共布尔deleteUser(长用户ID){
if(userRepository.findById(userId.isPresent()){
userRepository.deleteById(userId);
返回true;
}
返回false;
}
}

我尝试了我找到的所有技巧,但没有任何帮助。您能告诉我这里的错误是什么吗?

错误是
org.hibernate.AnnotationException:没有为实体指定标识符:com.project.project.model.Role

Hibernate告诉您,您的实体
角色
没有关联的id。这是因为您实体中的
@Id
注释来自
org.springframework.data.annotation
包,而不是来自
javax.persistence


尝试将
@Id
的导入更改为
javax.persistence.Id

我们可以看到堆栈跟踪吗?刚刚编辑过……您是否在应用程序中的任何位置使用实体管理器?是的,在UserService中我有@PersistenceContext private EntityManager em;但是我还没有用这个变量编写方法的实现。想不出它可能会引起什么问题。。。。但是你能编辑你的问题并添加那个类吗?
Error starting ApplicationContext. To display the conditions report re-run 
your application with 'debug' enabled.
2021-05-05 12:32:55.159 ERROR 11924 --- [           main] 
o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean 
 with name 'entityManagerFactory' defined in class path resource 

Invocation of init method failed; nested exception is 
org.hibernate.AnnotationException: No identifier specified for entity: 
com.project.project.model.Role
at 
at 

... 17 common frames omitted
Process finished with exit code 1
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/main_bd
spring.datasource.username=mysql
spring.datasource.password=mysql

spring.jpa.show-sql=true
spring.jpa.generate-ddl=false
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
@Service
public class UserService implements UserDetailsService {

@PersistenceContext
private EntityManager em;
@Autowired
UserRepository userRepository;
@Autowired
RoleRepository roleRepository;
@Autowired
BCryptPasswordEncoder bCryptPasswordEncoder;


@Override
public UserDetails loadUserByUsername(String username) throws 
UsernameNotFoundException {
    User user = userRepository.findByUsername(username);
    if (user == null) {
        throw new UsernameNotFoundException("Пользователь не найден");
    }
    return (UserDetails) user; // ПРОВЕРИТЬ!
}

public User findUserById(Long userId) {
    Optional<User> userFromDb = userRepository.findById(userId);
    return userFromDb.orElse(new User());
}

public List<User> allUsers() {
    return userRepository.findAll();
}

public boolean saveUser(User user) {
    User userFromDB = userRepository.findByUsername(user.getUsername());

    if (userFromDB != null) {
        return false;
    }

    user.setRoles(Collections.singleton(new Role(1L, "ROLE_USER")));
    user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
    userRepository.save(user);
    return true;
}

public boolean deleteUser(Long userId) {
    if (userRepository.findById(userId).isPresent()) {
        userRepository.deleteById(userId);
        return true;
    }
    return false;
}

}