in my java web application i need to inquire a list of deposits from a view named VwDepositsInfo by customerNumber.
when i execute my query:
select * from VW_DEPOSIT_INFO v where v.CUSTOMER_NUMBER=:customerNo
in database console my resultList size is 2 and have something like this:
1-{depositTypeDesc="shortTerm"} {depositTypeCode="850"}
2-{depositTypeDesc="longTerm"} {depositTypeCode="2"}
but when i test my code that includes a namedQuery:
#NamedQuery(
name = "inquireAccountByCustomerNumber",
query = "select c from VWDepositInfo c where c.customerNumber=:customerNo"
)
i get a resultList with size 2 but both the same, sth like this:
1-{depositTypeDesc="shortTerm"} {depositTypeCode="850"}
2-{depositTypeDesc="shortTerm"} {depositTypeCode="850"}
when i make it nativeQuery with defining the result class:
Query query = entityManager.createNativeQuery("select * from VW_DEPOSIT_INFO v where v.CUSTOMER_NUMBER=:customerNo", VWDepositInfo.class);
again i get the wrong results.
finally i tried nativeQuery without defining the result class:
Query query = entityManager.createNativeQuery("select * from VW_DEPOSIT_INFO v where v.CUSTOMER_NUMBER=:customerNo");
and result was as i expected to be.
and this is my VwDepositsInfo.class:
#Entity
#Table(name = "VW_DEPOSIT_INFO")
#Audited(withModifiedFlag = false)
#NamedQueries(
{#NamedQuery(
name = "inquireAccountByCustomerNumber",
query = "select c from VWDepositInfo c where c.customerNumber=:customerNo"
)
}
)
public class VWDepositInfo implements Serializable {
#Id
#Column(name = "CUSTOMER_NUMBER")
private Long customerNumber;
#Column(name = "BRANCH_CODE")
private Long branchCode;
#Column(name = "DEPOSIT_TYPE_CODE")
private Long depositTypeCode;
#Column(name = "DEPOSIT_SERIAL")
private Long depositSerial;
#Column(name = "DEPOSIT_TYPE_DESC")
private String depositTypeDesc;
#Column(name = "CURRENCY_TYPE_DESC")
private String currencyTypeDesc;
#Column(name = "DEPOSIT_OPEN_DATE")
private Date depositOpenDate;
Does anyone know why this is happening???
VW = view?
You probably need to specify the master key
use #id for unique field :)
you probably need more than one field with #id for a unique row.
for example both of DEPOSIT_TYPE_CODE and customerNumber
Related
I am trying to fetch data from parent to child both based on filter criteria using JPA Criteria query so that can avoid multiple queries to DB, but not able to achieve desired result. Following are my sample entities( without getters/setters)
#Entity
public class ParentTable implements Serializable{
private static final long serialVersionUID = 1L;
#Id
#Column(name = "ID")
private String id;
#Column(name = "KEY_COLUMN",length = 30)
private String keyColumn;
#Column(name = "CODE",length = 30)
private String code;
#Column(name = "KEY_DESC",length = 240)
private String desc;
#OneToMany(mappedBy = "parentTable",cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<ChildTable> childTableList;
}
#Entity
public class ChildTable implements Serializable{
private static final long serialVersionUID = 1L;
public ChildTable() {
super();
}
#Id
#Column(name = "ID",length = 80)
private String id;
#Column(name = "PARENT_KEY_COLUMN",length = 30,insertable = false,updatable = false)
private String parentKeyColumn;
#Column(name = "CHILD_CODE",length = 30)
private String childCode;
#Column(name = "CHILD_DESC",length = 240)
private String chldDesc;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "PARENT_KEY_COLUMN", referencedColumnName = "KEY_COLUMN")
private ParentTable parentTable;
}
Criteria builder snippet -
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<ParentTable> query = cb.createQuery(ParentTable.class);
Root<ParentTable> fromParent = query.from(ParentTable.class);
Join<ParentTable, ChildTable> details = fromParent.join("childTableList");
List<Predicate> conditions = new ArrayList();
conditions.add(cb.equal(details.get("childCode"), childCode));
conditions.add(cb.equal(details.get("chldDesc"),chldDesc));
TypedQuery<ParentTable> typedQuery = em.createQuery(query.select(fromParent).where(conditions.toArray(new Predicate[] {})));
List<ParentTable> parentTableList = typedQuery.getResultList();
This executes and gives result of parent table only, if i fetch childtable data I can see JPA query getting exceuted again, can this be avoided and fetch list of child entities which matches 3 dynamic params? 1. ParentTable.code, 2. ChildTable.childCode , 3. ChildTable.chldDesc .
Can anyone help me to construct JPA query like below which executes in one DB hit instead of multiple round trip, which is happening in above snippet of code?
select * from ParentTable p,ChildTable c where p.KEY_COLUMN=c.PARENT_KEY_COLUMN and p.CODE=? and c.CHILD_CODE=? and c.CHILD_DESC=?
Update :
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<ParentTable> cq = builder.createQuery(ParentTable.class);
Root<ParentTable> root = cq.from(ParentTable.class);
Join<ParentTable, ChildTable> join = root.join("childTableList");
Predicate p1=builder.equal(root.get("code"), "code");
Predicate p2=builder.like(join.get("chldDesc"), "%chldDesc%");
Predicate p3=builder.equal(join.get("childCode"), "childCode");
Predicate andPredicate = builder.and(p1,p2, p3);
cq.select(root).where(andPredicate);
EntityGraph<ParentTable> fetchGraph = entityManager.createEntityGraph(ParentTable.class);
fetchGraph.addSubgraph("childTableList");
List<ParentTable> parentTableList=entityManager.createQuery(cq).setHint("javax.persistence.loadgraph", fetchGraph).getResultList();
parentTableList.forEach(System.out::println);
This approach forms the expected query like above mentioned but again one more query is formed like below , why second query is triggered even-though its not required?
select * from ParentTable parentTable0_ where parentTable0_.code=?
JPA is required to give you managed entity results that reflect the data in the database. The filter you put on the query does not filter the internal relationships.
Your 'join' clause only affects the filter applied to returning ParentTable entities. Every entity returned will have a complete 'childTableList' collection, so even though you are returning ParentTable instances that have specific codes and descriptions, the childTableList shows all its children. That is JPA for you, and specific providers do have a way to filter these mapped collections (AdditionalCriteria ) IMO they are a bad route with many problems.
If you want to have childTableEntries that match the specific codes and descriptions, your query should be more of the form (using JPQL)
"Select c, p from ChildTable c join c.parentTable p where c.childCode = :code and c.chldDesc = :desc"
This will return you a List result, where the Object array has the child and parent entries for each row that matches. So duplicate parents if one has more than one child that matches.
Otherwise, the extra query is being caused by accessing the childTableList on the parentTable entries, because they are marked lazy. Your criteria query is specifying 'join', as you want to use the childTableList entries in filtering parentTable entities. If you want the childTableList fetched with the parentTables, you need to use fetchJoins. Root implements FetchParent, which would allow you to specify a 'fetch' on the childTableList in addition to the join you've defined. In JPQL, something like:
"select p from ParentTable p fetch join p.childTableList, join p.childTableList c where c.childCode = :code and c.chldDesc = :desc"
I am trying to fetch specific fields from my entities. I need the result in my entity structure.
Following are my entities:
Country
public class CountryModel {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "CmtID")
private int id;
#Column(name = "CmtName")
private String name;
#JoinColumn(name="CmtStateID")
#OneToMany(targetEntity=StateModel.class,fetch=FetchType.EAGER)
private List<StateModel> state;
}
State
public class StateModel {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "SmtID")
private int id;
#Column(name = "SmtName")
private String name;
}
Following is the HQL query am executing:
Query query = session.createQuery("select c.name, s.name from CountryModel c join c.state s where c.id=2");
CountryModel stateModel = (CountryModel) query.uniqueResult();
But am getting the following error:
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.muziris.entity.CountryModel
Thanks for helping.
Expected result:
Country :
name : india
state :
name : kerala
name : goa
name : Pak
state :
name : karachi
Since your classes are mapped you can try:
Query query = session.createQuery("from CountryModel c where c.id=2");
CountryModel countryModel = (CountryModel) query.uniqueResult();
Let's make use of the mapping and HQL.
From there you can use a DTO to have only the data that you need
public CountryDTO transform(CountryModel cm){
String countryName = cm.getName();
List<String> stateNames = cm.getState().stream.map(StateModel::getName)
.collect(Collectors.toList());
return new CountryDTO(countryName, stateNames);
}
CountryDTO is the result that you need.
Hibernate returns List<Object[]> when you use the projections.
List<Object[]> is a list of specified projection columns.
Some links
https://stackoverflow.com/a/36883968/3405171
How to transform a flat result set using Hibernate
I have a entity as below
public class Employee implements Serializable {
#Id
#Column(name = "EMPSEQ")
#GeneratedValue(strategy = GenerationType.AUTO)
private Long empSeq;
#Column(name = "EMPID")
private String empId;
#Column(name = "WINDOWSLOGINID")
private String logInId;
// assume respective getter and setter methods
}
I want to query all row where logInId does not start with "5"
I tried below code:
query = session.createQuery("select * from Employee e where e.logInId not like 5%");
the above code didn't work. what is the right way to use NOT LIKE in HQL
In your query there's an error:
query = session.createQuery("select * from Employee e where e.logInId not like 5%");
become:
query = session.createQuery("select * from Employee e where e.logInId not like '5%'");
e.logInId is string, so you must quote your condition 5%.
You can also use Hibernate Criteria for the same.
JPA EntityManger has unwrap() method which will return session.
Session session = getEntityManager().unwrap(Session.class);
Criteria criteria = session.createCriteria(Pojo.class);
criteria.add(Restrictions.not(Restrictions.like("loginId","5%")));
List<Pojo> list=criteria.list();
if(null!=list && list.size()> 0){
return list.get(0);
}
return null;
I'm using Hibernate and JPA. I have entity class CompanyRate.
CompanyRate.java
#Entity
public class CompanyRate {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int idRate;
#Column(nullable = false)
private int atmosphere;
#Column(nullable = true)
private int salary;
#Column(nullable = false)
private int opportunityToDevelop;
#Column(nullable = false)
private int socialPackage;
#ManyToOne(targetEntity = User.class)
private User user;
#ManyToOne(targetEntity = Company.class)
Company company;
}
I want to display AVG values of atmosphere, salary, opportunityToDevelop, socialPackage where company.idComapny = ? on JSP.
Ofc I can use getResultList() on executed query and diplay it on JSP using . But I wanna assign each result for single variable. What is the best way to do it?
What you're asking is equivalent to the following SQL:
SELECT c.id,
AVG(t.atmosphere),
AVG(t.salary),
AVG(t.opportunityToDevelop),
AVG(t.socialPackage)
FROM table t, company c
WHERE t.companyid = c.id
AND c.id IN (:companyIds)
GROUP BY c.id;
Translating the above into HQL/JPQL or leveraging Hibernate or JPA criteria APIs should be a trivial exercise.
Just be sure to call setParameter("companyIds", listOfCompanyIds) on the constructed Query and then call getResultList() or list() depending on the API you use.
Learning how to write JPA query. Please advise me whether it possible to write the below queries more efficiently, may be in a single select statement. May be a join, but not sure how to do it.
class Relationship {
#ManyToOne
public String relationshipType; //can be MANAGER, CUSTOMER etc
#ManyToOne
public Party partyFrom; // a person who has a relation
#ManyToOne
public Party partyTo; // a group a person relate to
}
Queries:
String sql = "";
sql = "select rel.partyTo";
sql += " from Relationship rel";
sql += " where rel.partyFrom = :partyFrom";
sql += " and rel.relationshipType= :typeName";
Query query = Organization.em().createQuery(sql);
query.setParameter("partyFrom", mgr1);
query.setParameter("typeName", "MANAGER");
List<Party> orgList = query.getResultList();
String sql2 = "";
sql2 = "select rel.partyFrom";
sql2 += " from Relationship rel";
sql2 += " where rel.partyTo = :partyToList";
sql2 += " and rel.relationshipType = :typeName2";
Query query2 = Organization.em().createQuery(sql2);
query2.setParameter("partyToList", orgList);
query2.setParameter("typeName2", "CUSTOMER");
List<Party> personList2 = query2.getResultList();
Both the queries work. Query 1 returns a list of groups, where the person (mgr1) has a relation MANAGER with. Query 2 returns all the Persons they are CUSTOMER to the groups returned by query 1. In effect, I get a list of Person they are belong to (customer) the same group where the Person (mgr1) has a relation MANAGER with.
Is it possible to combine them into single sql statement so possibly only one db access?
You literally nest one query inside the other, and use a "where in" clause to specify that the outer query should fetch customers from the inner query.
select rel2.partyFrom
from Relationship rel2
where rel2.relationshipType = :typeName2 /* customer */
and rel2.partyTo.id in
(select rel.partyTo.id
from Relationship rel
where rel.partyFrom = :partyFrom
and rel.relationshipType = :typeName)
Your calling code passes typeName, typeName2, and partyFrom parameters as before. PartyTo parameter is not needed, since the data comes from the subselect (inner query.)
You can achieve the same thing using a self join, with a where clause that filters managers on the left side, and customers on the right side, but using an 'in' clause is semantically clearer.
EDIT: I addded .id to the subselect, which I think is needed.
This is not answer to question but helping other folks in case if someone looking into #OneToMany relation in Spring Data JPA using JPQL, because the question is related to JPA so thought to share my 2-cents, apologize in advance
#Entity
#Table(name = "MY_CAR")
public class MyCar {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#Column(name = "DESCRIPTION")
private String description;
#Column(name = "MY_CAR_NUMBER")
private String myCarNumber;
#Column(name = "RELEASE_DATE")
private Date releaseDate;
#OneToMany(cascade = { CascadeType.ALL })
#JoinTable(name = "MY_CAR_VEHICLE_SERIES", joinColumns = #JoinColumn(name = "MY_CAR_ID "), inverseJoinColumns = #JoinColumn(name = "VEHICLE_SERIES_ID"))
private Set<VehicleSeries> vehicleSeries;
public MyCar() {
super();
vehicleSeries = new HashSet<VehicleSeries>();
}
// set and get method goes here
#Entity
#Table(name = "VEHICLE_SERIES ")
public class VehicleSeries {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#Column(name = "SERIES_NUMBER")
private String seriesNumber;
#OneToMany(cascade = { CascadeType.ALL })
#JoinTable(name = "VEHICLE_SERIES_BODY_TYPE", joinColumns = #JoinColumn(name = "VEHICLE_SERIES_ID"), inverseJoinColumns = #JoinColumn(name = "BODY_TYPE_ID"))
private Set<BodyType> bodyTypes;
public VehicleSeries() {
super();
bodyTypes = new HashSet<BodyType>();
}
// set and get method goes here
#Entity
#Table(name = "BODY_TYPE ")
public class BodyType implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#Column(name = "NAME")
private String name;
// set and get method goes here
public interface MyCarRepository extends JpaRepository<MyCar, Long> {
public Set<MyCar> findAllByOrderByIdAsc();
#Query(value = "select distinct myCar from MyCar myCar "
+ "join myCar.vehicleSeries as vs join vs.bodyTypes as bt where vs.seriesNumber like %:searchMyCar% "
+ "or lower(bt.name) like lower(:searchMyCar) or myCar.bulletinId like %:searchMyCar% "
+ "or lower(myCar.description) like lower(:searchMyCar) "
+ "or myCar.bulletinNumber like %:searchMyCar% order by myCar.id asc")
public Set<MyCar> searchByMyCar(#Param("searchMyCar") String searchMyCar);
}
Some data in tables like
Select * from Vehicle_Series
ID SERIES_NUMBER
1 Yaris
2 Corolla
Select * from Body_Type
ID NAME
1 Compact
2 Convertible
3 Sedan