我需要帮助。
当我使用getAllStreets()
方法时,我在HQL中有错误:
org.hibernate.exception.SQLGrammarException: Unknown column 'this_1_.houses_id' in 'field list'
我猜他必须写this_1_id
而不是this_1_.houses_id
可能是我做错了实体和关系?
2实体 - 房屋和街道
ER - 模型:
表街道
桌屋
我的课程:
街
@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;
}
}
我的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
答案 0 :(得分:8)
请确保您呼叫的表格具有正确的列。我遇到了问题,我发现映射的域列在数据库中不匹配。
答案 1 :(得分:3)
查看此代码:
@JoinTable(name="Houses", joinColumns = @JoinColumn(name="id"), inverseJoinColumns=@JoinColumn(name="houses_id"))
我不确定你想在这里实现什么,但JoinTable通常用于解决与中间表的ManyToMany关系。因此,此代码表示您的表Houses
包含id
和houses_id
列。错误消息表示houses_id
表中没有Houses
(这对我来说听起来合乎逻辑)
也许您应该尝试使用ManyToOne和JoinColumn?例如:
@JoinColumn(name="house_id")
或houses_id
如果这是您在街道表中的外键。如果这是多对一的关系,那么多个听起来很奇怪。
答案 2 :(得分:0)
还要检查您班上的吸气和吸气设备名称,所有名称必须相同
public String getNome() {//wrong Nome
return name;
}
public void getName(String name) {//right
this.name = name;
}
答案 3 :(得分:0)
我知道有一个可以接受的答案。对于在此页面上出现相同问题的任何人(字段列表中的未知列),我想提一个替代答案。
当我通过映射查询UserSurveyLog(两个表之间具有相同的SurveyID)时,我试图从调查表中提取调查详细信息
映射用户调查表
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(referencedColumnName = "SurveyID")
private Survey survey;
public Survey getSurvey() { return survey; }
public void setSurvey(Survey survey) { this.survey = survey; }
public Integer getUserSurveyLogID() {
return userSurveyLogID;
}
看看我收到的这个错误
休眠:插入hifiniteDB.UserSurveyLog(CompletedDate,CreatedByUserName,CreatedDate,ExpiryDate,LastDeliveredDate,ModifiedByUserName,ModifiedDate,OrgNodeID,ProgramUserID,RootOrgNodeID,survey_SurveyID,SurveyID,SurveyStatus,SurveyTriggerID,SurveyTriggerID, ,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) 2019-10-09 14:39:45.722 [https-jsse-nio-8443-exec-3]错误o.h.e.jdbc.spi.SqlExceptionHelper-'字段列表'中的未知列'survey_SurveyID'
观察到我在Survey Table上进行的映射已添加了survey_SurveyID。不知道为什么仅执行获取操作会将此列添加到应该独立的UserSurvey Log上的Insert中。
我解决此问题的方法是在@join列上添加insertable = false,可更新= false。
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(referencedColumnName = "SurveyID", insertable = false,updatable = false)
private Survey survey;
答案 4 :(得分:0)
我注意到,当我使用诸如userName或bloodGroup之类的变量名称时,它得到了错误“原因:java.sql.SQLSyntaxErrorException:“字段列表”中的未知列“ user0_.blood_group”” 要解决此问题,请更改变量名称,例如username或blood_Group。我希望它能得到例外的结果。
答案 5 :(得分:-1)
只需检查POJO中的变量名称以及数据库中提供的名称。当它们不相同时会抛出这个错误。
答案 6 :(得分:-1)
请检查您的数据库是否包含所有表格。如果表中缺少任何表或属性,请将它们添加到数据库并重新添加hibernate映射文件。我以这种方式修复了这个错误。我在我的数据库的两个表中丢失了2个属性,一旦我添加它们并重新添加hibernate,错误就被修复了。