Fetching multiple bags efficiently - java

I'm developing a multilingual application. For this reason many objects have in their name and description fields collections of something I call LocalizedStrings instead of plain strings. Every LocalizedString is basically a pair of a locale and a string localized to that locale.
Let's take an example an entity, let's say a book -object.
public class Book{
#OneToMany
private List<LocalizedString> names;
#OneToMany
private List<LocalizedString> description;
//and so on...
}
When a user asks for a list of books, it does a query to get all the books, fetches the name and description of every book in the locale the user has selected to run the app in, and displays it back to the user.
This works but it is a major performance issue. For the moment hibernate makes one query to fetch all the books, and after that it goes through every single object and asks hibernate for the localized strings for that specific object, resulting in a "n+1 select problem". Fetching a list of 50 entities produces about 6000 rows of sql commands in my server log.
I tried making the collections eager but that lead me to the "cannot simultaneously fetch multiple bags"-issue.
Then I tried setting the fetch strategy on the collections to subselect, hoping that it would do one query for all books, and after that do one query that fetches all LocalizedStrings for all the books. Subselects didn't work in this case how i would have hoped and it basically just did exactly the same as my first case.
I'm starting to run out of ideas on how to optimize this.
So in short, what fetching strategy alternatives are there when you are fetching a collection and every element in that collection has one or multiple collections in itself, which has to be fetch simultaneously.

You said
I tried setting the fetch strategy on the collections to subselect, hoping that it would do one query for all books
You can, but you need to access some property to throw the subselect
#Entity
public class Book{
private List<LocalizedString> nameList = new ArrayList<LocalizedString>();
#OneToMany(cascade=javax.persistence.CascadeType.ALL)
#org.hibernate.annotations.Fetch(org.hibernate.annotations.FetchMode.SUBSELECT)
public List<LocalizedString> getNameList() {
return this.nameList;
}
private List<LocalizedString> descriptionList = new ArrayList<LocalizedString>();
#OneToMany(cascade=javax.persistence.CascadeType.ALL)
#org.hibernate.annotations.Fetch(org.hibernate.annotations.FetchMode.SUBSELECT)
private List<LocalizedString> getDescriptionList() {
return this.descriptionList;
}
}
Do as follows
public class BookRepository implements Repository {
public List<Book> getAll(BookFetchingStrategy fetchingStrategy) {
switch(fetchingStrategy) {
case BOOK_WITH_NAMES_AND_DESCRIPTIONS:
List<Book> bookList = session.createQuery("from Book").list();
// Notice empty statement in order to start each subselect
for (Book book : bookList) {
for (Name address: book.getNameList());
for (Description description: book.getDescriptionList());
}
return bookList;
}
}
public static enum BookFetchingStrategy {
BOOK_WITH_NAMES_AND_DESCRIPTIONS;
}
}
I have done the following one to populate the database
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
// Ten books
for (int i = 0; i < 10; i++) {
Book book = new Book();
book.setName(RandomStringUtils.random(13, true, false));
// For each book, Ten names and descriptions
for (int j = 0; j < 10; j++) {
Name name = new Name();
name.setSomething(RandomStringUtils.random(13, true, false));
Description description = new Description();
description.setSomething(RandomStringUtils.random(13, true, false));
book.getNameList().add(name);
book.getDescriptionList().add(description);
}
session.save(book);
}
session.getTransaction().commit();
session.close();
And to retrieve
session = sessionFactory.openSession();
session.beginTransaction();
List<Book> bookList = session.createQuery("from Book").list();
for (Book book : bookList) {
for (Name address: book.getNameList());
for (Description description: book.getDescriptionList());
}
session.getTransaction().commit();
session.close();
I see
Hibernate:
select
book0_.id as id0_,
book0_.name as name0_
from
BOOK book0_
Hibernate: returns 100 rows (as expected)
select
namelist0_.BOOK_ID as BOOK3_1_,
namelist0_.id as id1_,
namelist0_.id as id1_0_,
namelist0_.something as something1_0_
from
NAME namelist0_
where
namelist0_.BOOK_ID in (
select
book0_.id
from
BOOK book0_
)
Hibernate: returns 100 rows (as expected)
select
descriptio0_.BOOK_ID as BOOK3_1_,
descriptio0_.id as id1_,
descriptio0_.id as id2_0_,
descriptio0_.something as something2_0_
from
DESCRIPTION descriptio0_
where
descriptio0_.BOOK_ID in (
select
book0_.id
from
BOOK book0_
)
Three select statements. No "n + 1" select problem. Be aware i am using property access strategy instead of field. Keep this in mind.

You can set a batch-size on your bags, when one unitialized collection is initialized, Hibernate will initialize a some other collections with a single query
More in the Hibernate doc

Related

Integrity constraint violation when deleting object in ManyToMany relationship

I have a many to many relationship between CATEGORY and PRODUCT in a very basic e-commerce java app.
Category has #ManyToMany relation with product. Therefore there is a table CATEGORY_PRODUCT with two colums CATEGORY_ID and PRODUCTS_ID
I want to delete all relations for certain product in that table, am i doing it right?
public void deleteProduct(long id){
Session session = HibernateUtil.getCurrentSession();
session.beginTransaction();
Product product = session.find(entityClass, id);
String sql = "DELETE FROM PUBLIC.CATEGORY_PRODUCT WHERE PRODUCTS_ID = " + id;
SQLQuery query = session.createSQLQuery(sql);
query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
session.delete(product);
session.getTransaction().commit();
}
The plan is to delete the product but i have "integrity constraint violations" because of the relationship.
Before you call session.delete(product), you need to call query.executeUpdate().
You don't need the query.setResultTransformer() call. You should get rid of that line. The result of the executeUpdate() is an int that says how many records were deleted.
You asked this several months ago and I'm just now seeing it. I assume you aren't still waiting on an answer, but maybe this can help the next person.

sorting a collection in hibernate with an object value which could be null

I have a hibernate class like this:
public class UserActivityLog implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private Users users;
private Servers servers;
private Date time;
private String event;
}
I sort a collection of UserActivityLog objects, using hibernate sorting. here's my hibernate criteria:
criteria.add(Restrictions.ge(sortField, new Timestamp(startDate.getTime())));
//add one day to the real end date for it to be considered in criteria
Date eDate = getEndDate(endDate);
criteria.add(Restrictions.le(sortField, new Timestamp(eDate.getTime())));
if (searchByUser >= 0) {
criteria.add(Restrictions.eq("users.id", searchByUser));
}
if (searchByHostId >= 0) {
criteria.add(Restrictions.eq("servers.id", searchByHostId));
}
if (!searchByEvent.isEmpty()) {
criteria.add(Restrictions.like("event", searchByEvent, MatchMode.ANYWHERE));
}
if(sortColumn.equals("username")) {
sortColumn = "users.username";
criteria.createAlias("users", "users");
}
else if (sortColumn.equals("hostName")) {
sortColumn = "servers.hostName";
criteria.createAlias("servers", "servers");
}
//specify the sorting oder
if(SortDirection.asc.equals(sortDirection)) {
criteria.addOrder(Order.asc(sortColumn));
} else {
criteria.addOrder(Order.desc(sortColumn));
}
List<Object> allRows = (ArrayList<Object>) criteria.list();
The servers property of the UserActivityLog can be null. When the collection has an object with servers null, and if I sort the collection using hostName which is a property in the Servers object, the sorted collection does not contain that object with servers = null. Is there any reason for that or am I doing something wrong?
Update:
Here is the hibernate query:
select count(*) as y0_ from USER_ACTIVITY_LOG this_ inner join SERVERS servers1_ on this_.HOST_ID=servers1_.ID
where this_.TIME>='2014/9/16' and this_.TIME<='2014/9/25' order by servers1_.HOST_NAME asc
I would guess that the objects with servers = null is not been fetched from the database because of:
if (searchByHostId >= 0) {
criteria.add(Restrictions.eq("servers.id", searchByHostId));
}
Not because of the sort.
I found both the root cause and the solution:
When I inspect the hibernate query corresponding to this sort, it has an inner join with the Servers table (the query is shown in the question). When there's a log entry with the server = null, the join fails therefore that record is not displayed.
What I did to fix the issue is to force a LEFT JOIN for that sort column:
if (sortColumn.equals("hostName")) {
sortColumn = "servers.hostName";
criteria.createAlias("servers", "servers", JoinType.LEFT_OUTER_JOIN);
}

How to write HIbernate Criteria for a query with subselects

I have two entities Issue and Issue_Tracker. I am using hibernate 3.6 and one to many association.
Issue.java
public class Issue implements Serializable {
private Integer issue_id;
private String issue_description;
private Date issue_raised_date;
private Set<Issue_Tracker> issueTracker = new HashSet<Issue_Tracker>(0);
#OneToMany(fetch=FetchType.LAZY, mappedBy="issue_id")
public Set<Issue_Tracker> getIssueTracker() {
return issueTracker;
}
public void setIssueTracker(Set<Issue_Tracker> issueTracker) {
this.issueTracker = issueTracker;
Issue_Tracker.java
public class Issue_Tracker implements Serializable
{
private Integer issue_id;
private String tracker_status;
private Timestamp tracked_time;**
And this is the sql query, how to achieve this using criteria
SELECT i.issue_id, i.issue_description,
it.tracker_status, it.tracked_time
FROM issues i
LEFT JOIN ( SELECT it.issue_id, it.tracker_status, it.tracked_time
FROM issue_tracker it
INNER JOIN (SELECT issue_id, MAX(tracked_time) tracked_time
FROM issue_tracker GROUP BY issue_id
) A ON it.issue_id = A.issue_id AND it.tracked_time = A.tracked_time
) it ON i.issue_id = it.issue_id
WHERE i.status = "Escalate To";
First of all I suggest you change the confusing name of Issue.issueTracker to Issue.issueTrackers since it is a Set. It makes things easier to read when you are querying. But whatever.
In Criteria API I do not think you can directly translate this SQL. You are better off writing a description of what result set you want. Do you want the last tracked time for all issues with "Escalate To" status? If so this should be close to what you want.
DetachedCriteria inner = DetachedCriteria.forClass(IssueTracker.class, "inner")
.setProjection(Projections.max("inner.tracked_time"));
Criteria crit = session.createCriteria(Issue.class, "issue");
// Add the join with an ON clause (I am not sure why you need the LEFT JOIN)
crit.createAlias("issueTracker", "it", Criteria.LEFT_JOIN,
Subqueries.eqProperty("it.tracked_time", inner));
// Specify the SELECT fields
crit.setProjections(Projections.projectionList()
.add(Projections.property("issue_id"))
// etc
.add(Projections.property("it.tracked_time"));
crit.add(Restrictions.eq("status", "Escalate To");
List<Object[]> rows = crit.list();

How to configure a JDO transaction to simulate creating a database sequence in App Engine Java?

This syntax does not produce unique values across different Fetch Groups:
#Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private long id;
So I've written the method below to simulate generating a database sequence for my Order model. But what I'm not sure about is what the transactional state needs to be configured as here (isolation level / nontransactional read / nontransactional write / optimistic / etc.):
public long getNextId()
{
PersistenceManager pm = this.getPm();
Transaction tx = pm.currentTransaction();
tx.begin();
long nextId = 0;
Query query = pm.newQuery("select id from orders order by id desc");
query.setRange(0, 1);
try
{
List<Order> results = (List<Order>) query.execute();
if (results.iterator().hasNext())
{
for (Order r : results)
{
nextId = r.getId() + 1;
}
}
else
{
return 0;
}
}
catch (Exception e)
{
return 0;
}
tx.commit();
return nextId;
}
Does the scope of the transaction need to be broader than just this method? In other words, should it also include the insert action for the new Order?
I want to make sure that no two Orders that I insert can have the same id value across the entire application.
IDs generated with IdGeneratorStrategy.SEQUENCE are unique for all entities with the same parent. If your entities are root entities (Eg, no parent), then they will all get unique IDs. What is your use case where you have child entities, but need a unique ID across all of them?
Whats wrong with IdGeneratorStrategy.SEQUENCE ? since GAE/J claims to support it

Hibernate: same generated value in two properties

I want the first to be generated:
#Id
#Column(name = "PRODUCT_ID", unique = true, nullable = false, precision = 12,
scale = 0)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "PROD_GEN")
#BusinessKey
public Long getAId() {
return this.aId;
}
I want the bId to be initially exactly as the aId. One approach is to insert the entity, then get the aId generated by the DB (2nd query) and then update the entity, setting the bId to be equal to aId (3rd query). Is there a way to get the bId to get the same generated value as aId?
Note that afterwards, I want to be able to update bId from my gui.
If the solution is JPA, even better.
Choose your poison:
Option #1
you could annotate bId as org.hibernate.annotations.Generated and use a database trigger on insert (I'm assuming the nextval has already been assigned to AID so we'll assign the curval to BID):
CREATE OR REPLACE TRIGGER "MY_TRIGGER"
before insert on "MYENTITY"
for each row
begin
select "MYENTITY_SEQ".curval into :NEW.BID from dual;
end;
I'm not a big fan of triggers and things that happen behind the scene but this seems to be the easiest option (not the best one for portability though).
Option #2
Create a new entity, persist it, flush the entity manager to get the id assigned, set the aId on bId, merge the entity.
em.getTransaction().begin();
MyEntity e = new MyEntity();
...
em.persist(e);
em.flush();
e.setBId(e.getAId());
em.merge(e);
...
em.getTransaction().commit();
Ugly, but it works.
Option #3
Use callback annotations to set the bId in-memory (until it gets written to the database):
#PostPersist
#PostLoad
public void initialiazeBId() {
if (this.bId == null) {
this.bId = aId;
}
}
This should work if you don't need the id to be written on insert (but in that case, see Option #4).
Option #4
You could actually add some logic in the getter of bId instead of using callbacks:
public Long getBId() {
if (this.bId == null) {
return this.aId;
}
return this.bId;
}
Again, this will work if you don't need the id to be persisted in the database on insert.
If you use JPA, after inserting the new A the id should be set to the generated value, i tought (maybe it depends on which jpa provider you use), so no 2nd query needed. then set bld to ald value in your DAO?

Categories

Resources