[hibernate-issues] [Hibernate-JIRA] Issue Comment Edited: (HHH-6103) Criteria API based on MapKeyColumn is not working

Edwin Dalorzo (JIRA) noreply at atlassian.com
Tue Sep 6 09:58:04 EDT 2011


    [ http://opensource.atlassian.com/projects/hibernate/browse/HHH-6103?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=43457#comment-43457 ] 

Edwin Dalorzo edited comment on HHH-6103 at 9/6/11 8:57 AM:
------------------------------------------------------------

Hello Mariam

Well, it appears it will take a while before somebody fix this. You can read what I investigated in my StackOverflow answer here:
http://stackoverflow.com/questions/5580614/jpa-criteriabuilder-join-maps/5587957#5587957

The candidate solutions in my case where:

1. Use another persistence vendor (EclipseLink and OpenJPA both could handle this case without problems)
2. Do not use map, use another kind of collection. In my case I created a class (kind of an entry class with key and value) and then use a set collection to hold the information of this entry objects. Correctly implemting equals() and hashCode let me look for items in the set just using the key value.

In my case I could not change vendor so I used option #2.

I hope that helps

Regards,
Edwin

      was (Author: edalorzo):
    Hello Mariam

Well, it appears it will take a while before somebody fix this. You can read what I investigated in my StackOverflow answer here:
http://stackoverflow.com/questions/5580614/jpa-criteriabuilder-join-maps/5587957#5587957

The candidate solutions in my case where:

1. Use another persistence vender (EclipseLink and OpenJPA both could handle this case without problems)
2. Do not use map, use another kind of collection. In my case I created a class (kind of an entry class with key and value) and then use a set collection to hold the information.

I hope that helps

Regards,
Edwin
  
> Criteria API based on MapKeyColumn is not working
> -------------------------------------------------
>
>                 Key: HHH-6103
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-6103
>             Project: Hibernate Core
>          Issue Type: Bug
>         Environment: Hibernate 3.6.1
> MySQL 5.0
>            Reporter: Edwin Dalorzo
>         Attachments: log.txt, src.zip
>
>
> I defined an Employee entity having a map of phone numbers, where the key is the type of phone (home, office, mobile) and the value is the phone number as a String.
> @ElementCollection(fetch=FetchType.LAZY)
> @CollectionTable(name="emp_phone")
> @MapKeyColumn(name="phone_type")
> @Column(name="phone_num")
> private Map<String, String> phoneNumbers;
> Then I tried to write a Criteria API query using a map join, like this:
> CriteriaBuilder builder = entityManager.getCriteriaBuilder();
> CriteriaQuery<Employee> criteria = builder.createQuery(Employee.class);
> Root<Employee> employeeRoot = criteria.from(Employee.class);
> criteria.select(employeeRoot);
> MapJoin<Employee, String, String> phoneRoot = employeeRoot.joinMap("phoneNumbers");
>             
> criteria.where(builder.equal(phoneRoot.key(), "HOME"));
> System.out.println(entityManager.createQuery(criteria).getResultList());
> You get an NullPointerException
> java.lang.NullPointerException
> 	at org.hibernate.ejb.criteria.path.AbstractPathImpl.prepareAlias(AbstractPathImpl.java:246)
> 	at org.hibernate.ejb.criteria.path.AbstractPathImpl.render(AbstractPathImpl.java:253)
> 	at org.hibernate.ejb.criteria.predicate.ComparisonPredicate.render(ComparisonPredicate.java:173)
> 	at org.hibernate.ejb.criteria.QueryStructure.render(QueryStructure.java:258)
> 	at org.hibernate.ejb.criteria.CriteriaQueryImpl.render(CriteriaQueryImpl.java:340)
> 	at org.hibernate.ejb.criteria.CriteriaQueryCompiler.compile(CriteriaQueryCompiler.java:223)
> 	at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:441)
> 	at com.dalorzo.Main.main(Main.java:57)
> Exception in thread "main" java.lang.RuntimeException: java.lang.NullPointerException
> 	at com.dalorzo.Main.main(Main.java:64)
> Caused by: java.lang.NullPointerException
> 	at org.hibernate.ejb.criteria.path.AbstractPathImpl.prepareAlias(AbstractPathImpl.java:246)
> 	at org.hibernate.ejb.criteria.path.AbstractPathImpl.render(AbstractPathImpl.java:253)
> 	at org.hibernate.ejb.criteria.predicate.ComparisonPredicate.render(ComparisonPredicate.java:173)
> 	at org.hibernate.ejb.criteria.QueryStructure.render(QueryStructure.java:258)
> 	at org.hibernate.ejb.criteria.CriteriaQueryImpl.render(CriteriaQueryImpl.java:340)
> 	at org.hibernate.ejb.criteria.CriteriaQueryCompiler.compile(CriteriaQueryCompiler.java:223)
> 	at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:441)
> 	at com.dalorzo.Main.main(Main.java:57)
> If you change the query to filter on value instead of key, it works just fine
> criteria.where(builder.equal(phoneRoot.value(), "88750372"));
> I have attached a log with exceptions as well as the Employee entity and the Main class containing a main method.
> Also, the code in question runs perfectly fine in EclipseLink.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list