NH QueryOver FetchMany Exception - vb.net

I am trying to query a parent User and then their UserRoleMappings. I have something similar to the below statement:
Dim userObj As User = session.Queryable(Of User)().Where(Function(x) x.username.Equals("someone")).FetchMany(Function(y) y.UserRoleMappings).SingleOrDefault()
When I try to run this statement I get the following exception:
A fetch request must be a simple member access expression; 'Convert(y.UserRoleMappings)' is a UnaryExpression instead.
Parameter name: relatedObjectSelector
I can confirm that I got this to work in a C# Test Case, but unfortunately I need it to work in VB. Can anyone assist?

Related

How to delete multiple entities using Doctrine QueryBuilder

I am working on a Symfony 2.8 based project to manage contact. The user can select from a list any number of contacts and should be able to delete all selected contacts at once. How can this be done in a single Query Builder statement?
// Contact entity uses a GUID as ID
$guids = array(...);
try {
$this->getEntityManager()->getConnection()->beginTransaction();
$qb = $this->getEntityManager()->getConnection()->createQueryBuilder()
->delete('AppBundle:Contact', 'c')
->where('c.guid in (:guids)')
->setParameter(':guids', array($guids, Connection::PARAM_STR_ARRAY));
log($qb->getSql());
$qb->execute();
$this->getEntityManager()->flush();
$this->getEntityManager()->getConnection()->commit();
} catch (\Exception $ex) {
// Rollback the transaction
$this->getEntityManager()->getConnection()->rollback();
}
1. Problem
Addressing the entity with AppBundle:Contact (which works without any problem when building a SELECT statement) does not work. This is the log output:
Query: DELETE FROM AppBundle:Contact c WHERE c.guid in (:guids)
Exception: Doctrine\DBAL\SQLParserUtilsException: Value for :Contact not found in params array. Params array key should be "Contact" in
2. Problem
Using the table name instead (->delete('contact', 'c')) does not work as well:
Query: DELETE FROM contact c WHERE c.guid in (:guids)
Exception: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'c WHERE c.guid in ('Array')'
3. Problem
Deleting a single entity does not work either:
->delete('contact', 'c')
->where('c.guid = (:guid)')
->setParameter(':guid', $guids[0]);
Query: DELETE FROM contact c WHERE c.guid = :guid
Exception: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'c WHERE c.guid = 'E7516B91-0549-4FFB-85F2-4BD03DC3FFC1''
What might be wrong her?
1st. Problem. Change setParameter line to the following, you don't need to use : in name of param.
->setParameter('guids', $guids);
Second problem - you should not use real table name if you're dealing with queryBuilder.
Third problem - your logic is not correct. If you want to delete single then
$qb = $this->getEntityManager()->createQueryBuilder()
->delete('AppBundle:Contact', 'c')
->where('c.guid = :guid')
->setParameter('guid', $guids[0]);
Additionally
I don't really know what doctrine version you're using, but
$this->getEntityManager()->getConnection()->createQueryBuilder() - seems wrong, because usually you're getting connection if you want to execute RAW SQL.
Try to change to
$qb = $this->getEntityManager()->createQueryBuilder()
And you need to use brackets around the variable only if it's array. Check code below
$queryBuilder->andWhere('r.id IN (:ids)')
->setParameter('ids', $ids);
Unless you want to execute raw SQL, you don't have to use your entity manager's connection, so you can replace $this->getEntityManager()->getConnection()->createQueryBuilder() by
$em->createQueryBuilder()
You could do something like
$qb = $this->createQueryBuilder()
->delete('AppBundle:Contact', 'c')
->where('c.guid in (:guids)')
->setParameter(':guids', $guids);
And if you want to log/execute it
$query = $qb->getQuery();
log($query->getSql());
$query->execute();
You also don't need to add the beginTransaction and rollback, if the query fails and an exception is thrown, doctrine will rollback automatically.

Entity Framework ExecuteStoreCommand gives {"ORA-00936: missing expression"}

I'm trying to run the following in a VB.NET application using Entity Framework 6 on an Oracle database:
mrstage.ExecuteStoreCommand("DELETE FROM CB_LISTINGS WHERE ELIGIBLE={0}", eligible)
When it executes, I get the error: {"ORA-00936: missing expression"}
On my table, ELIGIBLE is of type VARCHAR2, and the eligible variable is a string.
If I hardcode the parameter, for example:
mrstage.ExecuteStoreCommand("DELETE FROM CB_LISTINGS WHERE ELIGIBLE='ECB'")
It works fine.
I'd be very grateful if someone can offer any suggestions.
Thanks!
James
In Oracle parameters are named differently, use a column parameter like :0 instead of {0}.
So your code is now:
mrstage.ExecuteStoreCommand(
"DELETE FROM CB_LISTINGS WHERE ELIGIBLE=:0",
eligible)

How do I get the results of the Plucky Query inside my controller?

I'm missing something simple - I do not want to access the results of this query in a view.
Here is the query:
#adm = Admin.where({:id => {"$ne" => params[:id].to_s},:email => params[:email]})
And of course when you inspect you get:
#adm is #<MongoMapper::Plugins::Querying::DecoratedPluckyQuery:0x007fb4be99acd0>
I understand (from asking the MM guys) why this is the case - they wished to delay the results of the actual query as long as possible, and only get a representation of the query object until we render (in a view!).
But what I'm trying to ascertain in my code is IF one of my params matches or doesn't match the result of my query in the controller so I can either return an error message or proceed.
Normally in a view I'm going to do:
#adm.id
To get the BSON out of this. When you try this on the Decorated Query of course it fails:
NoMethodError (undefined method `id' for #<MongoMapper::Plugins::Querying::DecoratedPluckyQuery:0x007fb4b9e9f118>)
This is because it's not actually a Ruby Object yet, it's still the query proxy.
Now I'm fundamentally missing something because I never read a "getting started with Ruby" guide - I just smashed my way in here and learned through brute-force. So, what method do I call to get the results of the Plucky Query?
The field #adm is set to a query as you've seen. So, to access the results, you'll need to trigger execution of the query. There are a variety of activation methods you can call, including all, first, and last. There's a little documentation here.
In this case, you could do something like:
adm_query = Admin.where({:id => {"$ne" => params[:id].to_s},:email => params[:email]})
#adm_user = adm_query.first
That would return you the first user and after checking for nil
if #adm_user.nil?
# do something if no results were found
end
You could also limit the query results:
adm_query = Admin.where( ... your query ...).limit(1)

ReleaseCumulativeFlowData and CardState

I'm trying to run a query against the ReleaseCumulativeFlowData object as follows:
((ReleaseObjectID = 12345) AND CardState="Accepted")
However, running the query results in the following error message:
OperationResultError
Could not read: could not read all instances of class
com.f4tech.slm.domain.reporting.ReleaseCumulativeFlowDataSet
Is this a bug in Rally?
WSAPI is very picky about the structure of the query. You have to include parentheses around chained query filters, so you would need something like the following:
((ReleaseObjectID = 12345) AND (CardState = "Accepted"))

Possible to get NHibernate to execute queries as EF?

Is there anyway to use NHibernate in such a way that it whould only execute the query/queries once the returned object of a query/statement is used.. just like EF doest it?
For most instances with EF it wont send and execute the actual query to the database until the returned object of a linq-"statement" is used.. for instance:
var x = for e in entities.MyTable
select e;
This aint executed yet!
Which meens that Im able to mofidy the x-objects "linq-query" however I like without actualt "pulling" any data from the database:
x = x.Where(i=>i.SomeThing = someThing);
Still aint executed!
x.ToList<MyTable>()
Now its executed!
But in NHibernate the query gets executed once the transaction gets closed or commited from what I have understod.. and in most cases thats done already in the repository. So you can't simply in any other place alter the query and then send it to the database. Cause the query is already sent and that whould mean that you later on only whould alter whats "displayed" from the result.
I might have gotten this al wrong so please correct me if I am wrong.
Huge thanks in advance!
You can try something like this using detached QueryOver
var query = QueryOver.Of<Customer>()
.Where(x => x.LastName == "Smith"); // query is not executed yet
query.GetExecutableQueryOver(session).List();
Good thing is that you can pass QueryOver objects and get it executed somewhere else.