Reputation: 486
I have two Domain-Models: "UserBean" and "LoginBean". It's a one-to-many relationship, an user has many record of login. LoginBean.userid is foreign key of UserBean.id . Here are DDL of database:
CREATE TABLE `users` (
`id` int(11) NOT NULL,
`username` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`register_date` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
)
CREATE TABLE `login` (
`id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`login_date` datetime NOT NULL,
`login_result` char(1) NOT NULL,
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`),
CONSTRAINT `login_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
)
@Entity
@Table(name = "login")
public class LoginBean {
@Id
@Column(name = "id", nullable = false)
@GenericGenerator(name = "ddd", strategy = "increment")
@GeneratedValue(generator = "ddd")
private Integer id; //PK
// FK --> user.id
@Column(name = "user_id", nullable = false)
private Integer userid;
@Column(name = "login_date", nullable = false)
private Date logindate;
@Column(name = "login_result", nullable = false)
private Boolean loginresult;
@ManyToOne
@ElementCollection(targetClass = fordream.hibernate.bean.UserBean.class)
private UserBean user;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getUserid() {
return userid;
}
public void setUserid(Integer userid) {
this.userid = userid;
}
public Date getLogindate() {
return logindate;
}
public void setLogindate(Date logindate) {
this.logindate = logindate;
}
public Boolean getLoginresult() {
return loginresult;
}
public void setLoginresult(Boolean loginresult) {
this.loginresult = loginresult;
}
public UserBean getUser() {
return user;
}
public void setUser(UserBean user) {
this.user = user;
}
}
And:
@Entity
@Table(name = "users")
public class UserBean {
@Id
@GenericGenerator(name = "abc", strategy = "increment")
@GeneratedValue(generator = "abc")
private Integer id; //PK
@Column(name = "username")
private String username;
@Column(name = "password")
private String password;
@Column(name = "register_date")
private Date register_date;
@OneToMany(mappedBy = "user", cascade = { CascadeType.MERGE })
private Set loginset;
public Set getLoginset() {
return loginset;
}
public void setLoginset(Set loginset) {
this.loginset = loginset;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Date getRegister_date() {
return register_date;
}
public void setRegister_date(Date register_date) {
this.register_date = register_date;
}
}
When I run the application, I get a message :Repeated column in mapping for entity: LoginBean column: user_id (should be mapped with insert="false" update="false")
But I checked every place, I never defined "user_id" twice in "LoginBean".
That's why? Thanks !
Upvotes: 3
Views: 7189
Reputation: 781
Your mapping is a bidirectional association. with your UserBean as Parent and LoginBean as Child. So you have 2 mappings to user_id table
@Column(name = "user_id", nullable = false)
private Integer userid;
And
@ManyToOne
@ElementCollection(targetClass = fordream.hibernate.bean.UserBean.class)
private UserBean user;
with userid and user.id trying to map to user_id. Totally removing the userid field will do it for you which i think you had already tried.
The explanation: @ManyToOne tag just says to go and look at the Class marked by ManyToOne for the association mappings. And when it goes to the UserBean, it finds that LoginBean is associated to UserBean by the "user" property of LoginBean. So hibernate will try and map the UserBean's id to the LoginBeans field marked as FK(in db) to user table.
Upvotes: 1
Reputation: 422
This error is just because you are declaring id two times and getter and setter for id. i.e : In LoginBean Class as well as UserBean Class. If you will remove the id from the second class you will not get an error.You can write insertable=false and updateable=false.
private Integer id; //PK
and please check you have by mistake created getter and setter there.
Upvotes: 0