nHibernate abstractCriterion and making dynamically query - nhibernate

What i need to do is make query in nHibernate (fully dynamically i don't know how many objects i will have).
What this query should returns are objects of specific Ids(i got List<int>).
Is there any way to make restriction like
Restrictions.Eq("Id",first item from my list of ints).Or("Id",second item) .... and so on.
I know i can make it with AbstractCriterion but have no idea how to check if object from my List<int> is first one.
So how can I make that?
Thanks for advance:)

You need the In criteria:
session.CreateCriteria(typeof(XYZ))
.Add(Expression.In("Id", values))

Related

Updating SQL from object with groovy

When you read in a result set in Groovy it comes in a collection of maps.
Seems like you should be able to update values inside those maps and write them back out, but I can't find anything built into groovy to allow me to do so.
I'm considering writing a routine that allows me to write a modified map by iterating over the fields of one of the result objects, taking each key/value pair and using them to create the appropriate update statement, but it could be annoying so I was wondering if anyone else had done this or if it'sa vailable already in groovy.
It seems like just a few lines of code so I'd rather not bring in hibernate for this. I'm just thinking a little "update" method that would allow:
def rows=sql.rows(query)
rows[0].name="newName"
update(sql, rows[0])
to update the first guy's name in the database. Anyone seen/created such a monster, or is something like this already built into Groovy Sql and I'm just missing it?
(I suppose you may have to point out to the update method which field is the key field, but that's doable...)
Using the rows method will actually read out all of the values into a List of GroovyRowResult so it's not really possible to update the data without creating an update method like the one you mention.
It's not really possible to do that in the generic case because your query can contain joins or a column reference that is an aggregate, etc.
If you're selecting from a single table use the Sql.eachRow method however and set the ResultSet to be an updatable one, you can use the underlying ResultSet interface to update as you iterate through:
sql.resultSetConcurrency = ResultSet.CONCUR_UPDATABLE
sql.resultSetType = ResultSet.TYPE_FORWARD_ONLY
sql.eachRow(query) { row ->
row.updateString('name', 'newName')
row.updateRow()
}
Depending on the database/driver you use, you may not be able to create an updatable ResultSet.

NHibernate restriction Count of child collection

Take for example: a Person that has a collection of Pets. I want to only list the Persons that have at least 5 pets.
I have tried:
var result = (from a in UnitOfWork.CurrentSession.QueryOver<Person>()
where a.Pets.Count >4
select a
).List()
But it says it does not recognize the property Count (which makes sense because it is not a DB field). I also tried Count() and it still doesn't work saying it doesn't understand that function (throws exception).
I've tried all kinds of subqueries and criteria methods but I don't know enough to put it all together. And I don't know whether I shold use LINQ or HQL or QueryOver or Criteria...It would be much much mch easier in SQL but I don't want to "cheat"
I have been searching google like crazy, and everything I found either does not compile or I get a runtime error
You are using QueryOver instead of LINQ (Query<T>() extension method)

Coldfusion ORM relationship id list

I'm wondering if there is a simple way to get an ID list of all the target objects relating to a source object in a coldfusion component using ORM?
I can see that you can do a collection mapping for one-to-many relationships, but I am using many-to-many relationships. I don't want to have to get the array of objects and then loop over it to get each id.
Is there any built in function or property that could do this?
I think something like the code sample below is a little too heavy since it is getting the whole query and then getting a single column from it.
valuelist( EntityToQuery( object.getRelationalFields() ).id )
Sometimes it doesn't make sense to use ORM, and this is the time. Use the good old <cfquery> for this!
I think ORMExecuteQuery may work for you, something like this:
result = ORMExecuteQuery("select id from Model as m where m.parent.id = :id", {id = 123});
Actual clause format depends on the relationship definition.
In a result you'll have the array of model PKs.

In NHibernate, can I have a criteria query return a list of one property instead of a list of objects?

I'm a bit green with the criteria api. So far, I've been able to get a list of nhibernate objects by using .List().ToList() on an ICriteria object.
I was just wondering if it was possible, say, to get a list of ints of all the ID #s of the objects that would have been returned, instead of returning the objects.
I know you can do this with HQL but I'm searching the database with dynamic lists from the user gui so the criteria api seems to be the better option.
Thanks.
Isaac
You can use Projections to return only a subset of the properties of an object.

load record by predicate/lambda via vb.net linq

im pretty confused about lambdas and actually im not even sure i need them here
what im trying to do here is write a function that will return an object from a certain table with a certain criteria
so lets say i can write
function GetRecord(TableName as string,Criteria as string) as object
'do the linq-stuff
end function
now i dont care if the paremeters are strings or lambdas or whatever, but the end result must be that at runtime i dont know which table and which criteria will be used
as sometimes i need to get a customer record by email and sometimes a product by id etc.
if possible i would prefer returning a list of matching objects and then i would just use .firstordefault when i want 1 (such as by id...)
thank you , as always, for taking the time to read this and answer!
all the best
Have you considered using Dynamic LINQ?
Example:
Parsing an expression tree can be a challenging but rewarding method of solving this issue. I think it's overkill and I'd go with Dynamic Linq as decyclone mentioned.
A benefit of parsing the expression tree, however, is that you can have compile time checking of the submitted criteria.
Here are some articles that helped me.
How to: Implement an Expression Tree Visitor: http://msdn.microsoft.com/en-us/library/bb882521(VS.90).aspx
Building a custom IQueryable Provider: http://blogs.msdn.com/b/mattwar/archive/2008/11/18/linq-links.aspx
Walkthrough: Creating an IQueryable LINQ Provider: http://msdn.microsoft.com/en-us/library/bb546158(v=VS.90).aspx
Expression Tree Basics:
http://blogs.msdn.com/b/charlie/archive/2008/01/31/expression-tree-basics.aspx