繁体   English   中英

错误的休眠SQL查询:ElementCollection作为列包含

[英]Wrong hibernate SQL query: ElementCollection included as column

我在Entity类中有此字段,该类是String的集合:

...
@ElementCollection(fetch=FetchType.EAGER)
@JoinTable(name="wifi_network_config_auth_algorithm")
@JoinColumn(name="wifi_network_config_id", referencedColumnName="id")
private ArrayList<String> authAlgorithm = new ArrayList<String>();

public List<String> getAuthAlgorithm() {
    return authAlgorithm;
}

public void setAuthAlgorithm(ArrayList<String> authAlgorithm) {
    this.authAlgorithm = authAlgorithm;
}
...

当我使用HibernateTemplate.find(“ from wifi_network_config”)时,下面的查询生成并导致SQLGrammarException,因为与字段authAlgorithm对应的列不在数据库表中。 由于authAlgorithm是集合,因此不应将其包含在查询中。

2012-02-16 10:11:56,365 WARN - <SQL Error: 1054, SQLState: 42S22>
2012-02-16 10:11:56,366 ERROR - <Unknown column 'wifinetwor0_.authAlgorithm' in 'field list'>
org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; SQL [select wifinetwor0_.id as id0_, wifinetwor0_.ssid as ssid0_, wifinetwor0_.priority as priority0_, wifinetwor0_.auth_algorithms as auth4_0_, wifinetwor0_.group_cipher as group5_0_, wifinetwor0_.key_management as key6_0_, wifinetwor0_.pairwise_cipher as pairwise7_0_, wifinetwor0_.protocol as protocol0_, wifinetwor0_.vpn_type as vpn9_0_, wifinetwor0_.vpn_url as vpn10_0_, wifinetwor0_.authAlgorithm as authAlg11_0_ from wifi_network_config wifinetwor0_]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query
        at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:629)
        at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
        at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411)
        at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
        at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:912)
        at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:904)

数据库是MySql。 这适用于针对hsqldb运行的小型测试程序。

谢谢,哈里

您不能将持久性集合声明为ArrayList 您必须改为使用接口: List Hibernate需要用特定于Hibernate的集合(实现List)替换您的实际集合。

通常,对接口进行编程而不是对实现进行编码是一种好习惯。 使用Hibernate或JPA时,这是必须的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM