如何将Java中的2-d矩阵映射到Hibernate / JPA? [英] How to map a 2-d matrix in Java to Hibernate/JPA?
问题描述
我有一个旧数据库,我正试图重新设计到21世纪。其中一个现有的数据结构涉及一个包含值的二维矩阵的特定类。如果我要从数据库中反向设计这个类,我最终会得到一系列属性,如:
private BigDecimal NODE_1_MATRIX_POS_1_1;
私人BigDecimal NODE_1_MATRIX_POS_1_2;
等等。由于这是一个6×6的矩阵,因此有很多这样的列。
我一直在寻找更好的方法,但我不确定我在那里。我想要做的是这样的:
@Entity
public class TestClass {
@Id
私人长ID;
$ b @CollectionOfElements
@JoinTable(
name =MATRIX_DATA,
joinColumns = @ JoinColumn(name =ENTRY_ID))
private List<列表与LT; BigDecimal的>>矩阵;
但是这个失败:
org.hibernate.MappingException:无法确定类型:java.util.List,表:MATRIX_DATA,列:[org.hibernate.mapping.Column(element)]
与其仅仅试图解决错误,我想我会四处询问并尝试找到正确的方法来解决这个映射挑战。有没有人通过JPA发现成功和满意的映射多维数组?
这个错误,我想我会问,并试图找到正确的方法来解决这个映射挑战。有没有人通过JPA发现成功和满意度映射多维数组?
AFAIK,标准JPA不支持嵌套集合。 JPA wiki书对此主题有很好的介绍(我只引用它的一部分):
嵌套集合,地图和矩阵
在一个对象
模型中有一些常见的复杂集合
关系,例如
List
>
Map
s(即矩阵)或Map
> s,或Map
List
s,以及
on。不幸的是,这些类型的
集合映射到
关系数据库的效果很差。
JPA不支持嵌套集合关系,通常
例如,如果
最好更改您的对象模型
,以避免它们使持久性和
查询更容易。一种解决方案是
创建一个包装嵌套
集合的对象。员工
有一个
Map
项目
b字符串
项目类型和值a
列表或
项目
秒。为了映射这个
,带有休眠JPA批注?
新的ProjectType
类可以是
创建来存储项目类型和
aOneToMany
到项目
。
...
blockquote>
这就是我的建议。例如:
@Entity
public class TestClass {
@Id
private long id ;
@OneToMany(mappedBy =testClass)
私人列表< MatrixRow>矩阵;
其中
MatrixLine
会(省略许多细节):
@Entity
公共类MatrixRow {
@Id
私人长ID;
@ManyToOne
private TestClass testClass;
@CollectionOfElements
私人列表< BigDecimal>行;
}
或也许,您可以使用自定义用户类型(我不太确定这是如何工作的)。
或者(毕竟,您已经使用了非便携式注释)查看此问题以查看如何扩展Hibernate:
I have a legacy database I'm trying to redesign into the 21st century. One of the existing data structures involves a particular class which contains a 2-dimensional matrix of values. If I were to reverse-engineer this class from the database, I'd end up with a series of attributes like:
private BigDecimal NODE_1_MATRIX_POS_1_1; private BigDecimal NODE_1_MATRIX_POS_1_2;
and so on. Since this is a 6x6 matrix, there are a lot of such columns.
I've been looking for a better way, but I'm not sure I'm there. What I'd like to do is something like this:
@Entity public class TestClass { @Id private long id; @CollectionOfElements @JoinTable( name="MATRIX_DATA", joinColumns=@JoinColumn(name="ENTRY_ID")) private List<List<BigDecimal>> matrix;
But this fails:
org.hibernate.MappingException: Could not determine type for: java.util.List, at table: MATRIX_DATA, for columns: [org.hibernate.mapping.Column(element)]
Rather than just trying to fix the error, I thought I'd ask around and try to find the right approach to solving this mapping challenge. Has anyone found success and satisfaction mapping multidimensional arrays via JPA?
解决方案Rather than just trying to fix the error, I thought I'd ask around and try to find the right approach to solving this mapping challenge. Has anyone found success and satisfaction mapping multidimensional arrays via JPA?
AFAIK, nested collections are not supported by standard JPA. The JPA wiki book has a good section on this topic (I'm quoting only a part of it):
Nested Collections, Maps and Matrices
It is somewhat common in an object model to have complex collection relationships such as a
List
ofList
s (i.e. a matrix), or aMap
ofMap
s, or aMap
ofList
s, and so on. Unfortunately these types of collections map very poorly to a relational database.JPA does not support nested collection relationships, and normally it is best to change your object model to avoid them to make persistence and querying easier. One solution is to create an object that wraps the nested collection.
For example if an
Employee
had aMap
ofProject
s keyed by aString
project-type and the value aList
orProject
s. To map this a newProjectType
class could be created to store the project-type and aOneToMany
toProject
....
And that would be my suggestion. For example:
@Entity public class TestClass { @Id private long id; @OneToMany(mappedBy="testClass") private List<MatrixRow> matrix; }
Where
MatrixLine
would be (omitting many details):@Entity public class MatrixRow { @Id private long id; @ManyToOne private TestClass testClass; @CollectionOfElements private List<BigDecimal> row; }
Or maybe you could use a custom user type (I'm not too sure how this would work).
Or (after all, you're already using non portable annotations) have a look at this question to see how you could extend Hibernate:
这篇关于如何将Java中的2-d矩阵映射到Hibernate / JPA?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!