“Spring Boot数据未在PostgreSQL数据库中持久化。”

huangapple go评论62阅读模式
英文:

Spring Boot data not persisting in PostgreSQL database

问题

I have a PostgreSQL table created with a query that automatically inserts a timestamp when a new entry is added to the database. I am trying to store data in the table using a Spring Boot application, but the data is not being stored based on the numb column, instead it seems to be using the ID column by default. How can I ensure that the data is stored in the table based on the numb column rather than the ID column?

This is my code:

// database query to create table

CREATE TABLE public.mydb 
(
	numb varchar(15) NOT NULL,
	"on_off" varchar(6) NOT NULL,
	"timestamp" varchar(20) NOT NULL DEFAULT EXTRACT(epoch FROM (now() AT TIME ZONE 'utc'::text))
);

// controller

   @PostMapping
    public ResponseEntity<String> createUser(@RequestBody User user) {
        System.out.println(Thread.currentThread().getId()+"-----------------------------------"+System.currentTimeMillis()+"----------"+"inside controller");
        CompletableFuture<User> future = userService.createUser(user);
        String locationUrl = "/status/" + UUID.randomUUID().toString(); // generate a unique URL for the status check
        HttpHeaders headers = new HttpHeaders();
        headers.setLocation(URI.create(locationUrl));
        return new ResponseEntity<>("", headers, HttpStatus.OK);
    }

// service

  @Override
    @Async("asyncExecutor")
    public CompletableFuture<User> createUser(User user) {
        System.out.println(Thread.currentThread().getId()+"-----------------------------------"+System.currentTimeMillis()+"-------------inside services");
        return CompletableFuture.completedFuture(userRepository.save(user));
    }

// model

import javax.persistence.*;

@Entity
@Table(name="mydb")
public class User {

    @Id
    @Column
    private String numb;
    @Column
    private String on_off;

    @Column(name = "timestamp")
    @GeneratedValue
    private String timestamp;


    public User(String numb, String on_off) {
        this.numb = numb;
        this.on_off = on_off;
    }


    public String getNumb() {
        return numb;
    }

    public void setNumb(String numb) {
        this.numb = numb;
    }

    public String getOn_Off() {
        return on_off;
    }

    public void setOn_Off(String on_off) {
        this.on_off = on_off;
    }

    public String getTimestamp() {
        return timestamp;
    }

The expectation is to get a solution for storing data in a PostgreSQL table based on a specific column(.i.e. numb) instead of the ID column in a Spring Boot application, which is not happening currently.

//output

21-----------------------------------1683463770492----------inside controller
34-----------------------------------1683463770495-------------inside services
Hibernate: select user0_.numb as numb1_0_0_, user0_.on_off as on_off2_0_0_, user0_.timestamp as timestamp3_0_0_ from mydb user0_ where user0_.numb=?
英文:

I have a PostgreSQL table created with a query that automatically inserts a timestamp when a new entry is added to the database. I am trying to store data in the table using a Spring Boot application, but the data is not being stored based on the numb column, instead it seems to be using the ID column by default. How can I ensure that the data is stored in the table based on the numb column rather than the ID column?

This is my code:

// database query to create table

CREATE TABLE public.mydb 
(
	numb varchar(15) NOT NULL,
	&quot;on_off&quot; varchar(6) NOT NULL,
	&quot;timestamp&quot; varchar(20) NOT NULL DEFAULT EXTRACT(epoch FROM (now() AT TIME ZONE &#39;utc&#39;::text))
);

// controller

   @PostMapping
    public ResponseEntity&lt;String&gt; createUser(@RequestBody User user) {
        System.out.println(Thread.currentThread().getId()+&quot;-----------------------------------&quot;+System.currentTimeMillis()+&quot;----------&quot;+&quot;inside controller&quot;);
        CompletableFuture&lt;User&gt; future = userService.createUser(user);
        String locationUrl = &quot;/status/&quot; + UUID.randomUUID().toString(); // generate a unique URL for the status check
        HttpHeaders headers = new HttpHeaders();
        headers.setLocation(URI.create(locationUrl));
        return new ResponseEntity&lt;&gt;(&quot;&quot;, headers, HttpStatus.OK);
    }

// service

  @Override
    @Async(&quot;asyncExecutor&quot;)
    public CompletableFuture&lt;User&gt; createUser(User user) {
        System.out.println(Thread.currentThread().getId()+&quot;-----------------------------------&quot;+System.currentTimeMillis()+&quot;-------------inside services&quot;);
        return CompletableFuture.completedFuture(userRepository.save(user));
    }

// model

import javax.persistence.*;

@Entity
@Table(name=&quot;mydb&quot;)
public class User {

    @Id
    @Column
    private String numb;
    @Column
    private String on_off;

    @Column(name = &quot;timestamp&quot;)
    @GeneratedValue
    private String timestamp;


    public User(String numb, String on_off) {
        this.numb = numb;
        this.on_off = on_off;
    }


    public String getNumb() {
        return numb;
    }

    public void setNumb(String numb) {
        this.numb = numb;
    }

    public String getOn_Off() {
        return on_off;
    }

    public void setOn_Off(String on_off) {
        this.on_off = on_off;
    }

    public String getTimestamp() {
        return timestamp;
    }

The expectation is to get a solution for storing data in a PostgreSQL table based on a specific column(.i.e. numb) instead of the ID column in a Spring Boot application, which is not happening currently.

//output

21-----------------------------------1683463770492----------inside controller
34-----------------------------------1683463770495-------------inside services
Hibernate: select user0_.numb as numb1_0_0_, user0_.on_off as on_off2_0_0_, user0_.timestamp as timestamp3_0_0_ from mydb user0_ where user0_.numb=?

答案1

得分: 1

数据库表必须有一个主键。

英文:

The database's table must have a primary key.

huangapple
  • 本文由 发表于 2023年5月7日 22:02:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76194392.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定