简体   繁体   中英

Wrong hibernate SQL query: ElementCollection included as column

I have this field in an Entity class, which a collection of String's:

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

When I use HibernateTemplate.find("from wifi_network_config") the query below is generated & causes SQLGrammarException as the column corresponding to the field 'authAlgorithm' is not in the database table. Since authAlgorithm is a collection, it should not be included in the query.

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)

Database is MySql. This works in a small test program that's run against hsqldb.

Thanks, Hari

You may not declare a persistent collection as an ArrayList . You must use the interface instead: List . Hibernate needs to replace your actual collection by a Hibernate-specific collection (which implements List).

It's a good practice in general to program to interfaces rather than implementations. When using Hibernate or JPA, it's madatory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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