2013-02-25 11 views
12

Ho bisogno di un aiuto.java hibernate Colonna sconosciuta '' in 'lista campi'

Quando uso getAllStreets() metodo che ho errore di HQL:

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

Credo che lui deve scrivere this_1_id invece this_1_.houses_id

Può essere che ho fatto di sbagliato entità e la relazione?

2 entità - case e le strade

ER - Modello:

Streets Table

  • Id
  • Nome
  • Houses_id

Tabella Case

  • id
  • nome

mie classi:

Via

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

Casa

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

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

Errore:

 
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! Troppi codici !!! – Andremoniy

+0

Perché hai sia l'ID casa che la casa nel tuo oggetto stradale? – BobTheBuilder

+0

Hai provato a eseguire direttamente questa query? La tabella 'Streets' ha la colonna' houses_id'? –

risposta

3

Partenza di questo codice:

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

Non sono sicuro di cosa stai cercando di ottenere qui, ma lo standard JoinTable viene in genere utilizzato per risolvere molte relazioni di ManyMany con la tabella intermedia. Quindi questo codice implica che hai la tabella Houses con le colonne e houses_id. Il messaggio di errore dice che non c'è houses_id nella tabella Houses (che mi sembra logico)
Forse dovresti provare ManyToOne e JoinColumn invece? Per esempio:

@JoinColumn(name="house_id") 

o houses_id se è la vostra chiave esterna nella tabella di strade. Il plurale sembra strano se questa è davvero una relazione molti-a-uno.

+1

sì, hai ragione! ! elimino come triste qualcuno e scrivere: @ManyToOne \t @JoinColumn (name = "houses_id") \t Public House getHouse() {casa \t \t di ritorno; \t} \t public void setHouse (House house) { \t \t this.house = house; \t} ed è lavoro ... ora ho bisogno di test di verifica con la casa. –

+0

tutto lavoro, grazie a tutti ..) –

5

Assicurati che la tabella che stai chiamando abbia le colonne giuste.Ho avuto il problema e ho scoperto che le colonne del dominio mappato non corrispondevano nel database.

-1

Basta controllare i nomi delle variabili nel POJO e i nomi forniti nel database. Quando non sono uguali viene generato questo errore.

+2

Questo dovrebbe essere un commento. Non una risposta alla domanda. Aggiungi qualche descrizione e spiega con maggiori dettagli. – Raju

-1

Verificare se il database contiene tutte le tabelle. Se ci sono tabelle o attributi mancanti nella tabella, aggiungili al tuo database e aggiungi di nuovo i tuoi file di mappatura di ibernazione. Ho corretto questo errore in questo modo. Ho avuto 2 attributi mancanti in due tabelle del mio database e una volta aggiunti e riaggiunto l'ibernazione, l'errore è stato corretto.

Problemi correlati