我试图连接两个实体,但我收到错误:
Hibernate: select user0_.id as id1_9_0_, user0_.email as email2_9_0_, user0_.surname as surname3_9_0_, webuser1_.id as id1_11_1_, webuser1_.active as active2_11_1_, webuser1_.password as password3_11_1_, webuser1_.username as username4_11_1_ from user user0_ left outer join web_user webuser1_ on user0_.id=webuser1_.id where user0_.id=? 2016-04-12 14:38:26,504 SqlExceptionHelper qtp1239132915-13 [WARN] [] SQL Error: 0, SQLState: 42703 2016-04-12 14:38:26,504 SqlExceptionHelper qtp1239132915-13 [ERROR] [] Error: column user0_.id does not exist
我使用的实体类:
WEBUSER
@javax.persistence.Entity
@Table(name = "WEB_USER")
public class WebUser implements Entity, Serializable{
private static final long serialVersionUID = 1994758293065748925L;
@Id
@GeneratedValue
public Long id;
@Column(name = "username", nullable = false, unique = true)
public String username;
@Column(name = "password", nullable = false)
public String password;
@Column(name = "active", nullable = false)
public boolean active;
@OneToOne(fetch = FetchType.EAGER, mappedBy = "webUser", cascade = CascadeType.ALL)
public User user;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Column(length = 200)
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password =
Hashing.sha256().hashString(password, Charsets.UTF_8).toString();
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
public User getUser(){
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
WebUser other = (WebUser) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password
+ ", active=" + active + "]";
}
}
和用户:
@javax.persistence.Entity
@Table(name = "USER")
public class User implements Entity {
@Id
@GeneratedValue
private Long id;
@Column(name = "surname", nullable = false)
public String surname;
@Column(name = "email", nullable = false)
public String email;
@OneToOne
@PrimaryKeyJoinColumn
public WebUser webUser;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public String getEmail(){
return email;
}
public void setEmail(String email){
this.email = email;
}
public WebUser getWebUser(){
return webUser;
}
public void setWebUser(WebUser webUser){
this.webUser = webUser;
}
}
我做错了什么?
===
我将User类中的注释更改为:
@OneToOne
@JoinColumn(name = "web_user", nullable = false)
public WebUser webUser;
因为hibernate使用user.id = web_user.id连接表而不是像我想要的那样连接user.web_user = web_user.id。
我仍然收到错误的错误列但不同的错误:
select
user0_.id as id1_9_1_,
user0_.email as email2_9_1_,
user0_.surname as surname3_9_1_,
user0_.web_user as web_user4_9_1_,
webuser1_.id as id1_11_0_,
webuser1_.active as active2_11_0_,
webuser1_.password as password3_11_0_,
webuser1_.username as username4_11_0_
from
user user0_
inner join
web_user webuser1_
on user0_.web_user=webuser1_.id
where
user0_.web_user=?
2016-04-13 09:54:46,131 SqlExceptionHelper qtp673129458-15 [WARN] [] SQL Error: 0, SQLState: 42703
2016-04-13 09:54:46,131 SqlExceptionHelper qtp673129458-15 [ERROR] [] ERROR: column user0_.web_user does not exist
答案 0 :(得分:1)
问题在于列名'user'。当我将其更改为'web_user_info'时,一切正常。