I have 2 entities namely Product and Transaction and these 2 entities are link via a Many to Many relationship. I have the below in my transaction entity.
#ManyToMany(cascade={CascadeType.ALL})
#JoinTable(name="Transaction_Product")
When I run my project, there will be 3 tables tables created in my database and they are Transaction, Product and Transaction_Product respectively. The Transaction_Product is automatically generated when I run my project.
I am able to get items from my transaction table via this query below.
Query q = em.createQuery("SELECT t FROM Transaction t WHERE t.fulfillStatus = 0");
My question is, how do I get items from the Transaction_Product table?
I tried something like the query below and it did not work.
Query q = em.createQuery("SELECT bt FROM Transaction_Product bt WHERE bt.ProductID = 1);
Any help please? :)
checking if there are transactions with specifyed product u can use :
Query q = em.createQuery("SELECT t FROM Transaction t join t.products p WHERE p.id = :id");
Assuming that Transaction class has field :
#ManyToMany(cascade={CascadeType.ALL})
#JoinTable(name="Transaction_Product")
private Set<Product> products;
Related
How to join below two tables using gosu .. This is from Contact Manager (GW Training App)
1 ABContact
2 BankAccount
Below SQL can be used to find all account hold by one particluar contact
select b.accountnumber from ABContact a, BankAccount b where
a.id=b.contactid and a.id='123'
Please write the same query in Gosu
Query is like this
uses gw.api.database.Query
var account: BankAccount
var query = Query.make(ABContact).join("ID", BankAccount, "Contact").compare(BankAccount#ID, Equals, account.ID)
I am looking to get the count of all documents in a chosen partition. The following code however will return the count of all documents in the collection and costs 0 RU.
var collectionLink = UriFactory.CreateDocumentCollectionUri(databaseId, collectionId);
string command = "SELECT VALUE COUNT(1) FROM Collection c";
FeedOptions feedOptions = new FeedOptions()
{
PartitionKey = new PartitionKey(BuildPartitionKey(contextName, domainName)),
EnableCrossPartitionQuery = false
};
var count = client.CreateDocumentQuery<int>(collectionLink, command, feedOptions)
.ToList()
.First();
adding a WHERE c.partition = 'blah' clause to the query will work, but costs 3.71 RUs with 11 documents in the collection.
Why would the above code snippet return the Count of the whole Collection and is there a better solution to for getting the count of all documents in a chosen partition?
If the query includes a filter against the partition key, like SELECT
* FROM c WHERE c.city = "Seattle", it is routed to a single partition. If the query does not have a filter on partition key, then it is
executed in all partitions, and results are merged client side.
You could check the logical steps the SDK performs from this official doc when we issue a query to Azure Cosmos DB.
If the query is an aggregation like COUNT, the counts from individual
partitions are summed to produce the overall count.
So when you just use SELECT VALUE COUNT(1) FROM Collection c, it is executed in all partitions and results are merged client side.
If you want to get the count of all documents in a chosen partition, you just add the where c.partition = 'XX' filter.
Hope it helps you.
I believe this is actually a bug since I am having the same problem with the partition key set in both the query and the FeedOptions.
A similar issue has been reported here:
https://github.com/Azure/azure-cosmos-dotnet-v2/issues/543
And Microsoft's response makes it sound like it is an SDK issue that is x64-specific.
I am using lightswitch and i have to join 3 tables in my query. Table A join Table B join table C where tableC.id == 10
partial void Query2_PreprocessQuery(int? dept, ref IQueryable query)
{
query = query.Join(Employee_Personal_Infoes, b => b.Employee_Personal_Info1.Emp_id, (b));
}
First off, if you add relationships between your tables, you shouldn't ever need to do manual joins. You would then just use the navigation properties that get created when you add the relationships.
Second, you can't change the shape of the entity in a query, or return a set of different entities. If your query is based say on a Customer entity, you can't return a query of CustomerAdresses, or a Client entity from that query. The query can only return a filtered set of the same entity that the query is based on.
Does that make sense?
Hi is there an easy way to get the values of a foreign key, without writing sql queries.
I generated the code with the help of my mssql database (ADO.NET).
Here's an example for clarification
order table:
id customer_fk
1 100
2 105
customer table:
id name
100 Walter
105 White
view:
#model ...order
...
#customer_fk
#customer_fk delivers eg. "100" instead of "Walter"
Not sure if you are required to use ADO.NET, but to accomplish what you want, without writing sql, you'll need to use some kind of ORM, such as EntityFramework.
You will need to write LINQ, which generates SQL and since EF will know about the relationship between the two tables, you will have access to the "name" property in the customer table.
Linq to SQL might be a good option, since you're using MS SQL. Just add the DataContext item to your project, and drag/drop the tables from your server. Then you should be able to write something like:
public ActionResult Order(int orderId)
{
using (MyDataContext context = new MyDataContext())
{
var loadOptions = new LoadOptions();
loadOptions.LoadWith<order>(o => o.customer);
context.LoadOptions = loadOptions;
var theOrder = context.orders.Where(order => order.id == orderId).FirstOrDefault();
return View(theOrder);
}
}
But if you're already using ADO.Net, maybe just write the query? It's not that difficult:
SELECT order.*, customer.name
FROM order INNER JOIN customer ON order.customer_fd = customer.id
If you're doing this in Entity Framework (or some other similar ORM), you can write a LINQ query that joins to your foreign key table:
var customerOrders =
from o in context.Orders
join c in context.Customers on o.customer_fk equals c.ID
select new { OrderID = o.ID, CustomerName = c.Name };
I have an SQL query that lists the uid of all users who have a certain role:
SELECT u.uid
FROM {users} as u, {users_roles} as ur
WHERE u.uid = ur.uid AND ur.rid = 10 ORDER BY u.uid DESC
I need to load them all in an array as objects for listing.
I asked a question previously that left me only with the answer that what I wanted to do would be easier done without Views - so I'm going use a template file instead.
Therefore this question.
I know how to do this, but apparently my method is worth -2
This is how I want to do it:
$research['sql'] = "SELECT u.uid FROM {users} as u, {users_roles} as ur WHERE u.uid = ur.uid AND ur.rid = 10 ORDER BY u.uid DESC";
$research['sql_result'] = db_query($alumni['sql']);
// Load user objects and store in array
while($user_array = db_fetch_array($research['sql_result'])) {
// Create user objets based on uid
$user_obj = user_load($user_array['uid']);
// Load profile
profile_load_profile($user_obj);
$research['users'][$user_obj->uid] = $user_obj;
}
Please help me with how I should do it.
Your basic approach looks fine by me, except that the call to profile_load_profile() is redundant:
The user_load() function will invoke hook_user with operation 'load', and the profile module implements hook_user and calls profile_load_profile() for the load operation itself, so by the time you call it explicitly, it has already been called implicitly and you can just drop it.