So my problem is that I need to find all the recently deleted entities of a particular class, that is the entities which have been deleted since a particular timestamp. Specifically, I want to find entities deleted within the last hour.
All my entities have a created and updated timestamp which I maintain correctly with a listener:
#NotNull
#Column(name = "updated")
#Type(type="org.joda.time.contrib.hibernate.PersistentDateTime")
private DateTime updated;
I also use Envers and annotate my entities.
So to guess, my query should start like this:
// Query for deleted bookings
AuditReader reader = AuditReaderFactory.get(entityManager);
AuditQuery query = reader.createQuery()
.forRevisionsOfEntity(Booking.class, false, true)
but I don't know what to put here to find the deleted Booking's since a DateTime.
First, get a timestamp for one hour ago (in milliseconds):
long timestamp = (System.getCurrentTimeMillis()) - (60*60*1000);
Then you can query relative to the timestamp:
AuditReader reader = AuditReaderFactory.get(entityManager);
AuditQuery query = reader.createQuery()
.forRevisionsOfEntity(Booking.class, false, true)
.add(AuditEntity.revisionProperty("timestamp").gt(timestamp)
.add(AuditEntity.revisionType().eq(RevisionType.DEL));
List<Object[]> results = query.getResultList();
to get the revision data. Each Object[] has
revision meta data (DefaultRevisionEntity or your own class annotated with #RevisionEntity(CustomRevisionListener.class))
the entity instance (the Booking in this case)
the RevisionType, which we know will always be DEL in this case
I find the Envers API quite limited and sometimes have to turn to use just plain JPA. However, this is not one of those cases, I believe you can achieve your use case by doing the following:
AuditQuery query = reader.createQuery().forRevisionsOfEntity(classType, false, true)
.add(AuditEntity.revisionType().eq(RevisionType.DEL))
.addProjection(AuditEntity.property(ID).distinct())
.add(AuditEntity.revisionNumber().gt(revisionNumber);
The above example uses the revision number but you could easily retrieve the revision number from the start date you are looking for.
Related
I get a list of entity from database using the code below:
List<NeginHotData> neginHotData = getNeginHotDataByStatus(null);
Then i need to change the Status field of them from NULL to "Active" and that's OK, the problem is that ,Is it possible to do this without iterating through the list of objects and updating them one by one? That doesn't sound efficient to me.
I need a solution to update them completely with each other because i have about 7000 record and iterating through the list of objects and updating them one by one isn't really a good solution .
You can use HQL like this:
String hqlUpdate = "update NeginHotData c set c.status = :status where c.bla= :bla";
int updatedEntities = s.createQuery( hqlUpdate ).setString( "bla", bla).setString( "status", status).executeUpdate();
This works only if you set the same status for all entities involved.
I got document that looks like this
#Document(collection="myDocument")
public class MyDocument {
#Id
private String id;
private List<Dates> dates;
}
public class Dates{
private String key;
private DateTime value;
}
And OtherDocument is container for DateTime values from various sources, I can't simply make fields like DateTime birthdate; inside MyDocument because I don't know what key will be, they are just some dates that describe MyDocument. Now, I need to create search engine for those values, for example, someone want's to find all MyDocuments with dates that contains:
key : "Birthdate" greater than
value : "1990-01-01 00:00:00 +00:00"
and key : "Mather's birthday" less than
value: "1975-01-01 00:00:00 +00:00"
So, Criteria (using MongoTemplate here) first may look like this
Criteria criteria = Criteria.where("myDocument.dates.value")
.exists(true)
.gt(DateTimeUtil.valueOf("1990-01-01 00:00:00 +00:00")) //just converting String to DateTime here
.and("myDocument.dates.name")
.exists(true)
.all("Birthday"));
And second one:
Criteria criteria = Criteria.where("myDocument.dates.value")
.exists(true)
.lt(DateTimeUtil.valueOf("1975-01-01 00:00:00 +00:00"))
.and("myDocument.dates.name")
.exists(true)
.all("Mather's birthday"));
The problem is, I can't put those both Criteria in one Query, it will cause error. The only soultion I found till now is to make 2 separate Query in that case and then find common part by using
resultA.retainAll(resultB)
But the point is, I don't want to, this database will store a lot of data and those requests will be very frequent. I need this to work fast, and combining 2 lists in pure Java will be slow as hell with that amount of data. Any ideas how to deal with that?
edit#
here is the error thrown when I try to combine 2 Criteria like this in one Query
caught: (java.lang.RuntimeException), msg(json can't serialize type :
class org.joda.time.DateTime) java.lang.RuntimeException: json can't
serialize type : class org.joda.time.DateTime
You can use below code. $and the query together and use $elemMatch to match the dates fields on multiple condition.
Something like
Criteria criteria1 = Criteria.where("dates").
elemMatch(
Criteria.where("value").exists(true).gt(DateTimeUtil.valueOf("1990-01-01 00:00:00 +00:00"))
.and("name").exists(true).all("Birthday")
);
Criteria criteria2 = Criteria.where("dates").
elemMatch(
Criteria.where("value").exists(true).lt(DateTimeUtil.valueOf("1975-01-01 00:00:00 +00:00"))
.and("name").exists(true).all("Mather's birthday")
);
Criteria criteria = new Criteria().andOperator(criteria1, criteria2);
Note: You may still have the problem with joda time conversion.
I am fairly new to spring so maybe I am missing the wrong terminology and that is the reason why my searches brought nothing up.
The Query I am looking for should Take a parent object and remove all Child objects where a timestamp is not of a specific date.
Parent Id field1 field2
Child: Id parentId field 1 timestamp (datetime)
I tried several different approaches but nothing seems to work.
Alternatively I could try something like:
FindAllChildsByIdAndTimestamp(int id, Date date)
And fill a empty Parent with the data. But event hat does not work.
Any Idea what i am doing wrong?
Best and Thank you
You don't even need to bring a parent into this. All you need to do is to create a query on a child table and generate a query on that table with where clause would be parentId is and timestamp is . You mentioned that you are using Spring (which version BTW?) but didn't say what do you use to access your DB (Spring Data, Hibernate, JPA, JBC?) Also which DB and which version? I will give you example in Hibernate:
sessionFactory.getCurrentSession().createCriteria(Child.class)
.add(Restrictions.eq("parentId", yourParentId))
.add(Restrictions.gt("timestamp", date1))
.add(Restrictions.le("timestamp", date2))
.list();
date1 is midnight of your day and date2 is midnight of the following day. This should give you the list of your children
I solved the problem by seperating the two requests.
public interface ChildRepository extends CrudRepository<Child, Serializable> {
List<child> findAllBytimeStampBetweenAndParent(Date dayBegin, Date dayEnd, Parent parent);
And then attributed it to the parent
Parent parent = this.getById(id);
// Convert Date Type
DateTime dayTimeEnd = new DateTime(date);
dayTimeEnd = dayTimeEnd.plusHours(23);
dayTimeEnd = dayTimeEnd.plusMinutes(55);
Date dayEnd = dayTimeEnd.toDate();
// Get all Tracking Data of this Date
List<Child> ChildList = this.childRepository.findAllBytimeStampBetweenAndParent(date, dayEnd, parent);
as an alternative i could have used direct queries.
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/
I have an action in struts2 that will query the database for an object and then copy it with a few changes. Then, it needs to retrieve the new objectID from the copy and create a file called objectID.txt.
Here is relevant the code:
Action Class:
ObjectVO objectVOcopy = objectService.searchObjects(objectId);
//Set the ID to 0 so a new row is added, instead of the current one being updated
objectVOcopy.setObjectId(0);
Date today = new Date();
Timestamp currentTime = new Timestamp(today.getTime());
objectVOcopy.setTimeStamp(currentTime);
//Add copy to database
objectService.addObject(objectVOcopy);
//Get the copy object's ID from the database
int newObjectId = objectService.findObjectId(currentTime);
File inboxFile = new File(parentDirectory.getParent()+"\\folder1\\folder2\\"+newObjectId+".txt");
ObjectDAO
//Retrieve identifying ID of copy object from database
List<ObjectVO> object = getHibernateTemplate().find("from ObjectVO where timeStamp = ?", currentTime);
return object.get(0).getObjectId();
The problem is that more often than not, the ObjectDAO search method will not return anything. When debugging I've noticed that the Timestamp currentTime passed to it is usually about 1-2ms off the value in the database. I have worked around this bug changing the hibernate query to search for objects with a timestamp within 3ms of the one passed, but I'm not sure where this discrepancy is coming from. I'm not recalculating the currentTime; I'm using the same one to retrieve from the database as I am to write to the database. I'm also worried that when I deploy this to another server the discrepancy might be greater. Other than the objectID, this is the only unique identifier so I need to use it to get the copy object.
Does anyone know why this is occuring and is there a better work around than just searching through a range? I'm using Microsoft SQL Server 2008 R2 btw.
Thanks.
Precision in SQL Server's DATETIME data type does not precisely match what you can generate in other languages. SQL Server rounds to the nearest 0.003 - this is why you can say:
DECLARE #d DATETIME = '20120821 23:59:59.997';
SELECT #d;
Result:
2012-08-21 23:59:59.997
Then try:
DECLARE #d DATETIME = '20120821 23:59:59.999';
SELECT #d;
Result:
2012-08-22 00:00:00.000
Since you are using SQL Server 2008 R2, you should make sure to use the DATETIME2 data type instead of DATETIME.
That said, #RedFilter makes a good point - why are you relying on the time stamp when you can use the generated ID instead?
This feels wrong.
Other than the objectID, this is the only unique identifier
Databases have the concept of a unique identifier for a reason. You should really use that to retrieve an instance of your object.
You can use the get method on the Hibernate session and take advantage of the session and second level caches as well.
With your approach you execute a query everytime you retrieve your object.
So, I'm getting a number of instances of a particular entity by id:
for(Integer songId:songGroup.getSongIds()) {
session = HibernateUtil.getSession();
Song song = (Song) session.get(Song.class,id);
processSong(song);
}
This generates a SQL query for each id, so it occurred to me that I should do this in one, but I couldn't find a way to get multiple entities in one call except by running a query. So I wrote a query
return (List) session.createCriteria(Song.class)
.add(Restrictions.in("id",ids)).list();
But, if I enable 2nd level caching doesn't that mean that my old method would be able to return the objects from the 2nd level cache (if they had been requested before) but my query would always go to the database.
What the correct way to do this?
What you're asking to do here is for Hibernate to do special case handling for your Criteria, which is kind of a lot to ask.
You'll have to do it yourself, but it's not hard. Using SessionFactory.getCache(), you can get a reference to the actual storage for cached objects. Do something like the following:
for (Long id : allRequiredIds) {
if (!sessionFactory.getCache().containsEntity(Song.class, id)) {
idsToQueryDatabaseFor.add(id)
} else {
songs.add(session.get(Song.class, id));
}
}
List<Song> fetchedSongs = session.createCriteria(Song.class).add(Restrictions.in("id",idsToQueryDatabaseFor).list();
songs.addAll(fetchedSongs);
Then the Songs from the cache get retrieved from there, and the ones that are not get pulled with a single select.
If you know that the IDs exist, you can use load(..) to create a proxy without actually hitting the DB:
Return the persistent instance of the given entity class with the given identifier, obtaining the specified lock mode, assuming the instance exists.
List<Song> list = new ArrayList<>(ids.size());
for (Integer id : ids)
list.add(session.load(Song.class, id, LockOptions.NONE));
Once you access a non-identifier accessor, Hibernate will check the caches and fallback to DB if needed, using batch-fetching if configured.
If the ID doesn't exists, a ObjectNotFoundException will occur once the object is loaded. This might be somewhere in your code where you wouldn't really expect an exception - you're using a simple accessor in the end. So either be 100% sure the ID exists or at least force a ObjectNotFoundException early where you'd expect it, e.g. right after populating the list.
There is a difference between hibernate 2nd level cache to hibernate query cache.
The following link explains it really well: http://www.javalobby.org/java/forums/t48846.html
In a nutshell,
If you are using the same query many times with the same parameters then you can reduce database hits using a combination of both.
Another thing that you could do is to sort the list of ids, and identify subsequences of consecutive ids and then query each of those subsequences in a single query. For example, given List<Long> ids, do the following (assuming that you have a Pair class in Java):
List<Pair> pairs=new LinkedList<Pair>();
List<Object> results=new LinkedList<Object>();
Collections.sort(ids);
Iterator<Long> it=ids.iterator();
Long previous=-1L;
Long sequence_start=-1L;
while (it.hasNext()){
Long next=it.next();
if (next>previous+1) {
pairs.add(new Pair(sequence_start, previous));
sequence_start=next;
}
previous=next;
}
pairs.add(new Pair(sequence_start, previous));
for (Pair pair : pairs){
Query query=session.createQuery("from Person p where p.id>=:start_id and p.id<=:end_id");
query.setLong("start_id", pair.getStart());
query.setLong("end_id", pair.getEnd());
results.addAll((List<Object>)query.list());
}
Fetching each entity one by one in a loop can lead to N+1 query issues.
Therefore, it's much more efficient to fetch all entities at once and do the processing afterward.
Now, in your proposed solution, you were using the legacy Hibernate Criteria, but since it's been deprecated since Hibernate 4 and will probably be removed in Hibernate 6, so it's better to use one of the following alternatives.
JPQL
You can use a JPQL query like the following one:
List<Song> songs = entityManager
.createQuery(
"select s " +
"from Song s " +
"where s.id in (:ids)", Song.class)
.setParameter("ids", songGroup.getSongIds())
.getResultList();
Criteria API
If you want to build the query dynamically, then you can use a Criteria API query:
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<Song> query = builder.createQuery(Song.class);
ParameterExpression<List> ids = builder.parameter(List.class);
Root<Song> root = query
.from(Song.class);
query
.where(
root.get("id").in(
ids
)
);
List<Song> songs = entityManager
.createQuery(query)
.setParameter(ids, songGroup.getSongIds())
.getResultList();
Hibernate-specific multiLoad
List<Song> songs = entityManager
.unwrap(Session.class)
.byMultipleIds(Song.class)
.multiLoad(songGroup.getSongIds());
Now, the JPQL and Criteria API can benefit from the hibernate.query.in_clause_parameter_padding optimization as well, which allows you to increase the SQL statement caching mechanism.
For more details about loading multiple entities by their identifier, check out this article.