当前位置: 首页 > 知识库问答 >
问题:

JavaHibernate“字段列表”中的未知列“”

屠嘉
2023-03-14

我需要帮助。

使用GetAllStreets()方法时,在HQL中出现错误:

org.hibernate.exception.SQLGrammarException: Unknown column 'this_1_.houses_id' in 'field list'

我猜他必须写this_1_id而不是this_1_.houses_id

可能是我做错了实体和关系吗?

2个实体-房屋和街道

ER模型:

餐桌街道

  • ID
  • 名称
  • houses_id

桌房

  • ID
  • 名称

我的类:

街道

@Entity
@Table(name="Streets")
public class Street {
    private Long id;
    private String name;
    private Long houses_id;
    private House house;
    public Street(){}    
    @Id
    @GeneratedValue(generator="increment")
    @GenericGenerator(name="increment", strategy="increment")
    @Column(name="id")
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    @Column(name="name")
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @ManyToOne
    @JoinTable(name="Houses", joinColumns = @JoinColumn(name="id"), inverseJoinColumns=@JoinColumn(name="houses_id"))
    public House getHouse() {
        return house;
    }
    public void setHouse(House house) {
        this.house = house;
    }
    @Column(name="houses_id")
    public Long getHouses_id() {
        return houses_id;
    }
    public void setHouses_id(Long houses_id) {
        this.houses_id = houses_id;
    }
}

房屋

@Entity
@Table(name="Houses")
public class House {

    private Long id;
    private String name;
    public House(){}

    @Id
    @GeneratedValue(generator = "increment")
    @GenericGenerator(name="increment", strategy="increment")
    @Column(name="id")
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    @Column(name="name")
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

我的道小鬼:

StreetDaoImp:

public class StreetDAOImpl implements StreetDAO {
    @Override
    public void addStreet(Street street) throws SQLException {
        // TODO Auto-generated method stub
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();

            session.save(street);
            session.getTransaction().commit();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        finally{
            if(session != null && session.isOpen()){
                session.close();
            }
        }
    }
    @Override
    public Collection getAllStreets() throws SQLException {
        // TODO Auto-generated method stub
        Session session = null;
        List<Street> streets = new ArrayList<Street>(); 
        try {
            session = HibernateUtil.getSessionFactory().openSession();

            streets = session.createCriteria(Street.class).list();
            //Query q = session.createQuery("select str from com.ff.model.Street str join str.houses h where h.id = str.houses_id");
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return streets;
    }
}

HouseDaoImpl:

public class HouseDAOImpl implements HouseDAO {

    @Override
    public void addHouse(House house)throws SQLException {
        // TODO Auto-generated method stub
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(house);
            session.getTransaction().commit();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        finally{
            if(session != null && session.isOpen()){
                session.close();
            }
        }
    }

    @Override
    public Collection getAllHouses() throws SQLException {
        // TODO Auto-generated method stub
        Session session = null;
        List<House> houses = new ArrayList<House>();

        try {
            session = HibernateUtil.getSessionFactory().openSession();
            houses = session.createCriteria(House.class).list();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
         finally {
              if (session != null && session.isOpen()) {
                session.close();
              }

    }
    return houses;
}}

错误:

log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select this_.id as id1_1_, this_.houses_id as houses2_1_1_, this_.name as name1_1_, this_1_.houses_id as houses3_0_1_, house2_.id as id0_0_, house2_.name as name0_0_ from Streets this_ left outer join Houses this_1_ on this_.id=this_1_.id left outer join Houses house2_ on this_1_.houses_id=house2_.id
org.hibernate.exception.SQLGrammarException: Unknown column 'this_1_.houses_id' in 'field list'
    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:82)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
    at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129)
    at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
    at $Proxy14.executeQuery(Unknown Source)
    at org.hibernate.loader.Loader.getResultSet(Loader.java:2031)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1832)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1811)
    at org.hibernate.loader.Loader.doQuery(Loader.java:899)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341)
    at org.hibernate.loader.Loader.doList(Loader.java:2516)
    at org.hibernate.loader.Loader.doList(Loader.java:2502)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2332)
    at org.hibernate.loader.Loader.list(Loader.java:2327)
    at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:124)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1621)
    at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:374)
    at com.ff.DAO.StreetDAOImpl.getAllStreets(StreetDAOImpl.java:48)
    at FFMain.main(FFMain.java:58)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'this_1_.houses_id' in 'field list'
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
    at com.mysql.jdbc.Util.getInstance(Util.java:381)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2542)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1734)
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1885)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122)
    ... 16 more

共有2个答案

荀俊迈
2023-03-14

请确保您正在调用的表具有正确的列。我发现映射的域列在数据库中不匹配。

蒋星驰
2023-03-14

检查此代码:

@JoinTable(name="Houses", joinColumns = @JoinColumn(name="id"), inverseJoinColumns=@JoinColumn(name="houses_id"))

我不确定你在这里想要实现什么,但是JoinTable通常被用来解决与中介表的各种关系。因此,这段代码意味着您有表houseshouses_id列。错误消息说houses表中没有houses_id(在我看来这听起来很合乎逻辑)
也许您应该尝试ManyToOne和JoinColumn?例如:

@JoinColumn(name="house_id")

或者houses_id(如果这是streets表中的外键)。如果这真的是多对一的关系,复数听起来很奇怪。

 类似资料:
  • 所以首先我为我不可靠的解释道歉。我的PHP技能需要认真改进。 错误消息表示“”未知。我知道这不是真的,因为当我创建一个名为“”的变量并回显前者时,我会得到编号。 以下是完整的错误消息: 致命错误:未捕获异常“PDOException”,消息为“SQLSTATE[42S22]:未找到列:C:\xampp\htdocs\loginregister master\addnew中的“字段列表”中的1054

  • 即使我在表中设置了关系和定义了外键,我得到这个错误的原因是什么?

  • 问题内容: 我需要帮助。 当我使用方法时,我在HQL中有错误: 我猜他一定要写代替 可能是我做错了实体和关系吗? 2个实体-房屋和街道 ER-model: Table Streets ID Name Houses_id Table Houses ID name My Classes: Street House My DAOIMP: StreetDAOImp: HouseDAOImpl: 错误: 问

  • 问题内容: 我试图将值添加到phpmyadmin中的表中,但出现错误:“字段列表”中的未知列“ …”。 这是我的代码: 因此,当我在上一页的表单中输入fds作为名称时,我得到:“字段列表”中的未知列“ fds”。这以前从未发生过,我也不知道发生了什么。 问题答案: 我认为这 应该

  • 我正在尝试使用Hibernate处理OneTo诸多关系。我正在使用@Temporal注释告诉hibernate有关Data字段的信息。我不确定为什么我在这里得到这个错误。看来日期格式有问题。请让我知道如何解决它。 顾客 订单 跑步者 错误

  • 我正在编写一个spring boot+Hibernate JPA应用程序,我的Hibernate代码不工作,下面是详细信息 实体类: 服务/DAO: 我已经检查了实体类中的表名和列名,所有这些都是正确的,这是一个简单的映射,我也不涉及第二个表。