如何在列值为空时为列变量分配默认值

huangapple go评论57阅读模式
英文:

How to assign default value to column variable if column value is coming as null

问题

Hi我想要为列变量分配默认值如果从该变量的选择查询中获得的值为null

@Entity
@Table(name = "CDP_ALERTS")
public class Country {

   @Column(name = "alert_reported_time")
private String alertReportedTime;

@Column(name = "unique_id")
private String uniqueTrxId;

@Column(name = "status_data")
private String status;

public String getAlertReportedTime() {
        return alertReportedTime;
    }

    public void setAlertReportedTime(String alertReportedTime) {
        this.alertReportedTime = alertReportedTime;
    }
public String getUniqueTrxId() {
        return uniqueTrxId;
    }

    public void setUniqueTrxId(String uniqueTrxId) {
        this.uniqueTrxId = uniqueTrxId;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
    }

在这里status_data列的值有时为null
以下是选择查询

@Query("SELECT a FROM Country a WHERE to_char(alert_reported_time,'DD-MM-YY')=to_char(current_date,'DD-MM-YY') order by alert_reported_time desc")
List<Country> findCountryByDate();

以下是输出在一个对象中我得到的状态值为closed但在另一个对象中状态值为null

{
    [
        "alertSubject": "CWDigital Alert in MS onlinepayment with StatusCode 400",
        "alertReportedTime": "2020-05-29 15:16:03",
        "uniqueTrxId": "1018",
        "status": null,
        "reason": null,
        "conversationId": "ecd6184d-b2f1-4545-c5f2-1ac6d1df48fc",
        "clusterName": "patlacw01",
        "statusCode": "400\n",
        "nameSpace": "com-att-cwdigital-shop-prod"
    },
    {
        "alertSubject": "CWPOS Alert-500 in mS OrderManagement from pbhmacw01 and m26845@prod.shop.cwdigital.att.com",
        "alertReportedTime": "2020-05-29 15:15:41",
        "uniqueTrxId": "1017",
        "status": "CLOSED",
        "reason": null,
        "conversationId": "ee66359e-f87d-4eff-ce50-02ff6e18879a",
        "clusterName": "pbhmacw01",
        "statusCode": "500\n",
        "nameSpace": "com-att-cwpos-prod"
    }
]
英文:

Hi I want to assign default value to column variable if value from select query for that variable is coming as null.

    @Entity
@Table(name = &quot;CDP_ALERTS&quot;)
public class Country {
@Column(name = &quot;alert_reported_time&quot;)
private String alertReportedTime;
@Column(name = &quot;unique_id&quot;)
private String uniqueTrxId;
@Column(name = &quot;status_data&quot;)
private String status;
public String getAlertReportedTime() {
return alertReportedTime;
}
public void setAlertReportedTime(String alertReportedTime) {
this.alertReportedTime = alertReportedTime;
}
public String getUniqueTrxId() {
return uniqueTrxId;
}
public void setUniqueTrxId(String uniqueTrxId) {
this.uniqueTrxId = uniqueTrxId;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}

Here status_data column value is coming as null sometime.
below is the select query.

@Query( &quot;SELECT a FROM Country a WHERE to_char(alert_reported_time,&#39;DD-MM-YY&#39;)=to_char(current_date,&#39;DD-MM-YY&#39;) order by alert_reported_time desc&quot;)
List&lt;Country&gt; findCountryByDate();

Below is the output where in one object I am getting value as closed for status but in one object I am getting value as null.

{

[
&quot;alertSubject&quot;: &quot;CWDigital Alert in MS onlinepayment with StatusCode 400&quot;,
&quot;alertReportedTime&quot;: &quot;2020-05-29 15:16:03&quot;,
&quot;uniqueTrxId&quot;: &quot;1018&quot;,
&quot;status&quot;: null,
&quot;reason&quot;: null,
&quot;conversationId&quot;: &quot;ecd6184d-b2f1-4545-c5f2-1ac6d1df48fc&quot;,
&quot;clusterName&quot;: &quot;patlacw01&quot;,
&quot;statusCode&quot;: &quot;400\n&quot;,
&quot;nameSpace&quot;: &quot;com-att-cwdigital-shop-prod&quot;
},
{
&quot;alertSubject&quot;: &quot;CWPOS Alert-500 in mS OrderManagement from pbhmacw01 and m26845@prod.shop.cwdigital.att.com&quot;,
&quot;alertReportedTime&quot;: &quot;2020-05-29 15:15:41&quot;,
&quot;uniqueTrxId&quot;: &quot;1017&quot;,
&quot;status&quot;: &quot;CLOSED&quot;,
&quot;reason&quot;: null,
&quot;conversationId&quot;: &quot;ee66359e-f87d-4eff-ce50-02ff6e18879a&quot;,
&quot;clusterName&quot;: &quot;pbhmacw01&quot;,
&quot;statusCode&quot;: &quot;500\n&quot;,
&quot;nameSpace&quot;: &quot;com-att-cwpos-prod&quot;
}]

答案1

得分: 1

你可以修改属性的获取方法。

public String getStatus(){
if(status != null) return status;
return "无论你想要什么";
}

另外,如果你想要在数据库中保持默认值,你可以在属性中设置一个默认值。

@Column
private String status = "我的默认值";
英文:

You could change your getter method for the property.

public String getStatus(){
if(status != null) return status;
return &quot;Whatever you want&quot;;
}

Also, if you want to persist default value in db. you could set a default value in property.

@Column
private String status = &quot;My default value&quot;;

答案2

得分: 0

你需要将 nativeQuery 设置为 true,并且在选择查询中使用每个列与某个 SQL 方法一起,该方法将在该列具有空值时返回期望的值。就像在 H2 中,你可以使用 IFNULL(column, expectedvalue),在 Oracle 中你可以使用 nvl(column, expectedValue)

示例:

String query = "Select alertReportedTime, uniqueTrxId, IFNULL(status, 'defaultvalue') as status from CDP_ALERTS";

@Query(value = query, nativeQuery = true)
List<Country> findCountryByDate();
英文:

You need to set the nativeQuery = true, and use each column in select query with some sql method which return the expected value if that column has null value. Like in H2 you can use IFNULL(column, expectedvalue), in oracle you can use nvl(column, exepectedValue).

example:

String query = "Select alertReportedTime, uniqueTrxId, IFNULL(status, 'defaultvalue') as status from CDP_ALERTS";

@Query(value = query, nativeQuery = true)

List &lt;Country&gt; findCountryByDate();

答案3

得分: 0

如果您要向现有表添加新列,可以使用JPA注释设置默认值。这在您对列使用@Id注释时特别有帮助。这将在表的每一行上将默认值“SectionInput”设置为null。这非常高效且速度超快。

@Id
@Column(columnDefinition = "varchar(255) default 'SectionInput'")
protected String inputSource;

在我的情况下,这个新列被添加到了现有的复合主键中,这就是为什么我在其上使用@Id注释。

英文:

If you are adding a new column to existing table, you can use JPA annotations to set a default value. This is very helpful especially if you have @Id annotation on the column. This will set the default value of "SectionInput" instead of null on every row of the table. This is very efficient and super fast.

@Id
@Column(columnDefinition = &quot;varchar(255) default &#39;SectionInput&#39;&quot;)
protected String inputSource; 

In my case, this new column was added to the existing composite key, that's why I have @Id annotation on it.

huangapple
  • 本文由 发表于 2020年5月29日 20:45:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/62086353.html
匿名

发表评论

匿名网友

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

确定