Using Hibernate's Criteria and Projections to Select Multiple Distinct Columns - sql

Using Hibernate's Criteria, I want to execute the equivalent of:
select distinct uspscity, state from citycomplete where USPSCITY = 'HOUSTON'
I thought doing the following would yield the results I wanted:
ProjectionList projList = new ProjectionList();
projList.add(Projections.distinct(Projections.property("id.state")));
projList.add(Projections.distinct(Projections.property("id.uspsCity")));
criteria.setProjection(projList);
But, what this actually does is execute something like:
select distinct uspscity, distinct state from citycomplete where USPSCITY = 'HOUSTON'
Which throws an error, obviously.
Other than not using Criteria, is there a solution for this?
Thanks,
Brandon

ProjectionList projList = Projections.projectionList();
projList.add(Projections.property("id.state"));
projList.add(Projections.property("id.uspsCity"));
criteria.setProjection(Projections.distinct(projList));

Related

How to use select query in hibernate Dao implementation

select rg.guardian_name,
rg.guardian_id,
rg.guardian_email
from rts_guardian rd, rts_ward rw, rts_wrdgrd_junction junct
where rg.guardian_id = junct.guardian_id and rw.ward_id=junct.ward_id and rw.garde='VIII';
You can begin by this simple example :
Query query = session.createQuery
("select stock_code, stock_name from backup_stock where id_stock= :id");
Here is how you can set parameters :
query.setParameter("id", "1");
And to get the result:
List list = query.list();
please search more before asking here. You can find many documentation on this subject so make an effort.

How to convert SQL Query to CodeIgniter?

How to convert this query :
SELECT pembelian_detail_tb.kode_beli, pembelian_detail_tb.kode_produk, produk_tb.nama, pembelian_detail_tb.jumlah
FROM pembelian_detail_tb
INNER JOIN produk_tb ON pembelian_detail_tb.kode_produk = produk_tb.kode_produk;
I have try many code, and i still got undefine.
Try this
$query = $this->db->select('pembelian_detail_tb.kode_beli, pembelian_detail_tb.kode_produk, produk_tb.nama, pembelian_detail_tb.jumlah')
->from('pembelian_detail_tb')
->join('produk_tb', 'pembelian_detail_tb.kode_produk = produk_tb.kode_produk', 'inner')
->get();
I like to use Query Bindings once a join is involved in a query:
https://www.codeigniter.com/userguide3/database/queries.html
search page for "query bindings"
this escapes values for you of course and if you need to debug dump $this->db->last_query();

Using LINQ to SQl to achieve column Query

Can the below query be achieved with LINQ to SQL?
select id,
(select StateName from b_mstates where id=StateIdFk) as [State Name],
CityName
from b_mcities
var result = b_mcities.where(x => b_mstates.where(y => y.id == StateIdFK)
.contains(x.statename)).ToList();
Your question is pretty tough, because your sql wont run(it is malformed and missing logic). this is the jist of what you will need to do.
Assuming you have your associations set up correctly, it should be as simple as
var result = from city in m_bcities
select new {city.id, city.State.StateName, city.CityName}

Converting SQL script to LINQ with IN clause

I am trying to work out how to covert the script below from SQL in to LINQ. Any help would be welcome.
SELECT *
FROM
[tableName]
WHERE
[MyDate] IN
(SELECT
MAX([MyDate])
FROM
[tableName]
GROUP BY
[MyID])
I can't find an equivalent for the "IN" clause section. There are existing questions on this forum but none that cover selecting a DateTime.
Thanks in advance.
You can use the ".Contains(..)" function:
e.g.
var itemQuery = from cartItems in db.SalesOrderDetails
where cartItems.SalesOrderID == 75144
select cartItems.ProductID;
var myProducts = from p in db.Products
where itemQuery.Contains(p.ProductID)
select p;
Although it looks like 2 round trips, as the LINQ only constructs the query when the IEnumerable is tripped, you should get reasonable performance.
I think Any() is what you are looking for:
var result = tableName.Where(x =>
(from t in tableName
group t by t.MyID into g
where g.Max(y => y.MyDate) == x.MyDate
select 1).Any())

HQL size using QueryOver without subquery

Hope that someone can answer this.
I know that I can do the following using hql (pseudo code below)
var hql = "from objectA l where size(l.ChildCollection) > 0";
var data = Session.CreateQuery(hql)
.List<objectA>();
Is there a wondering if you could do something similar using QueryOver. Without resorting to using a sub query.I have a session filter on ChildCollection.
Unfortunately,
var query = QueryOver.Of<ObjectA>()
.WhereRestrictionOn(x => x.ChildCollection).IsNotEmpty();
Produces,
WHERE
exists(
select
1
from
[ChildCollection]
where
this_.Id=ObjectA_Id
);
Where as the Hql produces,
where
(
select
count(childcollection1_.ObjectA_Id)
from
[ChildCollection] childcollection1_
where
objectA0_.Id=childcollection1_.ObjectA_Id
and childcollection1_.DTCreated between #p0 and #p1
)>0
Cheers
Tanzy
var query = QueryOver.Of<ObjectA>().WhereRestrictionOn(x => x.ChildCollection).IsNotEmpty();
Both queries will generate similar sql:
SELECT this_.Id
FROM [ObjectA] this_
WHERE exists(select 1 from [ChildObjectB] where this_.Id = [key])
Is this what you were trying to achieve?