Hibernate:将 Double 作为 Int 保存到数据库

发布于 2024-10-15 18:44:17 字数 1020 浏览 1 评论 0原文

有谁知道我如何创建一个Java POJO,它在内存中存储一​​个双精度值,但在磁盘上存储一个整数?

我想将这些对象作为整数存储到磁盘,因为我有 1 亿个对象,这样可以节省空间。然而,有时我的 Java 代码会操作内存中的值(而不保存它们)并将它们转换为实数,

我尝试过类似的方法:

@Entity
@Table(name = "POJO")
public class POJO {
@Id
@GeneratedValue
@Column(name = "id")
int id;

@Column(name = "value")
int valueInteger; // this is weird:  in the database, we want to use integers, to save space.  but in memory, sometimes we use real values
double value;

public int getValueInteger() {
    return valueInteger;
}

public void setValueInteger(int valueInteger) {
    this.valueInteger = valueInteger;
    this.value = valueInteger;
}

public double getValue() {
    return value;
}

public void setValue(double value) {
    this.value = value;
    // might want to call setValueInteger() here if consistency is required.  Right now, I think it's not, so I'll leave it out.
}
}

这不是一个糟糕的解决方案,只是内存占用量大于所需的大小。有没有办法让 POJO 只有一个双精度数(当然还有 ID),并指示 hibernate 在存储到数据库时强制将双精度数转换为整数(例如,通过舍入该双精度数)?

感谢您的帮助!

does anyone know how I can create a Java POJO which has stores a double in memory but an integer on disk?

I would like to store these objects to disk as integers, because I have 100 million of them, and that would save space. However, sometimes my Java code manipulates the values in memory (without saving them) and turns them into real numbers

I have tried something like:

@Entity
@Table(name = "POJO")
public class POJO {
@Id
@GeneratedValue
@Column(name = "id")
int id;

@Column(name = "value")
int valueInteger; // this is weird:  in the database, we want to use integers, to save space.  but in memory, sometimes we use real values
double value;

public int getValueInteger() {
    return valueInteger;
}

public void setValueInteger(int valueInteger) {
    this.valueInteger = valueInteger;
    this.value = valueInteger;
}

public double getValue() {
    return value;
}

public void setValue(double value) {
    this.value = value;
    // might want to call setValueInteger() here if consistency is required.  Right now, I think it's not, so I'll leave it out.
}
}

This is not a terrible solution, except the memory footprint is larger than needed. Is there some way to have the POJO have only a double (and the ID of course), and instruct hibernate to force a double to integer conversion when it stores to the database (for instance, by rounding that double)?

Thanks for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忘羡 2024-10-22 18:44:17

希望这有效:

@Column(name = "value")
private int intValue;

@Transient
private Double value = null;

public double getValue() {
     if (value == null)
         value = intValue;
     return value;
}

public void setValue(double value) {
     this.value = value;
     this.intValue = value;
}

Hope this works:

@Column(name = "value")
private int intValue;

@Transient
private Double value = null;

public double getValue() {
     if (value == null)
         value = intValue;
     return value;
}

public void setValue(double value) {
     this.value = value;
     this.intValue = value;
}
有深☉意 2024-10-22 18:44:17

RDBMS 倾向于根据精度和小数位数来定义列,而不是 IEEE 类型和位数。如果您只定义一个固定比例为 0 的数据库列,当数据库自动削减数字时,您将得到您想要的结果。

您可以定义一个用户类型对象来保存它并进行转换,但如果您真正关心 32 位数字的内存占用量,那么将其替换为对自定义用户类型对象的引用并不是真正的进步。 (尽管 Hibernate 庞然大物潜伏在您的应用程序代码下,但担心原始 int 值似乎不是集中优化工作的好地方。)

RDBMS tend to define columns in terms of precision and scale, rather than IEEE types and bit counts. If you just define a database column with fixed scale of 0 you will get the result you want when the DB automatically chops the number down.

You could define a user type object that holds it and converts, but if you're genuinely concerned about the memory footprint of a 32bit number, replacing that with a reference to a custom user type object isn't really a step forward. (Although with the behemoth that is hibernate lurking under your application code, worrying about primitive int values doesn't seem like a good place to focus your optimization efforts.)

梦醒灬来后我 2024-10-22 18:44:17

好吧,我想通了:Hibernate 自动执行所有舍入和转换。所以不需要任何聪明的东西。删除 valueInteger。值将以整数形式保存到数据库中,除了(其舍入值)。

OK I figured it out: Hibernate does all of the rounding and casting automatically. So no need for anything clever. Remove valueInteger. value will get saved to the database as an integer in the way you would except (its rounded value).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文