【问题标题】:Spring JPA returns empty array from PostgreSQLSpring JPA 从 PostgreSQL 返回空数组
【发布时间】:2017-10-20 15:43:58
【问题描述】:

我是使用 Spring MVC 和 JPA 的新手。我有一个 Postgresql 数据库,在其中创建了两个表:作者和 userType。

CREATE TABLE UserType (

                idUserType INTEGER NOT NULL,

                userType VARCHAR NOT NULL,

                CONSTRAINT pkusertype PRIMARY KEY (idUserType)

);

CREATE TABLE Author (

                idAuthor INTEGER NOT NULL DEFAULT nextval('author_idauthor_seq'),

                lastName VARCHAR NOT NULL,

                firstName VARCHAR NOT NULL,

                CONSTRAINT pk_author PRIMARY KEY (idAuthor)

);

然后我想将它们映射到我在 Spring 中的代码并显示它们。问题是,即使我以相同的方式执行此操作,作者也会正确显示([某些 JSON]),而当我想查看 userType 时,我会收到一个空数组([])。我不知道为什么会发生这种情况,非常感谢您的帮助。

作者模型

package com.shareabook.model;

import javax.persistence.*;
@Entity
@Table(name = "author")
public class Author {

    @Id
    @GeneratedValue
    @Column(name = "idauthor")
    private Integer id;
    @Column(name = "lastname")
    private String lastName;
    @Column(name = "firstname")
    private String firstName;

    public Integer getId() {
        return id;
    }

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

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
}

用户类型模型

package com.shareabook.model;

import javax.persistence.*;
@Entity
@Table(name = "UserType ")
public class UserType {
    @Id
    @GeneratedValue
    @Column(name = "idusertype")
    private Integer id;
    @Column(name = "usertype")
    private String userType;

    public Integer getId() {
        return id;
    }

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

    public String getUserType() {
        return userType;
    }

    public void setUserType(String userType) {
        this.userType = userType;
    }
}

作者控制器

package com.shareabook.controller;

import com.shareabook.model.Author;
import com.shareabook.repository.AuthorRepository;
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 java.util.List;
@RestController
@RequestMapping(value = "/rest/author")
public class AuthorController {
    @Autowired
    AuthorRepository authorRepository;

    @GetMapping(value = "/all")
    public List<Author> getAll(){
        return authorRepository.findAll();
    }
}

用户类型控制器

package com.shareabook.controller;


import com.shareabook.model.UserType;
import com.shareabook.repository.UserTypeRepository;
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 java.util.List;

@RestController
@RequestMapping(value = "/rest/usertype")
public class UserTypeController {
    @Autowired
    UserTypeRepository userTypeRepository;

    @GetMapping(value = "/all")
    private List<UserType> getAll(){
        return userTypeRepository.findAll();
    }
}

作者资料库

package com.shareabook.repository;

import com.shareabook.model.Author;
import org.springframework.data.jpa.repository.JpaRepository;

public interface AuthorRepository extends JpaRepository<Author, Integer>{
}

用户类型存储库

package com.shareabook.repository;

import com.shareabook.model.UserType;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserTypeRepository extends JpaRepository<UserType, Integer>{
}

还有主类

package com.shareabook;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@EnableJpaRepositories(basePackages = "com.shareabook.repository")
@SpringBootApplication
public class ShareabookApplication {

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

}

我的 pom.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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.shareabook</groupId>
    <artifactId>shareabook</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>shareabook</name>
    <description>Basic project for application Share A Book</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>4.2.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.4-1201-jdbc4</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>1.5.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.16</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

Application.properties

spring.datasource.dbcp2.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/shareabookthesis
spring.datasource.username=postgres
spring.datasource.password=wiktoria2
spring.jpa.hibernate.ddl-auto=update
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true

【问题讨论】:

  • 能否请您删除您的表UserTypeAuthor,然后重新启动您的应用程序。由于您已使用 @Entity 标记模型,它将自动在 db 中创建您的表
  • @AjitSoman 我按照你的建议做了,但结果没有改变。令我惊讶的是,作者的一切都正确显示。问题仅在于 userType。
  • @AjitSoman 我应该添加 Tomcat 日志吗?
  • 无需发布tomcat日志
  • @AjitSoman 任何其他想法可能有什么问题?总而言之,数据库大约有 12 个表,其中 4 个的行为类似于 userType - 我只收到空数组。

标签: java spring postgresql spring-mvc jpa


【解决方案1】:

Postgres 中的数据库名称应为小写,表名不应为 CamelCase。表名应为 usertype 而不是 UserType。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-07-21
    • 1970-01-01
    • 1970-01-01
    • 2016-07-01
    • 1970-01-01
    • 2020-04-03
    • 1970-01-01
    • 2017-05-08
    相关资源
    最近更新 更多