product
id_product
productName
componentID
component
componentdID
nameofComponent
what i have here is a list of products that are gonna be in Combobox and on the right it's gonna be jtable in which i need when i pick a product it lists all components that are needed to make that product. How do i make sql query in selectProduct.
Also to mention 1 product can have up to 100 components. And There is atleast 50 products
Thanks
and you should avoid the direct use on componetId in product table avoid unusefulf replication of product for mantain the relation with componenent
for this
You could create a specific table for mantain the relation between products and components eg:
table product_component ( id, product_id, componentdID )
then you can select the component of a product as
select a.productName, b.nameofComponent
from product_component c
inner join product a on a.product_id = c.product_id
inner join component b on b.componentdID = c.componentdID
and for a specific product
select a.productName, b.nameofComponent
from product_component c
inner join product a on a.product_id = c.product_id
inner join component b on b.componentdID = c.componentdID
where a.product_id = your_product_id_value
Related
I have a JPQL subquery in which I want to return a list of customerIds that meet a specific condition based on a ManyToOne relationship as shown below:
SELECT c.customerId
FROM Customer c
INNER JOIN FETCH c.customersChild cc
LEFT JOIN FETCH c.childsPet cp on cp.name = 'Rover'
GROUP BY c.customerId
HAVING (COUNT(cp.name) / COUNT(*)) = 1
In this case, the customer should only be present in the list if all of their childrens' pet's names are Rover. The HAVING (COUNT(cp.name) / COUNT(*)) = 1 clause works as-is in Oracle (SQL), since COUNT(cp.name) counts the number of non-null rows for each customer, and COUNT(*) counts the total number of rows (including nulls present due to the left join) for each customer... I believe COUNT(cp.name) works in JPQL but it doesn't seem like there is equivalent for COUNT(*)... does anyone know if there is a way to count all the rows within a group including nulls?
I would suggest you rewrite your query to the more understandable anti-join variant:
SELECT c.customerId
FROM Customer c
WHERE NOT EXISTS (
SELECT 1
FROM c.customersChild cc
JOIN cc.childsPet cp
WHERE cp.name = 'Rover'
)
I have a table which stores the orders. The orders table has an 'One to Many' mapping to another table, which stores ordered item, orderItems. For each row of the orderItems, it refers to a row in the Product table. And I have another table which stores product image, the product image table has a foreign key which points to the product. How can I use JPA specifications to join the product image in the query result of the orders?
Something like:
Order -> [orderId, ... orderItems[],...]
OrderItem -> [orderitemId, ... product,...]
Product -> [productId, productname... ]
ProductImage -> [producdImageId, .... productId...]
I am searching on Order table.
thanks!
I have created 2 tables in MySQL [items, orderList], the foreign key id in orderlist references the primary key id in items. Now I want to take all columns{id, name, price (in Items), and quantity (in orderList)} from 2 tables in Java, how can I show id once because when I query data it shows id from both tables?
You can do with join queries, try the below query and select the fields whatever you want from two tables
SELECT items.id, items.name, items.price, orderList.quantity
FROM items INNER JOIN orderList ON items.id = orderList.id
In order to fetch the data only once, you need to mention where it should come from. You can try the following:
SELECT I.ID, I.NAME, I.PRICE, O.QUANTITY FROM ORDERLIST O, ITEMS I WHERE I.ID = O.ID
Here we have given aliases to both the tables and we have mentioned that the ID column will be picked from the ITEMS table.
I've the following DB model:
Category -< ProductCategory >- Product -< Variant
(Category has many-to-many relationship with Product and Product has one-to-many relationship with Variant)
Now I need to get all Category records that have product with active variants. I'm getting these objects via the following JPQL query:
#Query("select distinct c from Category c join c.products as p join p.variants as pv where pv.active = true")
It works well - returns categories accurately - however every single Category contains all the products - not only these with active variants.
How can I filter out the products (or variants) that are inactive in a single query?
Here's a postgres script that with database struct and sample data. For given data two categories (CAT 1, CAT 2), two products (PROD 1, PROD 2) and three variants (VAR 1, VAR 2, VAR 3) should be returned.
I had exactly the same problem and it took me a while to find out how this works. The child list should be filtered when you add FETCH after your JOIN like this:
SELECT DISTINCT c FROM Category c JOIN FETCH c.products as p join p.variants as pv where pv.active = true
I have the same problem on it, and I found that the FetchType in the #OneToMany annotation is important. It need to be set as Lazy mode. If it is Eager mode, jpa will create the sql query to fetch the data from the table for your child collection and won't filter it for you.
I have a database that currently got 3 tables that i want to join together.
products
product_category
category
I have an sql join that does what i need it to do, which is taking category_id and returning all products in that category.
SELECT products.name FROM products
JOIN product_category ON products.id = product_category.product_id
JOIN categories ON categories.id = product_category.category_id
WHERE categories.id = ?
I have tried to google for a while now without success.
How can i make this happen?