Search code examples
javahibernatejpamultidimensional-array

How to map a 2-d matrix in Java to Hibernate/JPA?


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?


Solution

  • 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: