Reputation: 401
I have two classes that I want integrate via Hibernate:
In my Database structure I have table "Goal" which has field "ROLE_ID". This field points on Role's table Primary Key "ID". So, every row (entity) in Goal table maps to Role's row (entity) One by One.
So, I want to join Role class to Goal class to be able to represent extended Rolename for every Goal, which is placed in Role class. I use the below code to do this in Hibernate (in Goal class):
@OneToOne
@JoinColumn (name="role_id")
Role role;
And getting error: Repeated column in mapping for entity: com.platform.entity.Goal column: role_id (should be mapped with insert="false" update="false") Plese help me investigate this error?
ROLES table
create table MAPP.ROLES
(
id INTEGER generated always as identity,
rolename VARCHAR2(255),
descr VARCHAR2(255)
)
GOAL Table
create table MAPP.DIC_GOAL
(
id INTEGER generated always as identity,
descr NVARCHAR2(255),
role_id INTEGER not null,
vl NUMBER
)
Goal class
package com.platform.entity;
@Entity
@Table(name="dic_goal", schema="MAPP")
public class Goal {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID", nullable = false)
private Long id;
@Column(name = "DESCR", length = 255, nullable = false)
public String descr;
@Column(name = "ROLE_ID", nullable = false)
public int role_id;
// Here it is!
@OneToOne
@JoinColumn (name="role_id")
Role role;
@Column(name = "VL")
private float vl;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getDescr() {
return descr;
}
public void setDescr(String descr) {
this.descr= descr;
}
public float getVl() {
return vl;
}
public void setVl(float vl) {
this.vl = vl;
}
@Override
public String toString() {
return "ID=" + id + " DESCR=" + descr + " VL=" + vl;
}
}
Role class
package com.platform.entity;
@Entity
@Table(name="roles", schema="MAPP")
public class Role implements GrantedAuthority {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID", nullable = false)
private Long id;
@Column(name = "ROLENAME", length = 255, nullable = false)
@Size(min=5, message="Не меньше 5 знаков")
private String roleName;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name="user_roles", schema="MAPP",
joinColumns = @JoinColumn(name="role_id"),
inverseJoinColumns = @JoinColumn(name="user_id")
)
private Set<User> users;
public String getName() {
return roleName;
}
public void setName(String name) {
this.roleName = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Override
public String getAuthority() {
return getName();
}
@Override
public String toString() {
return this.getName();
}
}
Upvotes: 1
Views: 956
Reputation: 401
As Nikos Paraskevopoulos corretly mentioned: The reason is simple: you are mapping the column role_id to 2 different properties, the Role role and int role_id So I removed int role_id and replaced it by linked Role object.
Upvotes: 1