Issue
I have following entities:
@Entity
@Getter
@Setter
@Table(name = "nomenclature", schema = "public")
public class Nomenclature implements Serializable {
@Id
@Column(name = "id")
private Long id;
@Column(name = "name")
private String name;
@OneToMany
@JoinTable(name="nomenclature_versions",
joinColumns= @JoinColumn(name="nomenclature_id", referencedColumnName="id"))
private List<NomenclatureVersion> version;
}
and
@Entity
@Setter
@Getter
@Table(name = "nomenclature_versions", schema = "public")
@NoArgsConstructor
public class NomenclatureVersion implements Serializable {
@Id
@Generated(GenerationTime.INSERT)
@Column(name = "id")
private Long id;
@Column(name = "nomenclature_id")
private Long nomenclatureId;
@Column(name = "user_id")
private Long userId;
@Column(name = "creation_date")
private LocalDateTime creationDate;
@Column(name = "puls_code")
private String pulsCode;
@Column(name = "pic_url")
private String picUrl;
@Column(name = "current")
private boolean current;
}
When im trying to get Nomenclature with JPARepository getById(id) method im getting org.postgresql.util.PSQLException: ERROR: column version0_.version_id does not exist
It feels like the problem is around Hibernate Naming Strategies but i cant solve it.
Is there any other way to let Hibernate know which column should it use to join tables?
Solution
Don't use @JoinTable
, the table is the same as the entity you are referencing. You just have to specify the @JoinColumn
, hibernate will look for the column in the table referenced by the entity you are mapping the list to, NomenclatureVersion
:
@OneToMany
@JoinColumn(name="nomenclature_id")
private List<NomenclatureVersion> version;
Answered By - gmanjon
Answer Checked By - Mary Flores (JavaFixing Volunteer)