#OrderColumn in JPQL query - jpa

I am having a #ManyToMany mapping with #OrderColumn as follows:
class Tag {
#ManyToMany(fetch = FetchType.LAZY) #Getter
#JoinTable(
name = "tag_graph",
inverseJoinColumns = #JoinColumn(name = "parent_id"))
private Set<Tag> parents = new TreeSet<>();
#ManyToMany(fetch = FetchType.LAZY, mappedBy = "parents") #Getter #OrderColumn
private List<Tag> children = new ArrayList<>();
}
My problem is that I want to write an JPQL query which would use the #OrderColumn of the generated #JoinTable. Something like:
SELECT t FROM Tag t WHERE ... ORDER BY t.ORDER_COLUMN
Is there a way to do that?

You should be able to use the INDEX function in JPQL.
See,
http://en.wikibooks.org/wiki/Java_Persistence/JPQL#Special_Operators

There is no need to specify the order column in queries, it's automatically used behind the scenes to preserve the order in which elements have been added to the relationship list. For a better explanation you can refer to this link.

Related

JPA CriteriaBuilder JOIN not working for collection

I have two entities, in one to many relationship. I am trying to join the collection of entities, but can't wrap my head around it how to use the framework. I always used hibernate's DetachedCriteria but is not an option for me anymore, any help would be great.
#Entity
#Table(name = "Project")
public class Project implements Serializable {
....
#OneToMany(cascade = CascadeType.ALL, mappedBy = "project")
private Collection<WorkReport> workReportCollection;
....
#Data
#Entity
#Table(name = "work_report")
public class WorkReport implements Serializable {
#JoinColumn(name = "id_work_report", referencedColumnName = "id_work_report", insertable = false, updatable = false)
#ManyToOne(optional = false)
private Project project;
And I am trying to join workReportCollection like this, but it always throws
LazyInit Exception
when accessing the field.
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Project> query = builder.createQuery(Project.class);
Root<Project> project = query.from(Project.class);
Predicate idPredicate = builder.equal(project.get("idProject"), idProject);
project.joinCollection("workReportCollection", JoinType.LEFT);
query.where(idPredicate);
TypedQuery<Project> q = em.createQuery(query);
return q.getSingleResult();
Only thing that works is using fetch instead of join but it fetches all other associations as well and that is too much data.
How to write a join correctly with JPA CriteriaBuilder? OR Should I use a fetch with some projection?
The join is correct but the collection is not initialized when you access it that's why you get the LazyInitException.
You have to add fetch:
project.fetch("workReportCollection");
to advice JPA to initialize the collection after querying.

JPA Annotations - How to retrieve all entities with a specific column value

Let say I have an entity object Customer with an "OneToMany" relation to Order. I want that when ever a "Customer" get loaded, only his orders with the Id = 1234, 5678 get loaded to.
Any ideas?
#Entity
#Table(name = "Customer")
public class Customer extends TraceableJPA {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "customer_id")
private Long id;
#OneToMany(cascade = CascadeType.ALL, mappedBy = "Customer", targetEntity = Order.class)
#Column(name = "order_id", value = {"1234","5678"} (?))
#OrderBy("isrtdate ASC")
#BatchSize(size = 20)
private List<Order> orders = new ArrayList<Order>();
Hibernate
If you use hibernate Session and its abilites , you can always use #FilterJoinTable mechanism.
Check THIS article for more information.
Yet it is not global, you have to predefine this filter and then explicitly configure Session object to use it.
JPA
JPA in its standard has NO SUCH FUNCTIONALITY, for global relations filtering.
You can always filter it in your queries : )

JPA Specification: Select all entities which have at least one param with attribute from list

I have 2 entities with relationship ManyToMany
#Entity
#Table
public class TranslationUnit implements Serializable {
#Id
private Long id;
#ManyToMany(mappedBy = "translationUnit", fetch = FetchType.EAGER)
private Set<Category> categories = new HashSet<>();
}
#Entity
#Table
public class Category implements Serializable {
#ManyToMany
#JoinTable(name = "category_translation_unit",
joinColumns = #JoinColumn(name = "categories_id", referencedColumnName = "id"),
inverseJoinColumns = #JoinColumn(name = "translation_units_id", referencedColumnName = "id"))
private Set<TranslationUnit> translationUnits = new HashSet<>();
}
In Category I have 1 field, which should be used for filtering:
String name;
I need to be able to specify list of Category names (List), and select those TranslationUnits which have at least one Category with specified name.
I have several other filtering options, which should be used together, and I successfully built Specifications for them. But I've stuck with this one.
Please help.
P.S. One of my existing Specifications looks like this:
Specification idSpec = (Specification) (r, q, cb) -> {
List<Predicate> predicates = new ArrayList<>();
if (!filterRequest.getTranslationUnitIds().isEmpty())
predicates.add(r.get(TranslationUnit_.id).in(filterRequest.getTranslationUnitIds()));
return cb.and(predicates.toArray(new Predicate[predicates.size()]));
};
Good day. You could use IN for filtering translation units by category names list. I believe, it will look like this using Criteria API:
Root<TranslationUnit> itemsRoot = ...;
Join join = itemsRoot.join("categories");
List<Predicate> predicates = new ArrayList<>();
predicates(join.get("name").in(categoryNamesList));

JPA join column only for insert/delete , disable select

I have #OneToMAny realationship inside my entity.
Is it possible to disable jpa from generating select for the joined column? beacuse I have many records in my main table and when selecting them , each record performs select for the joined column. I would like to disable this select is that possible?
UPDATE:
I tried inserting fetch LAZY but it still creates a select for Table02f and Table03f...
This is my code:
public class Table01f implements Serializable {
#OneToMany(fetch=FetchType.LAZY , cascade = CascadeType.ALL, mappedBy = "table01f")
private List<Table02f> table02fList;
//getter & setter...
}
public class Table02f implements Serializable {
#JoinColumn(name = "F2PRP", referencedColumnName = "F1PRP", insertable = false, updatable = false)
#ManyToOne(optional = false)
#JsonIgnore
private Table01f table01f;
#OneToMany(fetch=FetchType.LAZY , cascade = CascadeType.ALL, mappedBy = "table02f")
private List<Table03f> table03fList;
//getter & setter...
}
public class Table03f implements Serializable {
#JoinColumns({
#JoinColumn(name = "F3PRP", referencedColumnName = "F2PRP", insertable = false, updatable = false),
#JoinColumn(name = "F3BRN", referencedColumnName = "F2BRN", insertable = false, updatable = false)})
#ManyToOne(optional = false)
#JsonIgnore
private Table02f table02f;
//getter & setter...
}
Thank's In Advance.
Just add the fetch type LAZY to your #OneToMany relationship:
#OneToMany(fetch=FetchType.LAZY)
When you load the list of your main entities, JPA won't populate your list for this relationship, avoiding the generation of the SELECT.
Just have a look at this functionality in JPA documentation so that you can understand how to use it.
If you don't need the data make it LAZY (in general always make everything LAZY).
If you need the data, then you can use batch fetching, or join fetching.
http://java-persistence-performance.blogspot.com/2010/08/batch-fetching-optimizing-object-graph.html

How to make a CriteriaBuilder join with a custom "on" condition?

I want make a query where I join 2 tables, using the CriteriaBuilder. In MySQL the query I'm trying to make would look like this:
SELECT * FROM order
LEFT JOIN item
ON order.id = item.order_id
AND item.type_id = 1
I want to get all orders and if they have an item of type #1, I want to join with this item. However, if no item of type #1 is found, I still want to get the order. I can't figure out how to make this with the CriteriaBuilder. All I know how to make is:
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Order> cq = cb.createQuery(Order.class);
Root<Order> order = cq.from(Order.class);
Join<Order, Item> item = order.join(Order_.itemList, JoinType.LEFT);
Join<Item, Type> type = order.join(Item_.type, JoinType.LEFT);
cq.select(order);
cq.where(cb.equal(type.get(Type_.id), 1));
This query is broke, since it results in something like this in MySQL:
SELECT * FROM order
LEFT JOIN item
ON order.id = item.order_id
WHERE item.type_id = 1
The result will only contain orders with items of type #1. Orders without are excluded. How can I use the CriteriaBuilder to create a query like in the first example?
It is possible starting from the version 2.1 of JPA using the on method Join<Z, X> on(Predicate... restrictions);
Here is how:
Root<Order> order = cq.from(Order.class);
Join<Order, Item> item = order.join(Order_.itemList, JoinType.LEFT);
item.on(cb.equal(item.get(Item_.type), 1));
I think this is the same problem as posed in this question. It looks like it is not possible in CriteriaBuilder. It is possible in Hibernate Criteria API, but that probably won't help you.
JPA Criteria API: Multiple condition on LEFT JOIN
I know this question was made a long time a go, but recently a had the same problem and i found this solution from an Oracle forum, i copied and pasted just in case the link is not longer available.
MiguelChillitupaArmijos 29-abr-2011 1:41 (en respuesta a 840578) Think
you should use something like:
em.createQuery("SELECT DISTINCT e.Id" +
" from Email e " +
" left join e.idEmailIn e2 *with* e2.responseType = 'response'" +
" where e.type = 'in' and e.responseMandatory = true").getSingleResult();
An this is the link.
JPA Criteria : LEFT JOIN with an AND condition
There is a workaround if you are using Hibernate 3.6 with JPA 2.0
It is not the better solution, however it works perfect for me.
I´ve duplicate the entity with the #Where hibernate annotation.It means that everytime you use the join with this entity, hibernate will add the extra condition on the join statement at generated SQL.
For instance, initially we have the follow example:
#Entity
#Table(name = "PERSON")
public class Person {
#Id
#Column(name = "PERSON_ID")
private Long id;
#Id
#Column(name = "PERSON_NAME")
private String name;
#OneToMany(mappedBy = "person", fetch = FetchType.LAZY)
private Set<Address> addresses;
}
#Entity
#Table(name = "ADDRESS")
public class Address {
#Id
#Column(name = "ADDRESS_ID")
private Long id;
#Id
#Column(name = "ADDRESS_STREET")
private String street;
#ManyToOne
#JoinColumn(name = "PERSON_ID")
private Person person;
}
In order to add extra conditions on criteria Join, we need duplicate the Address #Entity mapping , adding the #Where annotation #Where(clause = " ADDRESS_TYPE_ID = 2").
#Entity
#Table(name = "ADDRESS")
#Where(clause = " ADDRESS_TYPE_ID = 2")
public class ShippingAddress {
#Id
#Column(name = "ADDRESS_ID")
private Long id;
#Id
#Column(name = "ADDRESS_STREET")
private String street;
#OneToOne
#JoinColumn(name = "PERSON_ID")
private Person person;
}
Also, we need to add the duplicate mapping association for the new entity.
#Entity
#Table(name = "PERSON")
public class Person {
#Id
#Column(name = "PERSON_ID")
private Long id;
#Id
#Column(name = "PERSON_NAME")
private String name;
#OneToMany(mappedBy = "person", fetch = FetchType.LAZY)
private Set<Address> addresses;
#OneToOne(mappedBy = "person")
private ShippingAddress shippingAddress;
}
Finally, you can use a join with this specific Entity in your criteria :
PersonRoot.join(Person_.shippingAddress, JoinType.LEFT);
The Hibernate Snippet SQL should seems like this :
left outer join
address shippingadd13_
on person11_.person_id=shippingadd13_.person_id
and (
shippingadd13_.ADDRESS_TYPE_ID = 2
)
ON clause is supported in Hibernate 4.3 version, anyone is aware if there is a parameter indexing issue between the parameter index of the additional custom conditions with the index of the existing mapping filters when doing an outer join with ON clause?
Using the Person entity class below as an example, say I am adding this filter to limit the address types and the filter is enabled to populate the IN clause. The parameter index for the IN clause will cause the issue [2] when I add additional conditions (such as using 'street' column) part of the ON clause. Is is a known issue?
[1] #Filter(name = "addressTypes", condition = "ADDRESS_TYPE in (:supportedTypes)")
[2]
Caused by: ERROR 22018: Invalid character string format for type BIGINT.
private Set addresses;