Retrieving selected fields from database using Hibernate - spring

I'm retrieving a list of rows with selected fields from Oracle database using Hibernate. The retrieval is made by the following method in one of the DAOs in my application.
#SuppressWarnings("unchecked")
public List<Object[]> getOldFileName(String []ids)
{
int len=ids.length;
Long longType[]=new Long[len];
for(int i=0;i<len;i++)
{
longType[i]=Long.valueOf(ids[i]);
}
return sessionFactory.getCurrentSession().createQuery("select catId, catImage from Category where catId in(:id)").setParameterList("id", longType).list();
}
Here I'm fetching two fields categoryId and categoryImage as listed in the given HQL query based on the catId supplied as an array of String[] as a method parameter via Spring. It works fine and there is no question about it.
But regarding my requirements, retrieving catId again is completely unnecessary and I would like to remove catId from the list of fields in the query something simply like the following.
select catImage from Category where catId in(:id)
If I try to execute this query then the following call to the preceding method inside the Spring controller class,
String temp[]=request.getParameter("setDel").split(",");
List<Object[]> oldFileNames = categoryService.getOldFileName(temp);
//Invokes the preceding method in DAO.
causes the following exception to be thrown,
java.lang.ClassCastException: java.lang.String cannot be cast to
[Ljava.lang.Object;
The exception message indicates that java.lang.String cannot be cast to an array of Objects - Object[]. It appears that the HQL statement attempts to get only a single value of type String instead of retrieving List<Object[]>.
I just want to delete files which are stored in a directory after retrieving their names from the database and mentioning of catId in the list of fields in HQL is completely unnecessary.
Why do I need to add catId in the list of fields of the HQL statement in this scenario?

The return type for the list should be List. When only one column comes back hibernate does not put the result into and Object[]

Related

SpringData JPA: Query with collection of entity as parameter

I have a list of entities on which I want to perform an update, I know I could update the table with list of String/Integer.. etc as the parameter with something like
#Query("update tableName i set i.isUpdated = true where i.id in :ids")
void markAsUpdated(#Param("ids") List<Integer> itemIds);
I'm trying to avoid repeated conversion of list of entities to list of Ids for making the query in DB. I know there are deleteAll and deleteInBatch commands which accept parameter as list of entities.
How do I do this in JPA Query, I tried the following but it didn't work yet.
#Modifying(flushAutomatically = true, clearAutomatically = true)
#Query("update tableName i set i.updated = true where i in :items")
void markAsUpdated(#Param("items") List<Item> items)
The query needs ids, it doesn't know how to deal with entities.
You have multiple options:
Just pass ids to the method, the client is responsible for extracting ids.
Pass entities and use SpEL for extracting ids
As suggested in the comments use a default method to offer both APIs and to delegate from one to the other.
As for the question that came up in the comments: You can move the method for extracting the id into a single method by either have relevant entities implement an interface similar to this one:
interface WithId {
Long getId();
}
Or by passing a lambda to the method, doing the conversion for a single entity:
List<ID> extractIds(List<E> entities, Function<E, ID> extractor) {
// ...
}

hotchocolate throws error when using UseFiltering() on a field

I have a pretty simple setyp where I'm putting graphql over an entityframework datacontext (sql server).
I'm trying to get filtering to work. I've tried adding .UseFiltering() to a field descriptor like so...
descriptor.Field(t => t.AccountName).Type<NonNullType<StringType>>().UseFiltering();
But it causes this error on startup...
HotChocolate.SchemaException: 'Unable to infer or resolve a schema
type from the type reference Input: System.Char.'
I assume I'm doing something wrong somewhere...
"UseFiltering" is supposed to be used to filter data which represents a collection of items in some way (IQueryable, IEnumerable, etc).
For instance, if you have users collection and each user has AccountName property you could filter that collection by AccountName:
[ExtendObjectType(Name = "Query")]
public class UserQuery
{
[UseFiltering]
public async Task<IEnumerable<User>> GetUsers([Service]usersRepo)
{
IQueryable<User> users = usersRepo.GetUsersQueryable();
}
}
In that example the HotChocolate implementation of filtering will generate a number of filters by user fields which you can use in the following way:
users(where: {AND: [{accountName_starts_with: "Tech"}, {accountName_not_ends_with: "Test"}]})
According to your example: the system thinks that AccountName is a collection, so tries to build filtering across the chars the AccountName consists of.

How can I delete all records from a table?

I've been searching for an answer on how to delete ALL records from a table using LINQ method syntax but all answers do it based on an attribute.
I want to delete every single record from the databse.
The table looks like so:
public class Inventory
{
public int InventoryId { get; set; }
public string InventoryName { get; set; }
}
I'm not looking to delete records based on a specific name or id.
I want to delete ALL recods.
LINQ method syntax isn't a must, bt I do prefer it since it's easier to read.
To delete all data from DB table I recommend to use SQL:
Trancate Table <tableName>
Linq is not meant to change the source. There are no LINQ methods to delete or update any element from your input.
The only method to change you input, is to select the (identifiers of the )data that you want to delete in some collection, and then delete the items one by one in a foreach. It might be that your interface with the source collection already has a DeleteRange, in that case you don't have to do the foreach.
Alas you didn't mention what your table was: Is it a System.Data.DataTable? Or maybe an Entity Framework DbSet<...>? Any other commonly used class that represents a Table?
If you table class is a System.Data.DataTable, or implements ICollection, it should have a method Clear.
If your tabls is an entity framework DbSet<...>, then it depends on your Provider (the database management system that you use) whether you can use `Clear'. Usually you need to do the following:
using (var dbContext = new MyDbContext(...))
{
List<...> itemsToDelete = dbContext.MyTable.Where(...).ToList();
dbContext.MyTable.RemoveRange(itemsToDelete);
dbContext.SaveChanges();
}

How to Return all instances of the type with the given ID in JPA SpringBoot?

I'm trying to return (or fetch) all the records from the database based on an ID provided by me. I'm using JPA and i'm aware of findAll() method but it returns all the records without any criteria, I created a custom query and it is only returning a unique value from the table but i want to return all records based on a criteria.
For example, findAllByUserID(String UserID) method should return all the records based on that UserID not just one.
I'd appreciate any help or suggestion.
Thanks
Have a look at the doc. There you will find the keywords you can use to declare methods in repository interfaces that will generate the according queries:
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods
In your case: If userID is an attribute of your entity you can add a method
List<YourEntity> findByfindAllByUserID(String userId)
to your repository interface.
First, make sure that you're not using any aggregate function in your select query such as DISTINCT()
Then make sure that the the method which is implementing that query is returning a List of you're desired result.
here's how it should look :
#Query("select t from table t where t.code = ?1")
List<Result> findAllByUserID(String UserID);

Hibernate find object from database by few params

Can't find right query with few params.
Here is my query from DAO class:
public Notebook findByName(String name, Integer UserId) {
return (Notebook) sessionFactory.getCurrentSession().createQuery("from Notebook where ....");
}
I would like to get by this query object of Notebook by few params: name and user id(foreign key).
And i would like to get only one object, not list of objects even he has only 1 element.
The method createQuery(queryString) returns a Query on which you can set the parameters e.g.
Query query = createQuery("from Notebook where id=:id and title=:title");
query.setParameter("id", id);
query.setParameter("title", title);
query.setMaxResults(1);
query.uniqueResult(); // fetch the object.
If the query returns more then one results be sure to set setMaxResults() or an exception will be thrown.
First
Edit to
public Notebook findByName(String name, Integer UserId) {
return (Notebook) ....createQuery("from Notebook where ....")...uniqueResult;
}
BTW: I am not including the part where you are setting the parameters for your query
If there is no a unique result, uniqueResult throws an exception.
Be sure your query statement is very specific and you are including something like master.pk=child.fk (where pk should fk)
could you update and include the complete query statement?

Resources