Use Hibernate Criteria for filtering keys and values in Map

穿精又带淫゛_ 提交于 2019-12-05 00:52:54

Please confirm if you've used indeces or indices. The correct usage is as follows:

criteria.createAlias("code.properties", "p");
criteria.add(Restrictions.eq("p.indices", "foo"));
criteria.list();

Alternatively, you can use

eq("p." + CollectionPropertyNames.COLLECTION_INDICES)  

in the Restriction.

Please let me know if it still doesn't work for you.

Tim

This may not be what you are expecting, but in higher version of hibernate the alias is not being resolved i had an issue in Projections when using the alias refer https://stackoverflow.com/questions/36607042/hibernate-criteria-query-using-projections-aggregate-function-alias-throws-sette

pioardi

There are various problems with your approach: Watching MapTest folder in your project github I can advise you to always call the variable names as column names, and to use those same names in the Criteria. The Criteria are maps, whose first argument is the attribute name and the second argument is the value of attribute. For many to many relationship you can try to use :

  @JoinTable(
      name="docente_classe",
      joinColumns=
          @JoinColumn(name="id_dipendente", referencedColumnName="id_dipendente"),
      inverseJoinColumns=
          @JoinColumn(name="id_classe", referencedColumnName="id_classe")
  )

This is JPA annotation , I don't know if this compile on your project.

https://github.com/tredue1/School_Manager/blob/master/code/SM/src/main/java/agc2/it/sm/gestioneDocente/DocenteJpa.java

For query on many to many relationship watch this post :

jpa criteria for many to many relationship .

I don't believe you can do this with the legacy Hibernate Criteria API and there is a small limitation with Hibernate's implementation using the JPA criteria API. One way to solve this would be:

Get a List<CodeProperty> by querying that entity directly with your predicate requirements.

List<CodeProperty> propertyKeys = entityManager
  .createQuery( "FROM CodeProperty p WHERE p.name = :name" )
  .setParameter( "name", "foo" )
  .getResultList();

Query your Code entity using JPA criteria.

// setup the query
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Code> query = cb.createQuery( Code.class );
Root<Code> root = query.from( Code.class );
MapJoin<Code, CodeProperty, CodeValue> mapRoot = root.joinMap( "propertiesMap" );
query.select( root ).where( mapRoot.key().in( propertyKeys ) );

// get results
List<Code> results = entityManager.createQuery( query ).getResultList();

I tried to combine these using:

query
  .select( root )
  .where ( cb.eq( mapRoot.key().get( "name" ), "foo" ) )

The problem is this results in a NotYetImplemented exception :(.

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