How to return deleted rows using jdbi? - java

How do I return the deleted rows using jdbi? For example, I'd like to do something like this:
public List<User> deleteAndReturnUsers() {
return jdbi.withHandle(handle -> {
return handle.createQuery("DELETE FROM mytable where id = :id")
.bind("id", "someid")
.map(new UserMapper())
.list();
});
}

Postgresql has a returning keyword...
https://dbfiddle.uk/?rdbms=postgres_13&fiddle=25d284309745e40c8ea29945481d1aa2
DELETE FROM data WHERE val >= 2 RETURNING *;
You can then execute that query in the same way as you would a normal SELECT statement, and get a result set back; containing the records deleted.

Related

How to delete a list of entities

I have a JPA query to delete selected entities which is stored in a list. I am currently deleting the entities in a loop via the in built JPA delete() method 1 by 1. Is there a way to just pass in the list instead of looping it?
Current implementation that works but looping to delete 1 by 1. I need the initial query to get list of entities for other reasons thus not looking to change that. Just a way to pass in the list of entiies to be deleted. Please advice. Thanks.
Note: This is with Java 8 and Spring 4.3 if it matters.
#GetMapping("/delete/{name}/{count}")
public String delete(#PathVariable String name, #PathVariable int count){
boolean isDelete = true;
while (isDelete){
//1st query
List<PersonEntity> results = personService.get(name, count);
if(results != null && !results.isEmpty()){
System.out.println("Deleting following: ");
//2nd query
results.forEach(p -> {
System.out.println(p.getName());
personService.delete(p);
});
} else {
isDelete = false;
}
}
return "Done!";
}
You can try something like this:
List<PersonEntity> results = personService.get(name, count);
if(results != null && !results.isEmpty()) {
List<Integer> personIds = results.stream()
.map(personIds)
.collect(Collectors.toList());
personService.deleteManyById(personIds);
In your service:
public void deleteManyById(List<Integer> ids) {
personRepository.deleteByIdIn(ids);
}
In your repo (assuming it's a spring JpaRepository):
void deleteByIdIn(List<Integer> ids);
Just be aware of the fact that dbs have a limit in the number of parameters you can pass in a IN condition

Criteria Api: Order/Select by implicit related table

I try to build a CriteriaQuery which provides the following functionality:
I have three tables with the following fields:
table_a:
id, name_a
table_b:
id, name_b
table_ab:
id_a, id_b
Now I want to get all elements out of table_a ordered by the name_b field of the corresponding element in table_b.
The Result should be a Specification for usage in a JpaRepository. I tried using joins, but i stuck at the point, how to combine the joins:
Specification<TableA> specification = (root, query, cb) -> {
CriteriaQuery<TableAb> abQuery = cb.createQuery(TableAb.class);
CriteriaQuery<TableB> bQuery = cb.createQuery(TableB.class);
Root<TableAb> abRoot = abQuery.from(TableAb.class);
Join<TableAb, TableA> aJoin = abRoot.join("tableA");
Join<TableAb, TableB> bJoin = abRoot.join("tableB");
//combine joins
query.orderBy(cb.asc(/* Expression to order by */));
return cb.conjunction();
};
In my opinion the main problem is that there is no "path" from table_a to table_b, but I explicitly do not want to have any reference inside of table_a to table_b.
Since you're using Spring Data JPA , you can just make an interface with a method on it that look like this:
public interface TableABRepository extends Repository<TableAB, Long> {
public List<TableAB> findAllByOrderByTableB();
}
Assuming your TableAB class is something like this:
class TableAB {
TableA tableA;
TableB tableB;
}
Thak method will return all elements from table_ab ordered by the name_b field.
After that you just get the TableA elements from the TableAB returned list.

Jpa named query: how to get a list of column

I have an entity orderdetails, where a user can have many ordernames I want to get all the ordername by userid using jpa named query. I tried this
SELECT o.orderName FROM OrderDetails o WHERE o.userId=:userId;
Since the return type will be List in the resultset, I executed the query like this
getEntityManager().createNamedQuery("getOrderNamesByUserId",
orderDetail.class).setParameter("userId", userId);
This obviously is not working. How can I get that query working? One way is to iterate the List but I wonder whether there is another way around?
Well this is non-compiled code snippet, you could try the following approach.
#Override
public List<OrderDetails> findOrders(Long userId) {
TypedQuery<OrderDetails> query = entityManager.createNamedQuery(
"OrderDetails.getOrderNamesByUserId", OrderDetails.class);
query.setParameter("userId", userId);
List<OrderDetails> list = query.getResultList();
return list;
}
If you just need to select one column(ordername), use getResultList() method.
Query query = getEntityManager().createNamedQuery("getOrderNamesByUserId", OrderDetail.class);
query.setParameter("userId", userId);
List<String> orderNameList = query.getResultList();

JPA Query.getResultList() - use in a generic way

I'm creating a complex query with multiple tables and need to list the result. Usually, I'm using the EntityManager and map the result to the JPA-Representation:
UserEntity user = em.find(UserEntity.class, "5");
Then I can access all values as the user UserEntity class defines it. But how can I access the field-values returned from a native, multiple-table query? What I get is a List of Objects. That's fine so far, but what "is" that Object? Array? Map? Collection? ...
//simpleExample
Query query = em.createNativeQuery("SELECT u.name,s.something FROM user u, someTable s WHERE s.user_id = u.id");
List list = query.getResultList();
//do sth. with the list, for example access "something" for every result row.
I guess the answer is quite simple, but most examples out there just show the usage when directly casting to a targetClass.
PS: In the example I could use the class-mappings of course. But in my case someTable is not managed by JPA, and therefore I don't have the entity nor do I have a class-representation of it, and since I'm joining like 20 tables, I don't want to create all the classes just to access the values.
General rule is the following:
If select contains single expression and it's an entity, then result is that entity
If select contains single expression and it's a primitive, then result is that primitive
If select contains multiple expressions, then result is Object[] containing the corresponding primitives/entities
So, in your case list is a List<Object[]>.
Since JPA 2.0 a TypedQuery can be used:
TypedQuery<SimpleEntity> q =
em.createQuery("select t from SimpleEntity t", SimpleEntity.class);
List<SimpleEntity> listOfSimpleEntities = q.getResultList();
for (SimpleEntity entity : listOfSimpleEntities) {
// do something useful with entity;
}
If you need a more convenient way to access the results, it's possible to transform the result of an arbitrarily complex SQL query to a Java class with minimal hassle:
Query query = em.createNativeQuery("select 42 as age, 'Bob' as name from dual",
MyTest.class);
MyTest myTest = (MyTest) query.getResultList().get(0);
assertEquals("Bob", myTest.name);
The class needs to be declared an #Entity, which means you must ensure it has an unique #Id.
#Entity
class MyTest {
#Id String name;
int age;
}
The above query returns the list of Object[]. So if you want to get the u.name and s.something from the list then you need to iterate and cast that values for the corresponding classes.
I had the same problem and a simple solution that I found was:
List<Object[]> results = query.getResultList();
for (Object[] result: results) {
SomeClass something = (SomeClass)result[1];
something.doSomething;
}
I know this is defenitly not the most elegant solution nor is it best practice but it works, at least for me.
Here is the sample on what worked for me. I think that put method is needed in entity class to map sql columns to java class attributes.
//simpleExample
Query query = em.createNativeQuery(
"SELECT u.name,s.something FROM user u, someTable s WHERE s.user_id = u.id",
NameSomething.class);
List list = (List<NameSomething.class>) query.getResultList();
Entity class:
#Entity
public class NameSomething {
#Id
private String name;
private String something;
// getters/setters
/**
* Generic put method to map JPA native Query to this object.
*
* #param column
* #param value
*/
public void put(Object column, Object value) {
if (((String) column).equals("name")) {
setName(String) value);
} else if (((String) column).equals("something")) {
setSomething((String) value);
}
}
}
What if you create a bean with all required properties and cast the result using Java 8+ streams?
Like this:
public class Something {
private String name;
private String something;
// getters and setters
}
And then:
import javax.persistence.Query;
...
Query query = em.createNativeQuery("SELECT u.name,s.something FROM user u, someTable s WHERE s.user_id = u.id", Something.class);
List<?> list = query.getResultList();
return list
.stream()
.map(item -> item instanceof Something ? (Something) item : null)
.collect(Collectors.toList());
That way, you don't need to return List<Object[]> nor hide the warning with #SuppressWarnings("unchecked")
Ps.:
1 - I know that this post is very old. But... I'm here in 2021, so others will be coming here too =)
2 - This is wrong or bad practice? Let me know :D
You can also update your hibernate to a version greater than 5.4.30.final

Select single item from database with Spring Hibernate Sessionfactory

This is in my DAO:
public List<Weather> getCurrentWeather() {
return sessionFactory.getCurrentSession().createQuery("from Weather").list();
}
This gets all of the elements from table Weather. But lets say I wanna do something like this(I want only one element from table Weather):
public Weather getCurrentWeather() {
return sessionFactory.getCurrentSession().createQuery("from Weather where id = 1").list(); // here should be something else than list()
}
I know there should not be list() in the end, but what must I write there, to get only one object?
If you have an id, you just use get:
public Weather getCurrentWeather() {
return sessionFactory.getCurrentSession().get(Weather.class, 1);
}
If you do need to do a query, yeah you'll have to grab the top of the result set, or you can use uniqueResult() on the query.
Is there something wrong with getting a list? :) Even if you know there is only 1 hibernate cannot assume that. Getting a list is safer anyway!
public Weather getCurrentWeather() {
List<Weather> list = sessionFactory.getCurrentSession().createQuery("from Weather where id = 1").list(); // here should be something else than list()
return (list.isEmpty() ? null : list.get(0));
}
You need to use the Criteria API as below:
List<LeaveManagement> leaveManagements = session.createCriteria(LeaveManagement.class)
.add( Restrictions.isNull("approvedTimeStamp") )
.uniqueResult();
If you want to write a hql query you can write as:
String hql = "from LeaveManagement where approvedTimeStamp is null";
Query query = session.createQuery(hql);
LeaveManagement results = (LeaveManagement) query.uniqueResult();

Categories