java Sping Boot 数据未在PostgreSQL数据库中持久化

46qrfjad  于 2023-05-12  发布在  Java
关注(0)|答案(1)|浏览(91)

我创建了一个PostgreSQL表,其中包含一个查询,当向数据库添加新条目时,该查询会自动插入一个时间戳。我尝试使用Sping Boot 应用程序在表中存储数据,但数据不是基于numb列存储的,而是默认情况下使用ID列。如何确保数据存储在基于数字列而不是ID列的表中?
这是我的代码:
//数据库查询以创建表

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

//控制器

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

//服务

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

期望得到一个解决方案,用于基于特定列(即numb),而不是Sping Boot 应用程序中的ID列,这是目前没有发生的。
//输出

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

相关问题