2013-02-25 76 views
12

我需要幫助。java休眠未知列''在'字段列表'

當我使用getAllStreets()方法我有錯誤的HQL:

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

我猜他一定要寫this_1_id代替this_1_.houses_id

可能是我做錯了實體和關係?

2實體 - 房屋和街道

ER - 型號:

表街道

  • 編號
  • 名稱
  • 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"), [email protected](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; 
    } 
} 

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

錯誤:

 
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 
+10

Aaaa!代碼太多! – Andremoniy 2013-02-25 11:49:20

+0

爲什麼你在街道上有房子ID和房子? – BobTheBuilder 2013-02-25 11:49:53

+0

您是否試圖直接運行此查詢? 「Streets」表是否有'houses_id'列? – 2013-02-25 11:52:07

回答

3

看看這個代碼:

@JoinTable(name="Houses", joinColumns = @JoinColumn(name="id"), [email protected](name="houses_id")) 

我不確定你想在這裏實現什麼,但JoinTable通常用於解決與中間表的ManyToMany關係。因此,此代碼意味着您有Housesidhouses_id列。錯誤消息說,Houses表中沒有houses_id表(這對我來說聽起來很合邏輯)
也許你應該試試ManyToOne和JoinColumn?例如:

@JoinColumn(name="house_id") 

houses_id如果這是在街道上表的外鍵。複數聽起來很奇怪,如果這是真正的多對一關係。

+1

是的,你是對的! ! 我刪除多麼可悲的人,並寫: @ManyToOne \t @JoinColumn(name = 「houses_id」) \t公房getHouse(){ \t \t回房子; \t} \t public void setHouse(House house){ \t \t this.house = house; \t} 它的工作..現在我需要與房子測試realtion。 – 2013-02-25 12:30:38

+0

所有的工作,謝謝所有..) – 2013-02-25 12:32:59

5

請確保您所調用的表具有正確的列。我遇到了這個問題,並發現映射的域列在數據庫中不匹配。

-1

只需檢查POJO中的變量名稱和數據庫中提供的名稱即可。當它們不一樣時,就會拋出這個錯誤。

+2

這應該是一個評論。不是問題的答案。添加一些描述並解釋更多細節。 – Raju 2016-04-09 05:33:59

-1

請檢查您的數據庫是否包含所有表格。如果表中缺少任何表或屬性,請將它們添加到數據庫並重新添加您的hibernate映射文件。我以這種方式解決了這個錯誤。我的數據庫的兩個表中缺少兩個屬性,一旦我添加它們並重新添加休眠,錯誤就被修復了。

相關問題