org.hibernate.engine.jdbc.spi.SqlExceptionHelper-“字段列表”中的未知列

时间:2018-12-03 05:06:42

标签: java mysql hibernate jpa java-ee

请帮助。我是休眠5 + MySQL的新手,所以这可能很愚蠢。

我正在尝试创建一个非常基本的社交媒体应用程序,该应用程序可以注册,登录和添加朋友。我收到此错误:

23:47:57.340 [http-nio-8080-exec-6] ERROR     org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Unknown column 'friends0_.User_Id' in 'field list'
    javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
        at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:149)
        at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:157)
        at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1423)
        at com.alonso.socialnetworks.dao.UserDAO.getUserByName(UserDAO.java:29)
        at com.alonso.socialnetworks.action.LoginAction.validate(LoginAction.java:34)
        at com.opensymphony.xwork2.validator.ValidationInterceptor.doBeforeInvocation(ValidationInterceptor.java:250)
        at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:262)
        at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:49)
        at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:99)
        at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
        at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.doIntercept(ConversionErrorInterceptor.java:142)
        at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:99)
        at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
        at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:137)
        at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:99)
        at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
        at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:137)

当我尝试创建一个表来处理添加好友功能时,抛出该错误。

我继续创建表以将用户与他们的朋友相关联,如下所示:

表名:users_users

列:

users_id-int(11)

friends_id-int(11)

这是我的模特班:

package com.alonso.socialnetworks.model;

import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name = "Users")
public class User {

    @Id
    @GenericGenerator(name = "inc", strategy = "increment")
    @GeneratedValue(generator = "inc")
    @Column(name = "Id")
    private Integer id;

    @Column(name = "name")
    private String userName;

    @Column(name = "password")
    private String password;

    @OneToMany(fetch=FetchType.EAGER)
    @JoinTable(name="users_users")
    @JoinColumn(name="users_id")
    private Set<User> friends;

    public Set<User> getFriends() {
        return friends;
    }

    public void setFriends(Set<User> friends) {
        this.friends = friends;
    }

    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;
    }

}

这是我正在使用的代码。

https://github.com/JAlonsoHdz/SocialNetworkWebApp

我在做什么错? 似乎休眠状态未映射users_users和列。

1 个答案:

答案 0 :(得分:0)

我将实体映射更改为this approach

这是我的代码在User模型类上的样子:

Welcome 0

最后,当用户尝试添加不存在的朋友时,我不得不处理空指针异常。这就是窍门。