5

I created a small social media app with Spring Boot and in development I used H2 local database and it worked just fine. Now I try to connect it to a Heroku PostgreSQL database and I get the error in the title. Any ideas why? I suspected a wrong dialect, but everywhere I checked I should use org.hibernate.dialect.PostgreSQLDialect as I do right now. Otherwise probably there're some mishaps with the Entities or the Mapping? Even if yes, I just can't figure what and how to amend them. I'm quite new to database stuff, so please don't judge me if it's some stupid mistake.

Entity class : User

package com.schabby.socialplatform.models;

import com.fasterxml.jackson.annotation.JsonIgnore;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;

@Entity
@SequenceGenerator(name="seq", initialValue=1, allocationSize=100)
public class User implements Serializable {
    
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY, generator="seq")
    private Long id;
    
    private String username;
    private String password;
    private String name;
    private int age;
    private String picture;
    private boolean online;
    
    @JsonIgnore
    @OneToMany(mappedBy = "User")
    private Collection<Post> posts = new ArrayList<Post>();

    public User() {
    }

    public User(String username, String password, String name, int age, String picture, boolean online) {
        this.username = username;
        this.password = password;
        this.name = name;
        this.age = age;
        this.picture = picture;
        this.online = online;
    }
    
    public Long getId() {
        return id;
    }

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

    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 String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public Collection<Post> getPosts() {
        return posts;
    }

    public void setPosts(Collection<Post> posts) {
        this.posts = posts;
    }

    public boolean isOnline() {
        return online;
    }

    public void setOnline(boolean online) {
        this.online = online;
    }

    public String getPicture() {
        return picture;
    }

    public void setPicture(String picture) {
        this.picture = picture;
    }
    
    
    
} 

Entity class: Post

package com.schabby.socialplatform.models;

import com.fasterxml.jackson.annotation.JsonIgnore;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;

@Entity
public class Post implements Serializable {
    
    @Id
    @GeneratedValue
    private long id;
    
    private String text;
    
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date date;
    
    @JsonIgnore
    @ManyToOne
    @JoinColumn(name="User_id")
    private User User;
    
    
    public Post() {
    }

    public Post(String text, Date date, User user) {
        this.text = text;
        this.date = date;
        this.User = user;
    }

    public long getId() {
        return id;
    }

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

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public User getUser() {
        return User;
    }

    public void setUser(User user) {
        this.User = user;
    }
}

My 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 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>2.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.schabby</groupId>
    <artifactId>socialplatform</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>socialplatform</name>
    <description>Social Media Platform</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
                <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>
                <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.1</version>
        </dependency>
                <dependency>
                    <groupId>com.h2database</groupId>
                    <artifactId>h2</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.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
                <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-data-jpa</artifactId>
                </dependency>
     <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.8.6</version>
      <type>jar</type>
     </dependency>
    </dependencies>

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

</project>

application.properties :

spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB

spring.datasource.url=jdbc:postgresql://ec2-54-217-206-236.eu-west-1.compute.amazonaws.com:5432/d5rk5sir0732uc?sslmode=require
spring.datasource.username=
spring.datasource.password=
spring.datasource.driverClassName=org.postgresql.Driver
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=create

Thank you for every suggestions!

2 Answers 2

10

I found an answer here on Stackoverflow!

The problem was that some names, like my table's name: 'user' is reserved for PostgreSQL. So, changing the name of it solved my issue.

Here's the original answer: https://stackoverflow.com/a/56206827/13304501

Sign up to request clarification or add additional context in comments.

2 Comments

Well, I spent few hours dealing with this issue. Thank you so much for posting this. This totally solved my problem. If you most use user as a table name you can mark your entity with: @Table(name="\"user\"") and that will do the trick.
Holy jesus, another victim here. god
2

I had also the same error, and I had User entity class gave annotation @Table, but I saw that I should declare the name of table and I made it the same with my class name as @Table(name ="user") but user is used as reserved keyword in PostgreSQL, I changed it into @Table(name ="users"). Then it worked for me. The solution found from here, Executing DDL Commands in Spring-Boot I hope this helps you too.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.