English 中文(简体)
制止双重犯罪。 (EJB实体/JBoss)
原标题:Storing Double.POSITIVE_INFINITY in MySQL (EJB entity/JBoss)

我拥有以下简便的日本邮局实体:

@Entity
@Table( name = myentity_table )
public class MyEntity {

  private double a;
  private double b;
  //(...)
}

<><><>>>>>><>>>>><>>>>>和<>>><<>>>>>> 可在<0>上<> >上<> > ><><>><>>>><>>>>><>>>>><>>>>><>>>>><>>>>>>><>>>>>>>><>>>>>>>><>>>>>>>><>>>>>>>><>>>>>>>><>>>>>>>>>>>>>><>>>>>>>>>>>>>>>>>>><>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 双重。 POSITIVE_INFINITY。 当我试图利用标准实体管理人把双倍制的实体储存到+INF输入数据库时,我例外:

java.sql.SQLException: 绝对值不是有效数字或大致数字数值

我知道我的SQL可能不支持N/INF/+INF号。 没有任何办法储存这个实体,而不必写“总部”的问询,将“+INF”改为“(或最高一倍)”? 理想的情况是,我要像往常一样,通过实体经理这样做。

提前感谢。

最佳回答

实体生命周期备用方法 @PrePersist, @PreUpdate可用于核实外地价值NAN/-INF/+INF等,然后相应确定违约值。

 //--

 @PrePersist  
 @PreUpdate  
 private void resetField() {

      if(field == Double.POSITIVE_INFINITY)
            field = somePredefinedValue;
 }

 //--
问题回答

I managed this problem by adding a varchar column to store the text representation of Float.NaN, Float.POSITIVE_INFINITY and Float.NEGATIVE_INFINITY while the original column will store NULL. Then I use the setter and the getter to do manage those two columns.

我的爱琴科

/** The value I persist. See it is a Float; */
@Column(name = "VALUE")
private Float value;

/** the  value complement  that does the trick. */
@Column(name = "VALUE_COMPLEMENT") // You can see I ve added a new column in my table (it is a varchar)
private String valueComplement;

/**
 * value getter.
 * If my value is null, it could mean that it is a NaN or +/- infinity.
 * Then let s check the complement.
 */
public Float getValue() {
    if (value == null) {
        try {
            return Float.parseFloat(this.valueComplement);
        } catch (NumberFormatException e) {
            return null;
        }
    } else {
        return value;
    }
}

/**
 * value setter
 * If the given value is a NaN or Inf, set this.value to null 
 * and this.complement to the string representation of NaN or +/- Inf.
 */
public void setValue(Float value) {
    if (value != null && (value.isNaN() || value.isInfinite())) {
        this.valueComplement = value.toString();
        this.value = null;
    } else {
        this.value = value;
    }
}

结果:

| ID | LABEL                 | VALUE | VALUE_COMPLEMENT |
| -- | --------------------- | ----- | ---------------- |
|  1 | PI                    |  3.14 | NULL             |
|  2 | gravity acceleration  |  9.81 | NULL             |
|  3 | sqare root of -1      | NULL  | NaN              |
|  4 | log of 0              | NULL  | -Infinity        |




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签