Cary
Cary

Reputation: 23

JPA Map<String,String> restriction on map value length?

@Entity
@Table(name="Foo")
public class FooEntity {
    @Id
    private String name;
    ...
    @ElementCollection(fetch=FetchType.EAGER)
    private Map<String, String> properties = new HashMap<>();
    ...
}

I've got a JPA Entity similar to what's shown above. When running the above I get a "properties" table with a default maximum length for the map value column of 255 characters. How do I change this length restriction on the map value, if I wanted to support lengths of (say) up to 64K for the value (not the key)?

Upvotes: 2

Views: 1138

Answers (1)

Bohuslav Burghardt
Bohuslav Burghardt

Reputation: 34796

You can use following mapping:

@Entity
@Table
public class Foo {
    // ID, other properties, ...

    @ElementCollection
    @MapKeyColumn(name="key", length=100)
    @Column(name="value", length=512)
    @CollectionTable(name="properties", joinColumns=@JoinColumn(name="foo_id"))
    private Map<String, String> properties;
}

The interesting part for you is the @Column annotation which allows you to customize mapping of the value column.

This mapping will result in following DDL when you run schema export:

CREATE TABLE properties (
    foo_id INT8 NOT NULL,
    value VARCHAR(512),
    key VARCHAR(100) NOT NULL,
    PRIMARY KEY (user_id, key)
);

Upvotes: 2

Related Questions