Sorry for the cryptic title..
Could you help me out, on how do a select, based on the count of a property, using Criteria?
I have an object (Pool) with a property (PoolItems), and i want to select all the Pools w. more than 5 PoolItems.
Try this:
DetachedCriteria dCriteria = DetachedCriteria.For<PoolItem>("pItem")
.SetProjection(Projections.Count("Id"))
.Add(Restrictions.EqProperty("pItem.PoolID", "pool.Id"));
IList<Post> posts = Session.CreateCriteria<Pool>("pool")
.Add(Subqueries.Gt(5, dCriteria)).List<Pool>();
Assuming the PoolItem table has a PoolID column as a foreign key of the Pool table.
The relationship is a one-to-many. If you do not have the PoolID property mapped in the PoolItem class and you just have the many-to-one object mapping named "Pool" then replace in the detached criteria the "pItem.PoolID" with "pItem.Pool.Id".
Related
I have a table called Carts with a has_many relationship to another table called Subcarts. I want to create a scope that returns all instances of the Cart table where all the associated Subcarts fits a particular condition (ex: subcart.status == 1). If even one subcart that is associated to a Cart instance has a status of 1, the query should not return the cart instance.
scope :cart_bucket, -> {
includes(:subcarts).
where.not('subcarts.status = ?', 1).
references(:subcarts)
I've also tried to use select within the query but had an error returned because of the return array data type. I would like to keep this within a scope and not create a method if at all possible.
You can select all the carts ids from those subcarts where the status is not 1 and use that as the where clause to leave out all the carts matching those ids:
Cart.where('NOT EXISTS (SELECT s.cart_id FROM subcarts s WHERE s.cart_id = carts.id AND s.status = 1)')
The ActiveRecord version might change a bit:
Cart.where.not(id: Subcart.where(status: 1).select(:cart_id))
There the SQL generated uses a NOT IN clause instead of NOT EXISTS.
Basically, I have a table called Client Entity that contains an Entity Type column and I have an entity type called BEN. I want to show all records where this particular type does not exist. The issue is that the record itself may not exist if the entity type has not been appointed so I am basically asking the query to show me where no 'rows' exists because of this value...
Basically I'm looking for a code to highlight the absence of data, not NULL values.
Can anyone help ?
If you have a table of "records", then you would use not exists:
select r.*
from records r
where not exists (select 1
from cliententity ce
where ce.record_id = r.record_id and
ce.entity_type = 'BEN'
);
I don't know what your real table or column names are, but this gives the idea on how to approach the problem.
I have few classes mapped as subclasses to a table using a discriminator column. But the discriminator column is not mapped to any field (even in the base class). I want to create Query (in QueryOver syntax) which filters by the type of the subclass
E.g. Table:
Orders (Id,OrderType,Amount,Qty...)
// OrderType is the discriminator column
mapped classes:
DeliveryOrder (Id,Amount,Qty...)
WorkOrder (Id,Qty,...)
SalesOrder (Id,Amount,...)
Need a query something like
Query.Where(()=>_orderAlias is DeliveryOrder)
I remember this really cool answer by Andrew Whitaker to the question:
How can QueryOver be used to filter for a specific class?
and it shows syntax like this:
q = q.Where(b => b.GetType() == typeof(DeliveryOrder));
I have a LinQ query which is intended to Update the table concerned.
The code is as follows:
LINQHelperDataContext PersonalDetails = new LINQHelperDataContext();
var PerDetails1 = (from details in PersonalDetails.W_Details_Ts
where details.UserId == userId
select details).First();
PerDetails1.Side = "Bridge";
PerDetails1.TotalBudget = 4000000;
PersonalDetails.SubmitChanges();
However, this change/update does not get reflected in the DB. Also,this does not throw any exception.Please suggest.
Make sure W_Details_Ts has one (or more) member properties marked as primary key. L2S can't generate update or delete statements if it does not know the underlying table's PK member(s).
I am creating a java bean from a master detail table. Lets call the master table A and Detail table B. When I make this in to a java bean I have BeanA and BeanB. BeanA will have all the relavant rows in the A table and will also have a list collection of BeanB (detail table B) based on the primarykey. So BeanA will look like
class BeanA {
String property1;
String Property2;
List<BeanB> lstpropery; //This is the data of detail table based on the primary key
}
In this scenerio I am using Spring JDBC to query the table. So when I query the table A and loop through the resultset and set the BeanA properties, I will also set the lstproperty by doing the query to B table.
so the code will be
String qry = "select * from A';
result = this.queryTemplate.query(qry, obj, new RowMapper(){
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
BeanA bean1= new BeanA();
bean1.setProperty1(rs.getString(1));
bean1.setProperty2(rs.getString(2));
bean1.setLstpropery(callTableB(String primaryKey)); // I see a performance issue here
return bean1;
}
});
Is there a better way to do this?
Use a left join :
select a.*, b.* from A a left outer join B b on a.id = b.aId
(You should in fact explicitely list all the columns and assign aliases to each of them).
This query will retrieve a row per couple A-B, and will also retrieve all the As which have no B.
For each row, see if you already have an A with the returned A ID. If not, construct the A and put it in a map (indexed by A ID). Then construct a B instance with the columns from the B table (unless the returned B ID is null, which means that the A has no B), and add it to the list of Bs of the A you got from the map or just constructed.
Note that ORMs like JPA make this tedious work much easier: they populate an object tree from a single request for you.
If you have a chance to use Hibernate, then lazy-fetching concept might be useful here.