英文:
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,
"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=?
答案1
得分: 1
数据库表必须有一个主键。
英文:
The database's table must have a primary key.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论