如何将Java中的2-d矩阵映射到Hibernate / JPA? [英] How to map a 2-d matrix in Java to Hibernate/JPA?

查看:128
本文介绍了如何将Java中的2-d矩阵映射到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
列表或项目秒。为了映射这个
新的 ProjectType 类可以是
创建来存储项目类型和
a OneToMany 项目



...
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 of Lists (i.e. a matrix), or a Map of Maps, or a Map of Lists, 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 a Map of Projects keyed by a String project-type and the value a List or Projects. To map this a new ProjectType class could be created to store the project-type and a OneToMany to Project.

...

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆