1

I am working on a Spring Boot application trying to use Spring Data JPA and Hibernate but I am finding some difficulties trying to insert a new record into an example table.

This is my application pom.xml file content:

<?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.5.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.easydefi</groupId>
    <artifactId>GET-USER-WS</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>GET-USER-WS</name>
    <description>Microservice that retrieves users from DB</description>
    <properties>
        <java.version>11</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.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        
        <!-- JPA -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        
        <!-- Lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

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

</project>

This is my application.properties file content:

spring.datasource.url = jdbc:postgresql://172.21.0.2:5432/EasyDefiDB
spring.datasource.username = postgres
spring.datasource.password = MY_PSWD
spring.datasource.driver-class-name = org.postgresql.Driver
spring.jpa.hibernate.ddl-auto = none
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true

Then into my Postgres I created a DB named EasyDefiDB containing this very simple Example table that I am using to perform some experiment:

CREATE TABLE IF NOT EXISTS public."Example"
(
    id bigint NOT NULL,
    test character varying(50) COLLATE pg_catalog."default" NOT NULL,
    CONSTRAINT "Example_pkey" PRIMARY KEY (id)
)

So I created this Example entity class:

package com.easydefi.users.entity;

import java.io.Serializable;
import java.util.Date;

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

import lombok.Data;

@Entity
@Table(name = "Example")
@Data
public class Example implements Serializable {

    private static final long serialVersionUID = -2878723722630662986L;
    
    @Id
    @Column(name = "id")
    private int id;
    
    @Column(name = "test")
    private String test;
    
    

    public Example(String test) {
        super();
        this.test = test;
    }



    public Example() {
        super();
    }
    

}

Then I created this ExampleRepository extenting JpaRepository interface:

public interface ExampleRepository extends JpaRepository<Example, Integer> {

}

At the moment this interface is empty because I am only trying to save a new record using the JpaRepository save() method.

Finnally I created this unit test class in order to test the saving behavior:

@SpringBootTest()
@ContextConfiguration(classes = GetUserWsApplication.class)
@TestMethodOrder(OrderAnnotation.class)
public class ExampleRepositoryTest {
    
    @Autowired
    private ExampleRepository exampleRepository;
    
    @Test
    @Order(1)
    public void TestInsertExample()
    {
        Example example = new Example("TEST");
        
        exampleRepository.save(example);
        
        assertTrue(true);
        
    }

}

The problem is that running this method, when the save() method is called it give me the following error in my stacktrace:

2021-11-03 17:10:59.059  INFO 15054 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2021-11-03 17:10:59.416  WARN 15054 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2021-11-03 17:11:00.453  INFO 15054 --- [           main] c.e.users.GetUserWsApplicationTests      : Started GetUserWsApplicationTests in 5.067 seconds (JVM running for 6.686)
2021-11-03 17:11:00.812  INFO 15054 --- [           main] o.s.t.c.support.AbstractContextLoader    : Could not detect default resource locations for test class [com.easydefi.users.tests.RepositoryTests.ExampleRepositoryTest]: no resource found for suffixes {-context.xml, Context.groovy}.
2021-11-03 17:11:00.818  INFO 15054 --- [           main] .b.t.c.SpringBootTestContextBootstrapper : Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.event.ApplicationEventsTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener]
2021-11-03 17:11:00.818  INFO 15054 --- [           main] .b.t.c.SpringBootTestContextBootstrapper : Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@703eead0, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@674fd531, org.springframework.test.context.event.ApplicationEventsTestExecutionListener@7f53b345, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@76ee7301, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@71817f66, org.springframework.test.context.support.DirtiesContextTestExecutionListener@68feca3a, org.springframework.test.context.transaction.TransactionalTestExecutionListener@ad585fb, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@fa689db, org.springframework.test.context.event.EventPublishingTestExecutionListener@75a6bd06, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@6b170692, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@4d4bac56, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@76980c75, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@3696d12d, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@656672fb, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener@75df4b1d]
Hibernate: 
    insert 
    into
        example
        (test, id) 
    values
        (?, ?)
2021-11-03 17:11:09.867  WARN 15054 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 42P01
2021-11-03 17:11:09.869 ERROR 15054 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : ERROR: relation "example" does not exist
  Position: 13

So basically the problem seems that, for some reason that I cannot uderstand, it is trying to insert into the example table and not into the Example (as it is defined in my DB, with the first uppercase char).

Renaming my table from Example to example it works fine, the new record is correctly inserted into this table.

But why? How can I change this behaviour allowing me to correctly insert into a table starting with an uppercase letter?

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • Hi have you checked: https://stackoverflow.com/questions/54385462/hibernate-postgres-table-and-column-name-sensitive-issue? You can also quote your tablename in your Entity as `@Table(name = "\"Example\"")` for a quick workaround – pleft Nov 03 '21 at 16:32
  • 1
    This answer could help : https://stackoverflow.com/questions/28571848/spring-boot-jpa-insert-in-table-with-uppercase-name-with-hibernate – Harry Coder Nov 03 '21 at 16:33
  • @pleft quoting it is giving me the exact same problem – AndreaNobili Nov 03 '21 at 16:36

0 Answers0