简体   繁体   中英

Spring JPA returns empty array from PostgreSQL

I'm new to using Spring MVC and JPA. I have a Postgresql database in which I create two tables: author and 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)

);

Then I want to map them to my code in Spring and show them. The issue is that even though I do it in the same way, Author is displayed correctly ([some JSON]) while when I want to see userType I receive an empty array ([]). I have no idea why it's happening and would be grateful for help.

Author Model

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;
    }
}

UserType model

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;
    }
}

AUTHOR CONTROLLER

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();
    }
}

USERTYPE CONTROLLER

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();
    }
}

Author Repository

package com.shareabook.repository;

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

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

UserType repository

package com.shareabook.repository;

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

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

And the main class

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);
    }

}

My pom.xml file

<?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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM