更改布尔值 Spring Data JPA

huangapple 未分类评论47阅读模式
英文:

change boolean value Spring Data JPA

问题

我正在编写一段简单的代码,在这段代码中,我需要在数据库表中更改布尔值,而不是删除整行数据。我用注释标记了适当的字段:

@Column(name = "exist", columnDefinition = "boolean default true")

但是,当我在数据库中保存一个新实例时,它没有标记为true/false。然后我直接在字段中设置了默认值:

private Boolean exist = true;

它开始标记我的实例,但是当我尝试更改值时,在两种情况下都不起作用(在第一种情况下,我无法将null更改为false,在第二种情况下,无法将true更改为false)。

我使用的是PostgreSQL数据库,我从官方文档中获取了解决方案Default Column Values in JPA

我的实体类:

private long id;
private String colour;
private Boolean exist = true;

public Colour() {
}

@Id
@Column(name = "id", nullable = false, unique = true)
@GeneratedValue(strategy = GenerationType.IDENTITY)
public long getId() {
    return id;
}

@Column(name = "colour", unique = true)
public String getColour() {
    return colour;
}

@Column(name = "exist", columnDefinition = "boolean default true")
public Boolean getExist() {
    return exist;
}

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

public void setColour(String colour) {
    this.colour = colour;
}

public void setExist(Boolean exist) {
    this.exist = exist;
}

我的删除方法:

@Override
public void deleteColour(long id) {
    Colour colour = getOne(id);
    try {
        colour.setExist(false);
        colourRepository.save(colour);
    } catch (NullPointerException e) {
        System.out.println("delete is not possible: colour doesn't exist");
    }
}

运行deleteColour()方法后的"colours"表格:使用注释的情况

运行deleteColour()方法后的"colours"表格:使用字段默认值的情况

谢谢您的时间!

英文:

I'm writing a simple code, where I need to change a boolean value instead delete whole row in my table in data base. I marked appropriate field with annotation:
" columnDefinition = " boolean default true ",

but when i saved a new instance in data base, it hasn't mark true/false on it. Then I put default value direct in field: " private Boolean exist = true; "

it start marking my instances, but when i try to change the value, it isn't work in both cases. (in first case I can't change null to false, in second case true to false)

I use PostgreSQL, I took my solution from official documentation Default Column Values in JPA

my entity class:

private long id;
private String colour;
private Boolean exist = true;

public Colour() {
}

@Id
@Column(name = "id", nullable = false, unique = true)
@GeneratedValue(strategy = GenerationType.IDENTITY)
public long getId() {
    return id;
}

@Column(name = "colour", unique = true)
public String getColour() {
    return colour;
}

@Column(name = "exist", columnDefinition = "boolean default true")
public Boolean getExist() {
    return exist;
}

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

public void setColour(String colour) {
    this.colour = colour;
}

public void setExist(Boolean exist) {
    exist = exist;
}

my delete method:

 @Override
public void deleteColour(long id) {
    Colour colour = getOne(id);
    try {
        colour.setExist(false);
        colourRepository.save(colour);
    } catch (NullPointerException e) {
        System.out.println("delete is not possible: colour doesn't exist");
    }
}

table "colours" after running method deleteColour(): in case of annotation

table "colours" after running method deleteColour(): in case of field default

thank you for your time!

huangapple
  • 本文由 发表于 2020年7月24日 04:40:14
  • 转载请务必保留本文链接:https://java.coder-hub.com/63062799.html
匿名

发表评论

匿名网友

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

确定