Below is the HQL query I'm using :
Session ssn=HibernateSessionFactory.openSession();
Query query = ssn.createQuery("from TUIDInfo e where e.id = :cred_id ");
query.setParameter("cred_id",cred_id);
List list = query.list();
My table contains around 20 entries matching the cred_id but this query only returns the last row in the table. Why is this so ? The query.list().size() attribute is always 1 even though there are lot more entries.
Related
What is correct way to perform hibernate replacement when we have a certain values (a list), we want the result to be limited based on that list. If there is no value then we want to show all, not restricted.
For example the productList below could be 1,2,3, then we want to show only COLUMN1 that has value 1,2, or 3.
If the productList is an empty list. We want to display all result.
Here is an illustration what I want to do:
List<Integer> productList = new ArrayList()<>;
org.hibernate.Session session = new Session();
org.hibernate.Query query = session.createQuery("SELECT * FROM TABLE_A WHERE COLUMN1 IN (:products)");
query.setParameterList('products', (Collection) productList);
I have a List composed by Integer elements. I have to make a single query like this:
From Table as t where t.id <> element1 AND t.id <> element2 AND ......
Someone can give me a tip how to set the input list? I have to set the single element or the entire list?
Create Collection of Integers:
Collection<Integer> ints = new ArrayList<Integer>();
ints.add(1);
ints.add(2);
ints.add(3);
Set it as parameter:
Query q = entityManager.createQuery("FROM Table as t WHERE t.id NOT IN (:ints)");
q.setParameterList("ints", ints);
Relevant question: Hibernate HQL Query : How to set a Collection as a named parameter of a Query?
What I want to achieve:
Select a susbset of entities that have a property value that exists in a List of values. This list is returned by another Query.
In plain SQL, this can be easily achieved with subqueries. This is the Criteria query that returns the relevant values:
DetachedCriteria subq = DetachedCriteria.forClass(MyClass.class)
.setProjection(
Projections.projectionList()
.add(Projections.alias(Projections.max("interestingVal"), "interestingVal"))
.add(Projections.groupProperty("someval1"))
.add(Projections.groupProperty("someval2"))
.add(Projections.groupProperty("someval3"))
);
I would then like to select the entities that have a value of interesting_val that was returned by the above query. Like so:
List<MyClass> resultList = sessionFactory.getCurrentSession().createCriteria(MyClass.class)
.add(Subqueries.propertyIn("interestingVal", subq))
.list();
Unfortunately, the subq subquery returns a List of Object-arrays with length 4, since I have 4 Projection values. All projection values seem to be automatically added to the SELECT clause. This results in an
SQLSyntaxErrorException: ORA-00913: too many values.
How can I either tell my second Criteria query to only use the first element in the Object array or only retrieve the first column in my subquery?
Instead of propertyIn try propertiesIn.
String[] vals = new String[]{"interestingVal", "someval1", "someval2", "someval3"};
List<MyClass> resultList = sessionFactory.getCurrentSession().createCriteria(MyClass.class)
.add(Subqueries.propertiesIn(vals, subq))
.list();
I am using hibernate 4 and Spring 3.
I have 5 tables and each table is mapped with 1 entity class.Now if I have to select columns from 1 table than i will do below :
String hql = "from Employee E";
Query query = session.createQuery(hql);
List results = query.list();
This value in this result will be of type EmployeeEntity.
Or I can use Criteria as well.
Now my requirnment is that I have to get the result from all 5 tables.1-2 columns from each table.
Earlier it was one 1 table so i was getting one entity , now I am getting results from 5 tables so how to map it in entity.
List results1 = query.list(); // considering select and getting 6 columns in results with diffenent tables.
Now how to iterate this result1.
I hope you got my question.
You can use Result Set Transformer of Query:
Say you have 4 columns from different tables like tab1col,tab2col,tab3col,tab4col.
Create a 'POJO' as follows
class MyClass
{
private Integer tablcol;
private Integer tab2col;
private Integer tab3col;
private Integer tab4col;
// getter and setters
}
Following way you can transform you result set:
List<MyClass> myClassList=query.setResultTransformer(Transformers.aliasToBean(MyClass.class)).list();
Note: query should contain a result set(something like cursor in oracle).
I'm using Java EE 6 and query a database using JPA's javax.persistence.Entitymanager. I have a JPQL query code snippet that looks like something like this:
Query query = entityManager.createQuery("
select A.propertyX, B.propertyY, C.propertyZ
from TableA A, TableB B, TableC C
where A.id = :id and B.id = A.id and C.type = B.type
");
query.setParameter("id", id);
Object[] result = (Object[]) query.getSingleResult();
Where propertyX/Y/X all are references to other entities. In my case, a matching row from TableA, TableB, and TableC all exist. For the matching rows, TableA.propertyX and TableB.propertyY hold values whereas TableC.propertyZ is null (and non-required).
I expect this to execute and return an Object[] array with values for the first two elements (propertyX and propertyY) and null for the third element (propertyZ).
However, when propertyZ is null, a NoResultException is thrown. If I change the data so that propertyZ is not null, the query executes and returns a value.
Is this expected JPQL behavior?
How can I ensure that my query will behave as I originally expected?
The obvious work-around is to select the entire root entity than any sub-property, e.g. 'C' rather than 'C.propertyZ', and then get the property out of the entity object. However, I'd like for this to work as I expect it to without doing so.
If, for a given row in A and B, there is a row in C where C.type = B.type, but the propertyZ column for that row is null, then you are right that your query should return a record.
However if for that given row in A and B, there is no matching row in C where C.type = B.type, then your query will return no result. This has nothing to do with JPQL, but with SQL
If you want the latter case to still return a record with null in the propertyZ field, you need to use OUTER JOINs
HTH