How to map a column to a JPA property if it was already mapped in an @ManyToOne association when using Hibernate

I’d like to modify a database field (Char[50]) , as a java String field, via @Column, and a setter method.

But the Column is already used , to create an object field, via @ManyToOne and @JoinColumn. So, when I try to create the String field with @Column I get a org.hibernate.MappingException: Repeated column in mapping for entity: ... (should be mapped with insert="false" update="false") error.

Is there a solution? I’d expect it’d be possible to use the same column for different things?

Yes, it is possible. You just have to do what the error message tells you.

So, this is how you map the repeated column:

@Column(name = "my_char_column", insertable="false" updateable="false")
private String myProperty.

Unfortunately, this solution doesn’t work when the field is part of a composite PK (a field marked with @Id). Example:

...
@Entity
@Table(name = "event_context")
@IdClass(EventContextKey.class)
public class EventContext implements Serializable {

    @Id
    @Column(name = "id", columnDefinition = "BINARY(16)")
    private UUID id;

    @Id
    @Column(
        name = "event_id",
        columnDefinition = "BINARY(16)", insertable = false, updatable = false)
    private UUID eventId;

    @Id
    @Column(
        name = "event_version_id",
        columnDefinition = "BINARY(8)", insertable = false, updatable = false)
    private SmallUID eventVersionId;

    @ToString.Exclude
    @EqualsAndHashCode.Exclude
    @LazyToOne(LazyToOneOption.NO_PROXY)
    @ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
    @JoinColumns({
            @JoinColumn(
                name = "event_id",
                referencedColumnName = "event_id"),
            @JoinColumn(
                name = "event_version_id",
                referencedColumnName = "event_version_id")
    })
    private EventEntity event;
......

Use this

@Entity
@Table(name = “event_context”)
@IdClass(EventContextKey.class)
public class EventContext implements Serializable {

@Id
@Column(name = "id", columnDefinition = "BINARY(16)")
private UUID id;

@Id
@Column(
    name = "event_id",
    columnDefinition = "BINARY(16)")
private UUID eventId;

@Id
@Column(
    name = "event_version_id",
    columnDefinition = "BINARY(8)")
private SmallUID eventVersionId;

@ToString.Exclude
@EqualsAndHashCode.Exclude
@LazyToOne(LazyToOneOption.NO_PROXY)
@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
@JoinColumns({
        @JoinColumn(
            name = "event_id",
            referencedColumnName = "event_id", insertable = false, updatable = false),
        @JoinColumn(
            name = "event_version_id",
            referencedColumnName = "event_version_id", insertable = false, updatable = false)
})
private EventEntity event;

Hi, Can you please provide some good reference on insertable = false and updatable = false

What kind of reference do you want? It’s essentially just saying that values from the attribute should not be used to either insert or update into the columns that are mentioned. Usually, this is used if you want to map a column multiple times, but Hibernate only allows writing to the column through a single attribute. If there were multiple paths with multiple values, it wouldn’t be clear which value to use. See the Javadoc for more information: Column (Java(TM) EE 7 Specification APIs)