spring-data-jpa 在SINGLE_TABLE层次结构的子类中非法使用@Table

utugiqy6  于 2022-11-10  发布在  Spring
关注(0)|答案(2)|浏览(347)

我是Spring的新手。我试着用SOLID技术创建一个项目,但是遇到了这个错误。继承在我的应用程序中不起作用。为什么继承不能正常工作?Spring允许继承吗?

错误文本:

2021-06-23 23:54:08.928  INFO 12984 --- [  restartedMain] hrms.northwind.NorthwindApplication      : Starting NorthwindApplication using Java 15.0.2 on DESKTOP-87K40S0 with PID 12984 (C:\Users\90553\Desktop\Eclipse Projects\HRMS Project\target\classes started by 90553 in C:\Users\90553\Desktop\Eclipse Projects\HRMS Project)
2021-06-23 23:54:08.929  INFO 12984 --- [  restartedMain] hrms.northwind.NorthwindApplication      : No active profile set, falling back to default profiles: default
2021-06-23 23:54:08.978  INFO 12984 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2021-06-23 23:54:08.978  INFO 12984 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2021-06-23 23:54:09.465  INFO 12984 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2021-06-23 23:54:09.512  INFO 12984 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 40 ms. Found 2 JPA repository interfaces.
2021-06-23 23:54:10.040  INFO 12984 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-06-23 23:54:10.052  INFO 12984 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-06-23 23:54:10.052  INFO 12984 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.46]
2021-06-23 23:54:10.130  INFO 12984 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-06-23 23:54:10.130  INFO 12984 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1152 ms
2021-06-23 23:54:10.203  INFO 12984 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2021-06-23 23:54:10.367  INFO 12984 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2021-06-23 23:54:10.458  INFO 12984 --- [  restartedMain] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2021-06-23 23:54:10.502  INFO 12984 --- [  restartedMain] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.32.Final
2021-06-23 23:54:10.619  INFO 12984 --- [  restartedMain] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2021-06-23 23:54:10.721  INFO 12984 --- [  restartedMain] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
2021-06-23 23:54:11.047  WARN 12984 --- [  restartedMain] org.hibernate.cfg.AnnotationBinder       : HHH000139: Illegal use of @Table in a subclass of a SINGLE_TABLE hierarchy: hrms.northwind.entities.concretes.Employer
2021-06-23 23:54:11.049  WARN 12984 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is java.lang.ClassCastException: class org.hibernate.mapping.SingleTableSubclass cannot be cast to class org.hibernate.mapping.RootClass (org.hibernate.mapping.SingleTableSubclass and org.hibernate.mapping.RootClass are in unnamed module of loader 'app')
2021-06-23 23:54:11.050  INFO 12984 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2021-06-23 23:54:11.053  INFO 12984 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.
2021-06-23 23:54:11.057  INFO 12984 --- [  restartedMain] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2021-06-23 23:54:11.066  INFO 12984 --- [  restartedMain] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-06-23 23:54:11.084 ERROR 12984 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed

雇主类别:

package hrms.northwind.entities.concretes;

import javax.persistence.Column;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import javax.persistence.Table;

import lombok.Data;
@Entity(name="Employers")

@Table(name="Employers")
@Data
public class Employer extends Person {
    @Id
    @GeneratedValue
    @Column(name="employer_id")
    private int employerId;

    @Column(name="company_name")
    private String company_name;

    @Column(name="website")
    private String website;

    @Column(name="phone_number")
    private String phone_number;

    @Column(name="password")
    private String password;

    public Employer(int personId, String name, String surname, String email, int employerId, String company_name, String website,
            String phone_number, String password) {
        super(personId, name, surname, email);
        this.employerId = employerId;
        this.company_name = company_name;
        this.website = website;
        this.phone_number = phone_number;
        this.password = password;
    }

}

雇主刀:

package hrms.northwind.dataAccess.abstracts;

import org.springframework.data.jpa.repository.JpaRepository;

import hrms.northwind.entities.concretes.Employer;

public interface EmployerDao extends JpaRepository<Employer,Integer>{

}

雇主服务:

package hrms.northwind.business.abstracts;

import java.util.List;

import org.springframework.stereotype.Service;

import hrms.northwind.entities.concretes.Employer;
@Service
public interface EmployerService {
    List<Employer> getAll();

}

雇主财务总监:

package hrms.northwind.api.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import hrms.northwind.business.abstracts.EmployerService;
import hrms.northwind.entities.concretes.Employer;

@RestController
@RequestMapping("/api/employers")
public class EmployerController {
    private EmployerService employerService;
    @Autowired
    public EmployerController(EmployerService employerService) {
        super();
        this.employerService = employerService;
    }
    @GetMapping("/getall")
    public List<Employer> getAll(){
        return this.employerService.getAll();
    }

}

雇主表:
Employer Table
有什么帮助吗?

t1qtbnec

t1qtbnec1#

也许这个答案已经晚了
我认为问题出在@Table上。当你想指定一个实体时,那个注解是可选的。
您可以找到更好的记录答案here

yvfmudvl

yvfmudvl2#

您可以将其添加到雇主类中。

@Inheritance(strategy = InheritanceType.JOINED)
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@PrimaryKeyJoinColumn(name="employer_id",referencedColumnName = "person_id")

相关问题