Spring Boot Jpa资料档案库saveAndFlush将时间戳返回为空

bqf10yzr  于 2023-01-13  发布在  Spring
关注(0)|答案(1)|浏览(174)

我有一个实体

@AllArgsConstructor
@NoArgsConstructor
@Setter
@Getter
@Entity
@Table(name = "t_org")
public class Organization {

  /**
   * id of the organization.
   */
  @Id
  @GeneratedValue(generator = "UUID")
  @Column(name = "c_id", columnDefinition = "BINARY(16) DEFAULT (UUID_TO_BIN(UUID(), TRUE))")
  private UUID id;

  /**
   * legal name of the organization.
   */
  @Column(name = "c_legal_name", nullable = false, unique = true)
  private String legalName;

  /**
   * alias name of the organization.
   */
  @Column(name = "c_alias", nullable = false, unique = true)
  private String alias;

  /**
   * timestamp when the organization was created.
   */
  @Column(name = "c_date", insertable = false, updatable = false,
      columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP",
      nullable = false)
  private Instant ts;
}

现在,当我从服务保存此实体时,以及当我想通过调用saveAndFlush(orgObject).getTs()访问时间戳时;
时间戳总是空的,但是我可以看到它被保存在数据库中,但是jpa没有返回它。
我试着用@CreationTimestamp注解它,但这将设置jvm的时间戳,我需要db的时间戳。

mqkwyuun

mqkwyuun1#

我试过了,这是获取时间戳。我创建了一个实体类,叫做post。并为它创建了API控制器和服务类。这是完整的代码。

    • 过帐实体类**
package com.project.demo.entity;

import java.time.Instant;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Entity
@Data
@Builder 
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "post")
public class Post {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id; 
    private String thePost; 
    private Instant creationTime; 
}
    • 服务等级**
package com.project.demo.service;

import java.time.Instant;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.project.demo.entity.Post;
import com.project.demo.repository.PostRepository;

import jakarta.transaction.Transactional;

@Service
public class PostService {
    @Autowired
    private PostRepository postRepository; 

    @Transactional
    public List<Post> getPosts(){
        return postRepository.findAll(); 
    }

    @Transactional
    public void savePost(Post post) {
        post.setCreationTime(Instant.now());
        postRepository.save(post);
    }
}
    • rest API的控制器类**
package com.project.demo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.project.demo.entity.Post;
import com.project.demo.service.PostService;

@RestController
@RequestMapping
public class Controller {
    @Autowired
    private PostService postService;

    @GetMapping("/posts")
    public List<Post> getPost() {
        return postService.getPosts();
    }

    @PostMapping("/posts")
    public String savePost(@RequestBody Post post) {
        post.setId(0);
        postService.savePost(post);
        return "Post saved";
    }
}
    • 应用程序.属性**
spring.datasource.name=test
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update

spring.datasource.url=jdbc:postgresql://localhost:5432/stackoverflow
spring.datasource.username=postgres
spring.datasource.password=123
spring.jpa.show-sql=true
    • 在postman中提取数据的截图**

The screenshot
希望这个有用。

相关问题