maven 即使使用@entity也不会创建bean

pbpqsu0x  于 12个月前  发布在  Maven
关注(0)|答案(2)|浏览(152)

我无法运行Sping Boot 应用程序,因为JPA存在问题。但我无法找到问题并解决它。我搜索了几个堆栈溢出主题的解决方案,但没有一个解决方案对我有帮助。我定义了以下模型:
我的实体类的代码-

package com.ncu.legalVise.entities;


import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import org.springframework.stereotype.Service;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Entity
@Table(name ="users")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Users {

    
//  public Users(String username, String password, int enabled) {
//      super();
//      this.username = username;
//      this.password = password;
//      this.enabled = enabled;
//  }
//
//  public Users() {
//      super();
//  }

    @Id
    private String username;
    
    @Column(nullable = false)
    private String password;
    
    @Column(nullable = false)
    private int enabled;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getEnabled() {
        return enabled;
    }

    public void setEnabled(int enabled) {
        this.enabled = enabled;
    }

    
    
}

字符串
我的pom文件-

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ncu</groupId>
    <artifactId>LegalVise</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>LegalVise</name>
    <description>Legal Vise</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        
        
        
        <!-- https://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.0-api -->
<dependency>
    <groupId>org.hibernate.javax.persistence</groupId>
    <artifactId>hibernate-jpa-2.0-api</artifactId>
    <version>1.0.1.Final</version>
</dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    
        <dependency>
        <groupId>jakarta.persistence</groupId>
        <artifactId>jakarta.persistence-api</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
            
        </dependency>
                <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>

    </dependencies>
    

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>


我的控制器-

package com.ncu.legalVise.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ncu.legalVise.entities.Authorities;
import com.ncu.legalVise.entities.Users;
import com.ncu.legalVise.repository.AuthoritiesRepository;
import com.ncu.legalVise.repository.UsersRepository;

@RestController
@RequestMapping("/api/public/security")
public class SecurityController {
    
    Users user;
    Authorities authority;
    UsersRepository usersRepository;
    AuthoritiesRepository authoritiesRepository;
    
    @Autowired
    SecurityController(Users user, Authorities authority,UsersRepository usersRepository,AuthoritiesRepository authoritiesRepository)
    {
        user = this.user;
        authority= this.authority;
        usersRepository=this.usersRepository;
        authoritiesRepository=this.authoritiesRepository;
        
    }
    @PostMapping("/register")
    public ResponseEntity<Object> register(@RequestBody String userName, @RequestBody String password, @RequestBody String authorities)
    {
        user.setUsername(userName);
        user.setPassword(password);
        user.setEnabled(1);
        authority.setUsername(userName);
        authority.setAuthority(authorities);
        if(!usersRepository.existsById(userName))
        {
            usersRepository.save(user);
            authoritiesRepository.save(authority);
            return ResponseEntity.ok(usersRepository.findById(userName));
        }
        return ResponseEntity.notFound().build();
    }
}


错误信息-
申请表请尽快开始
产品描述:
com.ncu.legalVise.controller.SecurityController中构造函数的参数0需要类型为“com.ncu.legalVise.entities.Users”的Bean,但找不到该Bean。
行动:
考虑在您的配置中定义一个类型为“com.ncu.legalVise.entities.Users”的bean。
请帮我解决这个问题
我尝试改变依赖关系,但没有任何工作

tsm1rwdh

tsm1rwdh1#

由于您的目标是SpringBoot 3,因此应该在Users类中将javax.persistence导入更改为jakarta.persistence导入。
SpringBoot 3的主要变化是采用了JakartaEE 9(放弃了对jdk 8/jdk 11的支持,以拥有jdk 17基线),这导致许多JavaEE功能必须从javax迁移到jakarta,包括JPA。
由于您的代码编译,我还建议清理您的pom.xml以删除旧的JPA依赖项,以便删除javax.persistence识别的包(以及旧的JavaEE 8 jpa支持)。

afdcj2ne

afdcj2ne2#

基于您正在使用的spring-boot(3.2.0)版本,我看到一些错误的配置来修复它
pom.xml中删除下一个依赖项

<!-- https://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.0-api -->
<dependency>
    <groupId>org.hibernate.javax.persistence</groupId>
    <artifactId>hibernate-jpa-2.0-api</artifactId>
    <version>1.0.1.Final</version>
</dependency>

字符串
另外,请确保在项目中从javax.导入到jakarta.时进行替换。
例如:而不是

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;


必须是:

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

相关问题