0

Problem: a new table is created once when I make a post request through The bash console. The rest of the queries go to the new table. Than he does not like those databases which are available. As I understand - they just don't know, but I don't know how to direct it in the right. Although all variables are also named.

A problem was found created due to an Entity annotation in the Message class. Please tell me how to make it added to an existing table, tried @Table(name = "ApiTable") to an existing one, and it generates a new api_table.. Also don't quite understand what needs to be added/changed to accept json post requests.

Application

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

@SpringBootApplication
@EnableJpaRepositories("com.example.api")
public class Application {

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

MainController

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller    
@RequestMapping(path="/demo") /
public class MainController {
    @Autowired
    private UserRepository TestApi;

    @PostMapping(path="/add")
    public @ResponseBody String addNewUser (@RequestParam String name
            , @RequestParam String email) {
        Message n = new Message();
        n.setName(name);
        n.setEmail(email);
        TestApi.save(n);
        return "Saved";
    }

    @GetMapping(path="/all")
    public @ResponseBody Iterable<Message> getAllUsers() {
        return TestApi.findAll();
    }
}

Message

import javax.persistence.*;

@Entity
public class Message {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;
    private String name;
    private String email;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

UserRepository

import org.springframework.data.repository.CrudRepository;

public interface UserRepository extends CrudRepository<Message, Integer> {

}

application.properties

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost/Test?useUnicode=true&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
Blacit
  • 203
  • 3
  • 12
  • Do you want Hibernate to update your database schema? What's the name of the existing table? – Thomas Oct 28 '19 at 13:24
  • Yes, I want Hibernate to update an already existing database. Its name is TestApi, and the name of the table is ApiTable – Blacit Oct 28 '19 at 13:32
  • "Its name is TestApi" - I'm not sure what "it" is since I only asked about the table's name and you stated that one is named "ApiTable". – Thomas Oct 28 '19 at 13:37
  • Sorry, the table name is ApiTable – Blacit Oct 28 '19 at 13:39
  • I see. The problem seems to be Spring Boot's default naming strategy which you'd have to replace. Have a look here for some info: https://stackoverflow.com/questions/39162976/hibernate-naming-strategy-changing-table-names – Thomas Oct 28 '19 at 14:26
  • Thank you very much. Let me mark your answer as the best. – Blacit Oct 30 '19 at 11:11
  • I'll add an answer for you to accept then :) – Thomas Oct 30 '19 at 11:14

1 Answers1

0

The problem seems to be Spring Boot's default naming strategy which you'd have to replace. Spring Boot's default naming strategy now seems to include converting camelCase to snake_case so you need to choose a different one (or implement your own).

Here's some more info on the topic: Hibernate naming strategy changing table names

Thomas
  • 87,414
  • 12
  • 119
  • 157