Home java How to Group ResultSet in Map By Objects (Java, JDBC)

How to Group ResultSet in Map By Objects (Java, JDBC)

Author

Date

Category

Hi) I have two classes that I need to create, getting some data for them from dB:

@ data
@Allargsconstructor
@Noargsconstructor
Public Class Product Implements Replication & LT; Product & GT; {
  Private Long ID;
  Private String Name;
  Private Long Priceincents;
  Private int rarate;
  Public Product (String Name, Long Preincents, Int Rate) {
    this.name = Name;
    this.priceincents = PriceinCents;
    this.rate = rate;
  }
  @Override
  Public Product Copy () {
    Return New Product (ID, NAME, PRIINCENTS, RATE);
  }
}
@ data
@Allargsconstructor
@Noargsconstructor
Public Class Category Implements Replication & LT; Category & GT; {
  Private Long ID;
  Private String Title;
  Private List & lt; Product & GT; Products;
  @Override
  Public Category Copy () {
    List & lt; Product & GT; CopiedProducts = this.Products.stream ()
        .map (Product :: Copy)
        .collect (collectors.tolist ());
    RETURN NEW CATEGORY (ID, TITLE, COPIEDPRODUCTS);
  }
}

I’ll get them so — & gt; select * from Products P Join Categories C ON P.Category_id = C.category_id

After executing the query, they fall into the resultset. How can I handle ResultSet to do somehow from it Stream, ultimately, creating all objects? Maybe there is an option at least somehow group the result to provide where one category ends and the other starts?

For example, the desired result would be Map & Lt; Category, List & LT; Product & GT; & GT;

BD scripts:

create database shop;
Create Table Categories (
Category_id bigint not null auto_increment primary key
Category_name Varchar (100) Not NULL
);
CREATE TABLE PRODUCTS (
    Product_ID Bigint Not Null Auto_Increment Primary Key,
    Product_Name Varchar (100) Not NULL,
    Price_IN_CENTS bigint,
    RATE INT,
    Category_id Bigint Not Null,
      Constraint fk_products_categories.
        Foreign Key (Category_id) References Categories (Category_ID)
    ON DELETE CASCADE.
);

Answer 1

Question decided. If someone helps, nothing to implement (spliters and other):

public list & lt; Category & gt; Findall () {
    String Query = "Select * from Categories C join Products P on C.category_id = p.category_id";
    MAP & LT; Category, List & LT; Product & GT; & GT; GroupedCategories = New Linkedhashmap & lt; & gt; ();
    TRY (Connection Connection = ConnectionProvider.getConnection ();
       Statement statement = connection.createTatement ();
       ResultSet ResultSet = Statement.ExecuteQuery (Query)) {
      While (resultset.next ()) {
        Category Category = New Category ();
        Category.Setid (resultset.getlong ("Category_ID"));
        Category.Settitle (resultset.getstring ("Category_name"));
        if (! groupedcategories.containskey (Category)) {
          GroupedCategories.Put (Category, New ArrayList & LT; & GT; ());
        }
        Product Product = NEW Product ();
        Product.Setid (resultset.getlong ("product_id"));
        Product.Setrate (ResultSet.getint ("RATE"));
        Product.setName (resultset.getString ("Product_Name"));
        Product.SetPriceInCents (ResultSet.getlong ("Price_in_Cents"));
        GroupedCategories.get (Category) .add (Product);
      }
    } Catch (SQLEXCEPTION E) {
      Throw New DatabaseException (E.getMessage ());
    }
    Return GroupedCategories.EntrySet (). Stream ()
        .peek (entry - & gt; entry.getkey (). setProducts (entry.getvalue ()))
        .map (map.ENTRY :: Getkey)
        .collect (collectors.tolist ());
  }

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions