Hibernate:将 Double 作为 Int 保存到数据库
有谁知道我如何创建一个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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
希望这有效:
Hope this works:
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.)
好吧,我想通了: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).