首页 \ 问答 \ Hibernate:在一个数据库表行中存储一个固定长度的数组(Hibernate: Storing an fixed length array in one database table row)

Hibernate:在一个数据库表行中存储一个固定长度的数组(Hibernate: Storing an fixed length array in one database table row)

我一直在尝试找到一个解决方案来存储一个固定长度的数组作为对象的属性,使用hibernate在同一个数据库表中,而不是使用BLOB作为数组的对象。

我目前有一个类ProductionQCSession,看起来像

@Entity
public class ProductionQCSession extends IdEntity {

    private Long id;
    private Float velocity;
    private Float velocityTarget;
    private Float[] velocityProfile;

    public ProductionQCSession() {
    }

    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    @Override
    public Long getId() {
        return id;
    }

    @SuppressWarnings("unused")
    public void setId(Long id) {
        this.id = id;
    }

    @Basic
    public Float getVelocity() {
        return velocity;
    }

    public void setVelocity(Float velocity) {
        this.velocity = velocity;
    }

    @Basic
    public Float[] getVelocityProfile() {
        return velocityProfile;
    }

    public void setVelocityProfile(Float[] velocityProfile) {
        this.velocityProfile = velocityProfile;
    }
}

理想情况下,我希望数据库结构

id|velocity|VPValue0|VPValue1|VPValue2|VPValue3|...    
21|     2.1|     0.1|     0.2|    -0.1|     0.3|...

我非常确切地知道,我们在velocityProfile数组中总共有15个项目,并且这些值与对象的属性一样多,因此我认为将它们添加到数据库表模式是有意义的(如果可能的话)。 我更喜欢这样,因为很容易获得原始表打印数据的概述。

当前代码只是将数组数据存储为BLOB。

我看过http://ndpsoftware.com/HibernateMappingCheatSheet.html映射备忘单,但似乎无法找到任何好的解决方案。

我只是想做别人不会做的事情?


I have been trying to find a solution to store a fixed length array as a property of an object using hibernate in the same DB table as the object not using a BLOB for the array.

I currently have a class ProductionQCSession which looks like

@Entity
public class ProductionQCSession extends IdEntity {

    private Long id;
    private Float velocity;
    private Float velocityTarget;
    private Float[] velocityProfile;

    public ProductionQCSession() {
    }

    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    @Override
    public Long getId() {
        return id;
    }

    @SuppressWarnings("unused")
    public void setId(Long id) {
        this.id = id;
    }

    @Basic
    public Float getVelocity() {
        return velocity;
    }

    public void setVelocity(Float velocity) {
        this.velocity = velocity;
    }

    @Basic
    public Float[] getVelocityProfile() {
        return velocityProfile;
    }

    public void setVelocityProfile(Float[] velocityProfile) {
        this.velocityProfile = velocityProfile;
    }
}

Ideally I would like the DB structure to be

id|velocity|VPValue0|VPValue1|VPValue2|VPValue3|...    
21|     2.1|     0.1|     0.2|    -0.1|     0.3|...

I know with a high certainty that we always have 15 items in the velocityProfile array and those values as just as much properties of the object as any other property therefore I think it makes sense to add them to the database table schema, if it's possible. I would prefer to have it this way as it would be easy to get a overview of the data just doing a raw table print.

The current code just stores the array data as a BLOB.

I have looked http://ndpsoftware.com/HibernateMappingCheatSheet.html mapping cheat sheet, but could not seem to find any good solution.

I'm I just trying to do something nobody else would do?


原文:https://stackoverflow.com/questions/29750087
更新时间:2021-11-17 19:11

最满意答案

从本质上讲,您正在尝试拥有一个多值字段,这不是关系数据库概念。 规范化的解决方案会将它们放入子表中,Hibernate允许您直接从父行访问(并将其作为集合返回)。

如果你坚持认为它应该在一个表中,那么你需要创建15个单独的列....并希望将来你不会突然需要16个。


Essentially, you're trying to have a multi-value field, which is not a relational database concept. A normalized solution would put those into a child table, which Hibernate would let you access directly from the parent row (and return it as a collection).

If you are adamant that it should be in a single table, then you'll need to create 15 individual columns....and hope that in the future you don't suddenly need 16.

相关问答

更多

相关文章

更多

最新问答

更多
  • 如何从rails插件加载javascript文件(How to load javascript file from rails plugin)
  • 我正在研究有向图的DFS,如何到达没有任何边缘指向的节点?(I'm working on a DFS of a directed graph, How do I get to a node without any edges pointed at it?)
  • 无法在swift中展开可选项(can't unwrap optional in swift)
  • 如何从熊猫中提取列表?(How to extract a list from pandas?)
  • 计算不同列中2个值的出现次数(Count number of occurrences of 2 values in different columns)
  • Stack ExtJS 6 + Spring Boot(Stack ExtJS 6 + Spring Boot)
  • 如何安装ioreg?(How to install ioreg?)
  • Chrome Packaged app webview中的重音字符未显示(Accented characters in Chrome Packaged app webview not showing up)
  • C ++ recvfrom超时(C++ recvfrom timeout)
  • 如何使用实体框架(.edmx模型)和Razor视图为MVC4或MVC 5创建部分视图?(How can i create a Partial View for MVC4 or MVC 5 using Entity Framework (.edmx Model) with Razor Views?)
  • 子类QStyledItemDelegate忽略样式表(Subclassed QStyledItemDelegate ignores Stylesheet)
  • 有没有办法以编程方式配置ios应用程序的通知显示设置?(Is there a way to programmatically configure the notification display setting for an ios app?)
  • boost asio serial_port_service和serial_port有什么区别(What is the difference of boost asio serial_port_service and serial_port)
  • 如何通过解析nginx日志文件自动拒绝IP地址(How to deny ip address automatically by parsing nginx log file)
  • 如何在C#中为ASCII 127定义字符或字符串常量?(How might I define a character or string constant in C# for ASCII 127?)
  • 如何获得R中两个集合的差异?(How to get the differences of two set in R?)
  • 如何通过IMG-Activity找到定制路径(How to find the Customizing Path via IMG-Activity)
  • 可以用python窗口打开.py文件么
  • 指针的增量是否与整数的增量相同?(Does increment in pointer not same as that of integer? [closed])
  • 我无法删除使用Delphi7处于关系中的数据库的2个表中的所有数据(I can't delete all the data within 2 tables of a database which is in a relationship using Delphi7)
  • 使用反射向ArrayList添加未知类的对象(Add an object of unknown class to ArrayList with reflection)
  • 我想学JAVA 推荐下怎么 学
  • 什么是ASP.NET Identity的IUserSecurityStampStore 接口?(What is ASP.NET Identity's IUserSecurityStampStore interface?)
  • jquery如何获取多个同id的div,然后修改css属性左边间距left的值
  • jquery ui resizable:调整旋转对象的大小(jquery ui resizable: resizing the rotated objects)
  • 如何用sublime text编写微信小程序代码
  • Hibernate:在一个数据库表行中存储一个固定长度的数组(Hibernate: Storing an fixed length array in one database table row)
  • shared_ptr用于原始指针参数(shared_ptr for a raw pointer argument)
  • 将本地git分支与远程分支进行比较?(How to compare a local git branch with its remote branch?)
  • 如何在DOM中查找字符串并通过php或jQuery更改它们(how to find strings in DOM and change them by php or jQuery)