如何在 Hibernate 中追踪 MappingMetamodelImpl.managedType 异常的原因

问题描述 投票:0回答:1

我正在制作一个原始的应用程序。当我使用 SQLite 时一切正常,现在我开始为 Postgres 重新制作它,就好像所有的 Java 都开始对我不利。该应用程序的本质很简单:

  1. 将打开一个简单的 HTML 页面
  2. 你可以创建/删除用户等等,原则上这并不重要
  3. 用户存储在数据库中

这是代码:

package com.example.module11.entity;

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

@Entity
@Table(name = "userStore")

public class User {
    @Id
    @Column(name = "id")
    private Long id;
    @Column(name = "name")
    private String name;
    @Column(name = "age")
    private Integer age;



    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

文件:

package com.example.module11.repo;

import com.example.module11.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;
@Repository
public interface UserRepository extends JpaRepository<User,Long> {
    public Optional<User> findById(Long id);
    public void deleteUserById(Long id);

}

文件:

package com.example.module11.userController;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.servlet.ModelAndView;

import java.util.NoSuchElementException;


@Controller
@RequestMapping(value = "/main-user-page")
public class UserController {

    UserDB userDB;

    @GetMapping
    public ModelAndView mainPage() {

        ModelAndView modelAndView = new ModelAndView();
        setUserForTable(modelAndView);
        modelAndView.setViewName("/mainPage.jsp");
        return setUserForTable(modelAndView);
    }


    @PostMapping
    public ModelAndView create(@RequestParam("name") String name,
                               @RequestParam("age") String age,
                               @RequestParam("idUser") String id) {
        if (id.isEmpty()) {//проверяю если id пустой то это запрос на удаление, а не создание
            try {
                userDB.inCor(age, name);//простой метод проверяющий на пустые \ некорректные значения
            } catch (Exception e) {
                ModelAndView modelAndView = new ModelAndView();
                modelAndView.setViewName("/error.jsp");
                return modelAndView;
            }
            userDB.createUser(Integer.parseInt(age), name);
            return mainPage();

        }else{
            deleteUser(id);// в этом месте я перенаправялю в метод deleteUser в ээтом мой вопрос и заключается, можно ли так 
        }
        return mainPage();
    }

    @DeleteMapping
    public ModelAndView deleteUser( String id){
        try {
            userDB.inCorId(id);
        } catch (Exception e) {
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.setViewName("/error.jsp");
            return modelAndView;
        }
        ModelAndView modelAndView = new ModelAndView();
        userDB.delete(id);

        return mainPage();
    }



    public ModelAndView setUserForTable(ModelAndView modelAndView) {
        for (long i = 1; i < 11; i++) {
            try {
                modelAndView.addObject(
                        "user" + i,
                        userDB.findByIdUser(i).getId() +
                                "- " + userDB.findByIdUser(i).getName() +
                                " " + userDB.findByIdUser(i).getAge()
                );
            } catch (NoSuchElementException e) {
                modelAndView.addObject("user" + i, "тут будет ваш пользователь");
            }
        }
        return modelAndView;
    }

    @Autowired
    public void setUserDB(UserDB userDB) {
        this.userDB = userDB;
    }
}

文件:

package com.example.module11.userController;

import com.example.module11.entity.User;

import com.example.module11.repo.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;


import java.util.Optional;

@Service
@EnableJpaRepositories(basePackages = "com.example.module11.repo")
public class UserDB {



    UserRepository repository;
    private static int count = 1;


    public User findByIdUser(Long id) {
        Optional<User> user = repository.findById(id);
        User newUser = user.get();
        return newUser;
    }


    public User createUser(int age, String name) {
        try {
            User user = new User();
            user.setName(name);
            user.setAge(age);
            user.setId((long) count);
            count++;
            repository.save(user);
            return user;

        } catch (Exception e) {
            System.out.println("err to create user: " + e);
            return null;
        }

    }

    public void delete(String id) {
        repository.deleteUserById(Long.parseLong(id));
    }

    public void inCor(String age, String name) throws RuntimeException {
        if (age.isEmpty() || name.isEmpty()) {
            throw new RuntimeException("age or name empty");
        }

    }

    public void inCorId(String id) throws RuntimeException {
        if (id.isEmpty()) {
            throw new RuntimeException("id empty");
        }
    }

    public int getCountUser() {
        return count;
    }

    @Autowired
    public void setRepository(UserRepository repository) {
        this.repository = repository;
    }
}

文件:

package com.example.module11;

import com.example.module11.repo.UserRepository;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Hello world!
 *
 */
@SpringBootApplication

public class App 
{
    public static void main( String[] args ) {
        SpringApplication.run(App.class,args);






    }
}

应用程序.yml

spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/postgres
    username: root
    password: 1234
  jpa:
    hibernate:
      ddl-auto: create-drop
    properties:
      hibernate:
        dialect: org.hibernate.dialect.PostgreSQLDialect

XML:

<?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/>
  </parent>
  <groupId>com.example</groupId>
  <artifactId>module11</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    <java.version>17</java.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.postgresql</groupId>
      <artifactId>postgresql</artifactId>
      <version>${postgresql.version}</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/javax.persistence/javax.persistence-api -->
    <dependency>
      <groupId>javax.persistence</groupId>
      <artifactId>javax.persistence-api</artifactId>
      <version>2.2</version>
    </dependency>
</dependencies>

  

</project>

错误:

org.hibernate.metamodel.model.domain.internal.MappingMetamodelImpl.managedType(MappingMetamodelImpl.java:463) ~[hibernate-core-6.3.1.Final.jar:6.3.1.Final]
    at org.hibernate.metamodel.model.domain.internal.MappingMetamodelImpl.managedType(MappingMetamodelImpl.java:97) ~[hibernate-core-6.3.1.Final.jar:6.3.1.Final]
    at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:82) ~[spring-data-jpa-3.2.0.jar:3.2.0]
    at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getEntityInformation(JpaEntityInformationSupport.java:69) ~[spring-data-jpa-3.2.0.jar:3.2.0]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:246) ~[spring-data-jpa-3.2.0.jar:3.2.0]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:211) ~[spring-data-jpa-3.2.0.jar:3.2.0]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:194) ~[spring-data-jpa-3.2.0.jar:3.2.0]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:81) ~[spring-data-jpa-3.2.0.jar:3.2.0]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:317) ~[spring-data-commons-3.2.0.jar:3.2.0]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet$5(RepositoryFactoryBeanSupport.java:279) ~[spring-data-commons-3.2.0.jar:3.2.0]
    at org.springframework.data.util.Lazy.getNullable(Lazy.java:135) ~[spring-data-commons-3.2.0.jar:3.2.0]
    at org.springframework.data.util.Lazy.get(Lazy.java:113) ~[spring-data-commons-3.2.0.jar:3.2.0]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:285) ~[spring-data-commons-3.2.0.jar:3.2.0]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:132) ~[spring-data-jpa-3.2.0.jar:3.2.0]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1822) ~[spring-beans-6.1.1.jar:6.1.1]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1771) ~[spring-beans-6.1.1.jar:6.1.1]
    ... 44 common frames omitted


Process finished with exit code 1

我不认为发布 html 页面有什么意义,因为它们没有问题。一切都在 SQLite 上运行。我怎样才能找到错误所在?

java spring spring-data
1个回答
0
投票

我发现了一个错误,我正在寻找的最愚蠢的东西,我从javax导入,但我需要从jacarta......抱歉

© www.soinside.com 2019 - 2024. All rights reserved.