I have a stored Procedure like the following
CREATE PROCEDURE [dbo].[spConfiguration_test]
#ID int
AS
select empid,name from employee;
select * from address;
I wanted to call this stored procedure from jpa.So I did like this
DAOcode
public List test()
{
String execProce="exec spConfiguration_test 1";
System.out.println(execProce);
Query query = entityManagerUtil.entityManager.createNativeQuery(execProce);
return query.getResultList();
}
service class Code
List test=serviceDaoImpl.test();
when I debug this then List(test) size is showing 1 and when I run it then it gives me only the records of 1st table(select empid,name from employee;)
But I want the details of 2nd table when the stored procedure is executed.
Can any one guide me please?
If you merge the two queries into one. Then everything must work.
Example:
SELECT e.empid
,e.name
,a.*
FROM employee e
,address a
WHERE e.empid = a.empid;
Related
I created a table in a Database, so I got a "customerId" in that table and a "cardId", so a Customer can has multiples cardId's . What I tried right now is that:
public CustomerId getCustomerId(String cardId) {
this.getEntityManager();
return em.find(CustomerMappingHelper.class, customerId);
}
even though this won't work (I guess since my Ecplise shows me some Errors) ......furthermore I have in an another class a lookup methode, right now I stuck because I'm not sure how I can look up after a customer Id through an another card Id, just the logic behind that to look in that row ?
Used JPQL and solved my Problem, here I search the customerId through the cardId
public String getCustomerId(String cardId) {
Query q = em.createQuery("SELECT c.customerId FROM CustomerMapping c WHERE c.cardId = :cardId");
q.setParameter("cardId", cardId);
String customerId = (String) q.getSingleResult();
return customerId;
I have the following situation:
Android sqlite database with two tables:
table1 ——1:N —— table2
table1
- int id (PK)
- text field2
- text field3
- text field N…
table2
int id (PK)
int t1_id (FK)
real field3
real field N…
Next I have the following Android java structures:
Class Table1 {
int id;
String field2;
String field3;
String fieldN…;
ArrayList<Table2> atable2;
}
Class Table2 {
int id;
int t1_id;
double field3;
double fieldN…;
}
public ArrayList<Table1> atable1 = new ArrayList<Table1>();
So, I need to select data from database to the atable1 arraylist based on a condition like the following:
select * from table1 where table1.field2 = ‘Italy’;
The question is that the arraylist atable2 inside arraylist atable1 must be selected based on the field t1_id from table2 (table2.t1_id = table1.id).
So anyone can help me to build an efficient form of doing a query (for Android sqlite) to get these data, including the data from table2?
Thanks in advance!
Best regards,
PS
There is no efficient form of loading 350000 records into memory on a mobile device.
But you don't need to, because you will not be able to show all of them on the screen at the same time.
Change your app to dynamically load only the data for the objects currently visible on the screen.
Using JPA, I want to select all Log objects for a specific actiontype. From the log object I want to get the user (log.getUser()), but the users appear several times in the result list. I tried it with distinct, but it did not work, I guess because I was not able to define, what exactly has to be distinct. Here is my JPA query:
SELECT DISTINCT log
FROM Log AS log JOIN log.action AS action
JOIN log.user AS user
WHERE action.actionType = :actionType
If I say SELECT DISTINCT user, then I don't have the whole log object in the end.
Any help or hint would be appreciated.
Edit:
Part of my Log Class:
public class Log {
private int logId;
private Calendar logDate;
private User user;
private Action action;
private String description;
....
}
Two Queries Solution
Since you want Log objects and distinct User objects you could do two queries, first one to retrieve the Log objects and second one to retrieve distict User objects.
// first one to select Log objects
String logQuery = "SELECT l FROM Log l WHERE l.actionType = :actionType";
...
List<Log> logs = logJpaQuery.getResultList();
// second one to select distinct users from this objects
String usersQuery = "SELECT distinct l.user FROM Log l where l.logId in (:logIds)";
...
userJpaQuery.setParameter("logIds", logs);
List<User> users = userJpaQuery.getResultList();
With this approach you have the distinct users for the select Logs objects.
The following is my select query in BrandMapper.xml.
<select id="getBrand" parameterType="String" resultMap="brandResult">
SELECT
B.bid as bid,
B.bname as bname,
B.avg_price as avg_price,
B.total_number as total_number,
P.pid as pid,
P.pname as pname,
P.bid as pbid,
P.bname as pbname,
P.specs as pspecs,
P.price as price
from Brands B left outer join Products P on P.bid = B.bid
where B.bname = #{bname, jdbcType=VARCHAR}
</select>
This is the interface
public interface BrandMapper {
BrandDAO getBrand(String bname);
}
This is the service class
#Service
public class BrandService {
#Autowired
private BrandMapper brandMapper;
public BrandDAO getBrand(String bname) {
System.out.println("Inside DBService getBrand");
return brandMapper.getBrand(bname);
}
}
My problem is that the getBrand function in BrandService returns a null value. If I replace the parameter #{bname} inside the BrandMapper.xml by a hardcoded string it works and returns the correct class. What am I doing wrong here? Is there any logs or anything available where I can see the actual query which is being constructed? Any help is appreciated.
I managed to enable loggin using log4j and this is the query which is getting executed
SELECT B.bid as bid, B.bname as bname, B.avg_price as avg_price, B.total_number as total_number, P.pid as pid, P.pname as pname, P.bid as pbid, P.bname as pbname, P.specs as pspecs, P.price as price from Brands B left outer join Products P on P.bid = B.bid where B.bname = ?
The parameter is not getting replaced. I cant figure out what I am doing wrong here.
The question mark is a placeholder in Prepared Statements, the logged query is perfectly fine and looks as expected. The real value should be passed along the query to your database as a separate parameter.
I wrote a hsql:
String queryString = "select t1.a, t1.b, t2.c from table1 t1, table2 t2 where t1.id = t2.id";
and then I have a class:
class test{
String a;
String b;
String c
....//other getter and setter
}
I tried:
List = getHibernateTemplate().find(queryString);
this doesn't work, when I use test object in jsp page, it will throw out exception.
I have to manually create a test object:
List<Object[]> list = getHibernateTemplate().find(queryString);
test.seta(list.get(0)[0]);
is it possible for hibernate to automatically map the class for me in hsql ?
If you have a mapping for both table1 and table2 (see Prashant question above) you can do something like:
String queryString = "select t1 from table1 t1
inner join t1.table2 t2";
After you run the query you should have a list of t1 objects.
for(Table1 t1:listOfTable1Objects) {
t1.getA(); //for example or whatever you want to do with your object.
}
The Problem is that you do not write a HQL query. You just write a normal SQL query. In HQL, because the hibernate make the mapping from table to class, you cannot make a projection. So, if you write something like
String query = "FROM Class1 WHERE ome_condition;
without the SELECT clause, the Hibernate will be able to convert the result in the proper object.
You can see more about this here: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html
If you dont have a mapping, you may create a auxiliary class for this. Say ResultClass. Then you add #NamedNativeQuery and #SqlResultSetMapping annotations to the class:
#NamedNativeQuery(name="queryHehehe", query="select t1.field1 f1, t2.field2 f2 from table1 t1, table2 t2", resultSetMapping="mappingHehehe")
#SqlResultSetMapping(name="mappingHehehe", entities={
#EntityResult(entityClass=my.clazz.AuxiliaryClass.class, fields = {
#FieldResult(name="id", column="f1"),
#FieldResult(name="other_property", column="f2")
}),
})
public class AuxiliaryClass {
public Long id;
public String other_property;
}
I have never used this, but can work. Good luck.
If you need a query to return values from multiple tables and create an object of an unmapped class, then you need to either do what you're doing here, or use a ResultTransformer.
In order to do this with HibernateTemplate, you'll need to change the way you use the template, possibly using execute(HibernateCallback action), as you'll need to convert the sql query to a Criteria as described in Hibernate Reference Native SQL Chapter.
If you do want to try this, you'll probably want to use an AliasToBeanResultTransformer or AliasToBeanConstructorResultTransformer rather than writing your own transformer.