Mapping array with Hibernate

后端 未结 4 815
谎友^
谎友^ 2020-11-29 09:53

Can you please help me to map this class using Hibernate?

public class MyClass{
    private Long id;
    private String name;
    private int[] values;
    .         


        
4条回答
  •  失恋的感觉
    2020-11-29 10:00

    This is a very good question, so I decided to write this article to explain how you can achieve this goal using the Hibernate Types project.

    Maven dependency

    The first thing you need to do is to set up the following Hibernate Types Maven dependency in your project pom.xml configuration file:

    
        com.vladmihalcea
        hibernate-types-52
        ${hibernate-types.version}
    
    

    Maven ARRAY columns

    Assuming you have this table in your database:

    create table event (
        id int8 not null, 
        version int4, 
        sensor_names text[], 
        sensor_values integer[], 
        primary key (id)
    )
    

    And you want to map it like this:

    @Entity(name = "Event")
    @Table(name = "event")
    @TypeDefs({
        @TypeDef(
            name = "string-array", 
            typeClass = StringArrayType.class
        ),
        @TypeDef(
            name = "int-array", 
            typeClass = IntArrayType.class
        )
    })
    public static class Event extends BaseEntity {
    
        @Type( type = "string-array" )
        @Column(
            name = "sensor_names", 
            columnDefinition = "text[]"
        )
        private String[] sensorNames;
    
        @Type( type = "int-array" )
        @Column(
            name = "sensor_values", 
            columnDefinition = "integer[]"
        )
        private int[] sensorValues;
    
        //Getters and setters omitted for brevity
    }
    

    The string-array and int-array are custom types which can be defined in the BaseEntity superclass:

    @TypeDefs({
        @TypeDef(
            name = "string-array", 
            typeClass = StringArrayType.class
        ),
        @TypeDef(
            name = "int-array", 
            typeClass = IntArrayType.class
        )
    })
    @MappedSuperclass
    public class BaseEntity {
    
        @Id
        private Long id;
    
        @Version
        private Integer version;
    
        //Getters and setters omitted for brevity
    }
    

    The StringArrayType and IntArrayType are classes offered by the Hibernate Types project.

    Testing time

    Now, when you insert a couple of entities;

    Event nullEvent = new Event();
    nullEvent.setId(0L);
    entityManager.persist(nullEvent);
    
    Event event = new Event();
    event.setId(1L);
    event.setSensorNames(
        new String[] {
            "Temperature", 
            "Pressure"
        }
    );
    event.setSensorValues( 
        new int[] {
            12, 
            756
        } 
    );
    entityManager.persist(event);
    

    Hibernate is going to generate the following SQL statements:

    INSERT INTO event (
        version, 
        sensor_names, 
        sensor_values, 
        id
    ) 
    VALUES (
        0, 
        NULL(ARRAY), 
        NULL(ARRAY), 
        0
    )
    
    INSERT INTO event (
        version, 
        sensor_names, 
        sensor_values, 
        id
    ) 
    VALUES ( 
        0, 
        {"Temperature","Pressure"}, 
        {"12","756"}, 
        1
    )
    

提交回复
热议问题