Hibernate JPQL - querying for KEY() in Map association error

佐手、 提交于 2019-12-23 17:30:25

问题


I'm trying to make a JPQL query that should fetch an entity and the keys from one of its map associations, and I'm getting a bizzare error.

My setup is JPA2 using the Hibernate (3.5) implementation.

The model is as follows:

I have a Department entity bean such as:

@Entity

public class Department {

@Id
@SequenceGenerator(name = "DEPARTMENT_ID_GENERATOR", sequenceName="department_sequence", allocationSize=100)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "DEPARTMENT_ID_GENERATOR")
@Column(unique = true, nullable = false)
protected Long id;

@OneToMany(fetch=FetchType.EAGER)
private Map<String,Phone> phones = new HashMap<String, Phone>();

//... getters and setters follow

and it's associated entity :

@Entity

public class Phone {

@Id
@SequenceGenerator(name = "PHONE_ID_GENERATOR", sequenceName="phone_sequence", allocationSize=100)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "PHONE_ID_GENERATOR")
@Column(unique = true, nullable = false)
protected Long id;
private int number;

//... getters and setters follow

Now I thought, as per the "Mastering JPA2..." book, that I could do a JPQL such as:

String queryString2 = "SELECT d, KEY(p) FROM Department d JOIN d.phones p WHERE p='internal'";

but all this gets me a bizzare error:

java.lang.IllegalStateException: No data type for node: org.hibernate.hql.ast.tree.MethodNode 

-[METHOD_CALL] MethodNode: '(' +-[METHOD_NAME] IdentNode: 'KEY' {originalText=KEY} -[EXPR_LIST] SqlNode: 'exprList' -[ALIAS_REF] IdentNode: 'phone2_.id' {alias=p, className=model.Phone, tableAlias=phone2_}

at org.hibernate.hql.ast.tree.SelectClause.initializeExplicitSelectClause(SelectClause.java:156)...

Can someone please tell me if the JPQ I'm using is wrong, and if so, what might be a correct alternative to getting an entity and ONLY the keys from one of its map associations?


回答1:


Your syntax is correct, this is because KEY & VALUE for maps are not implemented: https://hibernate.onjira.com/browse/HHH-5396 I am not aware of alternative query.




回答2:


For next readers, the bug mentionned by Mikko Maunu has just been resolved yesterday, so we just need to wait the next release :)



来源:https://stackoverflow.com/questions/7490039/hibernate-jpql-querying-for-key-in-map-association-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!