错误的hibernate SQL查询:ElementCollection包含在列中

时间:2012-02-16 19:56:46

标签: sql hibernate

我在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(“来自wifi_network_config”)时,生成以下查询&amp;导致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运行的小型测试程序。

谢谢, 哈

1 个答案:

答案 0 :(得分:1)

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

一般来说,编程接口而不是实现是一种很好的做法。使用Hibernate或JPA时,它很疯狂。