Unknown column in Java hibernate ‘field list’
I need a help
When I use getallstreets() method, I have HQL error:
org.hibernate.exception.sqlGrammarException: UnkNown column 'this_1_.houses_id' in 'field list'
I think he must write this_ 1_ ID instead of this_ 1_. houses_ id
Maybe I did the wrong entity and relationship?
2 entities – houses and streets
ER model:
Table Street
>ID > name > houses_ id
Table
>ID > name
My courses:
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
Solution
Look at this Code:
@JoinTable(name="Houses",inverseJoinColumns=@JoinColumn(name="houses_id"))
I don't know what you want to achieve here, but jointable is usually used to solve the manytomany relationship with the mediation table So this code means that you have a home with ID and home_ The house listed in ID The error message says there is no house in the houses table_ ID (which sounds logical to me) maybe you should try manytoone and joincolumn? For example:
@JoinColumn(name="house_id")
Or houses_ ID, if this is your foreign key in the street table If this is really a one - to - one relationship, multiple voices are strange