title: How it copy one object fields data to another object of the same class?
body: <p>In the context of problem, Category table having two rows mapped to Stock. But When I apply below logic i am getting only one row entry in result.</p>
  
  <p>To solve that i want to know how to copy an object properties to newly created object of the same class?
      So that newly created object will have the data of older object.</p>
  
  <pre><code>public class App {
      public static void main(String[] args) {
          System.out.println("Hibernate many to many (XML Mapping)");
          Session session = HibernateUtil.getSessionFactory().openSession();
          session.beginTransaction();
  
          try {
              List&lt;Stock&gt; stocksNew=new ArrayList&lt;Stock&gt;();
              List&lt;Stock&gt; stocks = session.createQuery("FROM Stock").list(); 
  
              for (Stock stock:stocks) {
                  Set&lt;Category&gt; categories = stock.getCategories();
  
                  if (categories.isEmpty())
                  {
                      System.out.println("true");
                      stocksNew.add(stock);
                  } 
                  else
                  {   
                      for (Iterator&lt;Category&gt; iterator2 = 
                          categories.iterator(); iterator2.hasNext();) {
                          Category category = (Category) iterator2.next();
                          stock.
                          stock.setCategoryId(category.getCategoryId());
                          stock.setName(category.getName());
                          stocksNew.add(stock);
  
                      }
                  }   
              }
  
              for (Stock stock : stocksNew) {
                  System.out.println("Stock ID: " + stock.getStockId()); 
                  System.out.println("Stock Name: " + stock.getStockName()); 
                  System.out.println("Stock  Code: " + stock.getStockCode()); 
                  System.out.println("Category Id:"+stock.getCategoryId());
                  System.out.println("Category Name:"+stock.getName());
              }
  
  
          } catch (HibernateException e) {
              e.printStackTrace(); 
          } finally {
              session.close(); 
          }
      }
  
  }
  </code></pre>
  
  <p><strong>Output</strong></p>
  
  <pre><code>Stock ID: 1
  Stock Name: PADINI
  Stock  Code: 7052
  Category Id:2
  Category Name:INVESTMENT
  Stock ID: 1
  Stock Name: PADINI
  Stock  Code: 7052
  Category Id:2
  Category Name:INVESTMENT
  </code></pre>
  
  <p>I am getting the above output, but in my requirement output should come in the fallowing manner</p>
  
  <pre><code>Stock ID: 1
  Stock Name: PADINI
  Stock  Code: 7052
  Category Id:1
  Category Name:CONSUMER
  Stock ID: 1
  Stock Name: PADINI
  Stock  Code: 7052
  Category Id:2
  Category Name:INVESTMENT
  </code></pre>