java hibernate'字段列表'中的未知列'' [英] java hibernate Unknown column ' ' in 'field list'

查看:122
本文介绍了java hibernate'字段列表'中的未知列''的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



当我使用 getAllStreets()方法时,我在HQL中遇到错误:

  org.hibernate.exception.SQLGrammarException:'字段列表'中的未知列'this_1_.houses_id'

我想他必须写 this_1_id ,而不是 this_1_。 houses_id



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

2实体 - 房屋和街道

ER - 模型:

表街道


  • Id

  • 名称

  • Houses_id


表格房屋




  • id

  • 名称



我的课程:

街道

  @Entity 
@Table(name =Streets)
public class Street {
private Long id;
私人字符串名称;
私人长屋_id;
私人房屋;
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;


$ / code $ / pre

$房子

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

private Long id;
私人字符串名称;
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;


我的DAOIMP:

StreetDAOImp:

  public class StreetDAOImpl implements StreetDAO {
@Override
public void addStreet(Street street)引发SQLException {
// TODO自动生成的方法存根
Session session = null;
try {
session = HibernateUtil.getSessionFactory()。openSession();
session.beginTransaction();

session.save(street);
session.getTransaction()。commit();
} catch(Exception e){
// TODO:处理异常
e.printStackTrace();
}
finally {
if(session!= null&& session.isOpen()){
session.close();


$ b @Override
public Collection getAllStreets()throws SQLException {
// TODO自动生成的方法存根
Session session = null;
列表< 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:处理异常
e.printStackTrace();
}
返回街道;


HouseDAOImpl:

  public class HouseDAOImpl implements HouseDAO {
$ b $ @Override
public void addHouse(House house)throws SQLException {
/ / TODO自动生成的方法存根
会话session = null;
try {
session = HibernateUtil.getSessionFactory()。openSession();
session.beginTransaction();
session.save(house);
session.getTransaction()。commit();
} catch(Exception e){
// TODO:处理异常
e.printStackTrace();
}
finally {
if(session!= null&& session.isOpen()){
session.close();



$ @覆盖
public Collection getAllHouses()抛出SQLException {
// TODO自动生成的方法存根
会话会话=空;
列表< House> houses = new ArrayList< House>();

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

}
返房;
}}

错误:

 
log4j:WARN记录器没有appender(org.jboss.logging)。
log4j:WARN请正确初始化log4j系统。
休眠:选择this_.id为id1_1_,this_.houses_id为houses2_1_1_,this_.name为name1_1_,this_1_.houses_id为houses3_0_1_,house2_.id为id0_0_,house2_.name作为name0_0_从街道THIS_左外连接房屋this_1_ on this_.id = this_1_.id left outer join房屋house2_上this_1_.houses_id = house2_.id
org.hibernate.exception.SQLGrammarException:未知列'this_1_.houses_id'in'field list'
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:82)
处org.hibernate作为org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
。 engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
位于org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
位于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)
org.hibernate.loader。 Loader.getResultSet(Loader.java:2031)
在org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1832)
在org.hibernate.loader.Loader.executeQueryStatement(Loader.java: 1811)
在org.hibernate.loader.Loader.doQuery(Loader.java:899)
在org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341)
at org .hibernate.loader.Loader.doList(Loader.java:2516)
在org.hibernate.loader.Loader.doList(Loader.java:2502)
在org.hibernate.loader.Loader.listIgnoreQueryCache (Loader.java:2332)
在org.hibernate.loader.Loader.list(Loader.java:2327)
在org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:124 )
在org.hibernate.internal.SessionImpl.list(SessionImpl.java:1621)
at或g.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:374)
在com.ff.DAO.StreetDAOImpl.getAllStreets(StreetDAOImpl.java:48)
在FFMain.main(FFMain.java: 58)
引起:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:'字段列表'中的未知列'this_1_.houses_id'$ sun.reflect.NativeConstructorAccessorImpl.newInstance0(本地方法)中的

。在sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
处java.lang.reflect.Constructor中sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
。 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.checkE rrorPacket(MysqlIO.java:3491)
在com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
在com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)
在com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
在com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2542)
在com.mysql .jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1734)
在com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1885)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

在java.lang.reflect.Method中.invoke(Method.java:597)
at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122)
... 16 more


  @JoinTable(name =h2_lin>解决方案

房屋,joinColumns = @JoinColumn(name =id),inverseJoinColumns = @ JoinColumn(name =houses_id))

我不确定你想在这里实现什么,但 JoinTable 通常用于解决与中间表的ManyToMany关系。所以这段代码意味着你有 Houses ,其中 id houses_id 列。错误消息表明 Houses 表中没有 houses_id (这听起来对我来说很合理)
也许你应该尝试ManyToOne和JoinColumn?例如:

  @JoinColumn(name =house_id)

houses_id 如果这是您在街道表中的外键。复数听起来很奇怪,如果这是真正的多对一关系。


I need a help.

When i use getAllStreets() method i have error in HQL:

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

I guess he must write this_1_id instead this_1_.houses_id

May be i did wrong Entity and relation ?

2 entity - Houses and Streets

ER - model:

Table Streets

  • Id
  • Name
  • Houses_id

Table Houses

  • id
  • name

My Classes:

Street

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

House

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

My DAOIMP:

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

Error:

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

解决方案

Check out this code:

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

I'm not sure what are you trying to achieve here, but JoinTable is usually used to resolve ManyToMany relations with intermediary table. So this code implies that you have table Houses with id and houses_id columns. Error message says that there is no houses_id in Houses table (which sounds logical to me)
Maybe you should try ManyToOne and JoinColumn instead? For example:

@JoinColumn(name="house_id")

or houses_id if that's your foreign key in streets table. Plural sounds strange if this is really many-to-one relationship.

这篇关于java hibernate'字段列表'中的未知列''的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆