JPA. @Column(updatable=false) on @ManyToOne related field
Asked Answered
T

1

2

In my auditable entities I have field creationUser which I would like to not update in db on merge operation.

Here is my entity code:

@Column(updatable=false) 
@ManyToOne(cascade = CascadeType.MERGE)
public User creationUser;

But it gives me an error:

Unexpected exception
PersistenceException: [PersistenceUnit: defaultPersistenceUnit] Unable to build EntityManagerFactory
Caused by: org.hibernate.AnnotationException: @Column(s) not allowed on a @ManyToOne property: models.AreaOfMedicine.creationUser

So how can I prevent from updating that field? Please help

Trachyte answered 28/4, 2015 at 10:40 Comment(2)
The error message tells you that @Column and @ManyToOne can't be specified together in this case. Remove @Column!Gustafson
I can read what was the problem. I dont wanna remove that annotation, cause I need it. Your solution Is not a solution at all.Trachyte
K
11

Use @JoinColumn(updatable = false) instead of @Column(updatable = false).

Knightly answered 28/4, 2015 at 10:43 Comment(3)
Not all heroes wear capes :DMatins
What if it is a @OneToMany ?Situla
@OneToMany would have the join column on the other side of the relationship.Knightly

© 2022 - 2024 — McMap. All rights reserved.