简体   繁体   中英

Hibernate mapping of List<byte[]> "Not an entity"

Hey everyone thank you for reading!

I am using hibernate and JPA to store a class with several annotated values. One of these is this one:

@ElementCollection(fetch = FetchType.EAGER)
@Fetch(value = FetchMode.SUBSELECT)
private List<byte[]> productThumbnails;

Now I am trying to optimize the database operations to 1) not include the thumbnails in the query. For this I am using a CriteriaQuery with multiselect on all the other fields

criteriaQuery.multiselect(root.get("id"),root.get("userId"), root.get("price"));

2) Only get the thumbnails via the id

CriteriaBuilder cb = getInstance().getCriteriaBuilder();
CriteriaQuery<byte[]> query = cb.createQuery(byte[].class);
Root<Product> product = query.from(Product.class);
query.multiselect(product.get(Product_.productThumbnails));
TypedQuery<byte[]> typedQuery = getInstance().createQuery(query);
List<byte[]> resultList = typedQuery.getResultList();
resultList.forEach(System.out::println);
getInstance().close();

For this I also created a static metamodel class:

@StaticMetamodel(Product.class)
public class Product_ {
    public static volatile SingularAttribute<Product, Integer> id;
    public static volatile ListAttribute<Product, List<byte[]>> productThumbnails;
}

If I am now running this code, I am getting the following error:

javax.servlet.ServletException: java.lang.IllegalArgumentException: org.hibernate.QueryException: not an entity [select generatedAlias0.productThumbnails from de.adesso.data.Product as generatedAlias0]

Did anyone else run into such a problem? Any help would be greatly appreciated! Thanks!

The JavaDoc for

CriteriaQuery<T> multiselect(Selection<?>... selections)

states:

"An argument to the multiselect method must not be a tuple- or array-valued compound selection item."

Haven't tried it myself, but maybe try

query.select(product.get(Product_.productThumbnails));

instead of

query.multiselect(product.get(Product_.productThumbnails));

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