简体   繁体   中英

Setting up CriteriaBuilder

I'm working on a big codebase that frequently makes use of CriteriaBuilder.Coalesce and Root . For example,

criteriaBuilder.coalesce(root.get('adjustedWeight'), root.get('weight'))

I don't understand this syntax. For one, the Coalesce API appears to accept only 1 argument of type Expression<? extends T> Expression<? extends T> or T . I also do not understand what Root is or why it is useful.

In order to help me understand, I'm creating a simplified example that uses the line of code above, with CriteriaBuilder and Root. I have successfully wired up Hibernate and JPA.

Please keep in mind I have ~3 days of Java programming experience. My native langugage is C#

import models.MyEntity;
import org.hibernate.jpa.HibernatePersistenceProvider;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.spi.PersistenceProvider;
import java.util.HashMap;
import java.util.List;


public class Main {
    public static final String SELECT_QUERY = "from MyEntity where title = :title";

    public static void main(String[] args) {
            String title = "secureexam";
            PersistenceProvider persistenceProvider = new HibernatePersistenceProvider();
            EntityManagerFactory entityManagerFactory = persistenceProvider
                    .createEntityManagerFactory("NewPersistenceUnit", new HashMap());
            EntityManager entityManager = entityManagerFactory.createEntityManager();
            List<MyEntity> result = entityManager
                    .createQuery(SELECT_QUERY, MyEntity.class)
                    .setParameter("title", title)
                    .getResultList();

            System.out.println("result is " + result.get(0).getTitle());
            entityManager.close();
    }
}

Can you please explain the first line of code to me regarding criteriaBuilder? Also, how can I modify my code to make use of this line of code?

UPDATE

The article mentioned in Kevin Peters answer worked with slight modification:

    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Entity> criteriaQuery = criteriaBuilder.createQuery(Entity.class);

//Test 1
Root<AssessmentEntity> rootTest1 = criteriaQuery.from(Entity.class);

In other words, CriteriaBuilder and CriteriaQuery had to be instantiated differently for the build to work. EG the whole CriteriaQuery<Entity class> syntax didn't make sense to me.

It seems you got the wrong API documentation, the proper one is this . The coalesce method (like the SQL pendant ) just returns the first non-null value, so either the value of adjustedWeight if that is not null, otherwise the value of weight , or at least null if none of the values is present.

The root variable in your case just represents the table you are working on, respective which represents the root of your query (the SQL query translation would be ... FROM ... ). The name is arbitrary. As database term this would be the selection . This article could also help you.

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