首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Spring巡检库保存不返回UUID字段

Spring巡检库保存不返回UUID字段
EN

Stack Overflow用户
提问于 2018-10-02 16:06:07
回答 1查看 4.7K关注 0票数 2

我试图用纯JPA保存一个实体,并且在保存时没有返回Postgres UUID字段。默认情况下,UUID字段使用public.uuid_generate_v1mc()自动生成.我使用postgres作为数据库,由于某种原因,在jpa中进行保存时创建的默认值不会被返回。我尝试过添加一个@Generated字段并将其设置为auto,但这没有任何作用。

我做错什么了吗?

数据库创建语句

代码语言:javascript
运行
复制
CREATE TABLE usermgmt.test
(
  id SERIAL PRIMARY KEY,
  active boolean NOT NULL DEFAULT false,
  created timestamp without time zone NOT NULL DEFAULT now(),
  updated timestamp without time zone NOT NULL DEFAULT now(),
  description character varying,
  name character varying NOT NULL,
  external_id bigint UNIQUE ,
  uuid uuid DEFAULT public.uuid_generate_v1mc(),
  alias character varying NOT NULL UNIQUE,
  code character varying NOT NULL UNIQUE
);

实体

代码语言:javascript
运行
复制
@SuppressWarnings("serial")
@Entity(name = "managementTest")
@Cacheable
@Table(schema = "usermgmt", name = "test")
public class Test extends BaseEntity implements Serializable {

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false)
    private String name;

    @Column(nullable = false)
    private String description;

    @Column(nullable = false, insertable = false, updatable = false)
    private UUID uuid;

    @Column(nullable = false, unique = true)
    private String alias;

    @Column(nullable = false, unique = true)
    private String code;

    public Long getId() {
        return id;
    }

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

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return
     */
    public String getDescription() {
        return description;
    }

    /**
     * @param description
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * @return the uuid
     */
    public UUID getUuid() {
        return uuid;
    }

    public void setUuid(UUID uuid) {
        this.uuid = uuid;
    }

    public String getAlias() {
        return alias;
    }

    public void setAlias(String alias) {
        this.alias = alias;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }
}

基本实体

代码语言:javascript
运行
复制
@MappedSuperclass
public abstract class BaseEntity {
    private LocalDateTime created;
    private LocalDateTime updated;
    @NotNull
    private Boolean active;

    public BaseEntity() {
        this.active = Boolean.TRUE;
    }

    @PrePersist
    @PreUpdate
    void setDates() {
        if (this.created == null) {
            this.created = LocalDateTime.now();
        }

        this.updated = LocalDateTime.now();
    }

    public Boolean getActive() {
        return this.active;
    }

    public void setActive(Boolean active) {
        this.active = active;
    }

    public LocalDateTime getCreated() {
        return this.created;
    }

    public LocalDateTime getUpdated() {
        return this.updated;
    }

    public void setUpdated(LocalDateTime updated) {
        this.updated = updated;
    }

    public void touch() {
        this.updated = LocalDateTime.now();
    }
}

Crud

代码语言:javascript
运行
复制
@Repository("managementTestCrudRepository")
public interface TestCrudRepository extends JpaRepository<Test, Long> {
    Test findByCode(String code);
    Test findByUuid(UUID uuid);
    Test findByAlias(String alias);
    List<Test> findByActive(Boolean active);
}

这是我用来保存实体的方法。

代码语言:javascript
运行
复制
@PutMapping
public Test putByJson(@RequestBody String json) {
    return testCrudRepository.save(new Gson().fromJson(json, Test.class));
}

对象,以邮递员的形式返回。

代码语言:javascript
运行
复制
{
    "created": {
        "month": "OCTOBER",
        "year": 2018,
        "dayOfYear": 275,
        "hour": 11,
        "minute": 9,
        "nano": 325000000,
        "second": 52,
        "dayOfMonth": 2,
        "dayOfWeek": "TUESDAY",
        "monthValue": 10,
        "chronology": {
            "id": "ISO",
            "calendarType": "iso8601"
        }
    },
    "updated": {
        "month": "OCTOBER",
        "year": 2018,
        "dayOfYear": 275,
        "hour": 11,
        "minute": 9,
        "nano": 329000000,
        "second": 52,
        "dayOfMonth": 2,
        "dayOfWeek": "TUESDAY",
        "monthValue": 10,
        "chronology": {
            "id": "ISO",
            "calendarType": "iso8601"
        }
    },
    "active": true,
    "id": 2,
    "externalId": null,
    "name": "test1",
    "description": "test1",
    "uuid": null,
    "alias": "test1",
    "code": "test1"
}
EN

回答 1

Stack Overflow用户

发布于 2018-10-10 19:54:20

因此,我发现您必须使用@Generated(GenerationTime.INSERT)注释

实际上,我使用的是@GeneratedValue,为此我尝试了所有不同的值,但它们都没有起作用。最后,在使用@Generated(GenerationTime.INSERT)之后,它工作得很好。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52612241

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档