Powered by Blogger.

Thursday, 31 July 2014

NamedQueries and NamedNativeQuery

By Shambhu Kumar  |  23:23 No comments

Cart class
@Entity
@Table(name = "CART")
@NamedQueries({
              @NamedQuery(name = "HQL_GET_ALL_Cart", query = "from Cart1"),
              @NamedQuery(name = "HQL_GET_CART_BY_ID", query
= "from Cart1 where id = :id") })
@NamedNativeQueries({
              @NamedNativeQuery(name = "SQL_GET_ALL_CART", query = "select cart_id,total,name from CART"),
              @NamedNativeQuery(name = "@SQL_GET_ALL_CART_BY_ID", query = "select id,total,name,items1
from Cart1 where id = :id") })
public class Cart {
       @Id
       @GeneratedValue(strategy = GenerationType.IDENTITY)
       @Column(name = "cart_id")
       private long id;

       @Column(name = "total")
       private double total;

       @Column(name = "name")
       private String name;

       @OneToMany(mappedBy = "cart")
       private Set<Items> items;

       //setter getter
}
 
Items class
@Entity
@Table(name="ITEMS")
public class Items {
       @Id
       @GeneratedValue(strategy=GenerationType.IDENTITY)
       @Column(name="id")
       private long id;
      
       @Column(name="item_id")
       private String itemId;
      
       @Column(name="item_total")
       private double itemTotal;
      
       @Column(name="quantity")
       private int quantity;
      
       @ManyToOne
       @JoinColumn(name="cart_id", nullable=false)
       private Cart cart;

            // setter getter
}

Main class
public class HibernateAnnotationMain {
       public static void main(String[] args) {
              SessionFactory sessionFactory = null;
              Session session = null;
              Transaction tx = null;
              try{
              sessionFactory = HibernateAnnotationUtil.getSessionFactory();
              session = sessionFactory.getCurrentSession();
              System.out.println("Session created");
              tx = session.beginTransaction();
              Query query = session.getNamedQuery("HQL_GET_ALL_Cart");
              List<Cart> carts= query.list();
              for(Cart cart:carts){
                     System.out.println(cart.getItems().size());
                     for(Items1 items:cart.getItems()){
                          System.out.println(items.getItemId()+"- "+items.getItemTotal());
 
                    }
              }
              System.out.println("--------------------------------");
              query = session.getNamedQuery("HQL_GET_CART_BY_ID");
              query.setInteger("id", 2);
              carts= query.list();
              for(Cart cart:carts){
                     System.out.println(cart.getItems().size());
                     for(Items items:cart.getItems()){
                           System.out.println(items.getItemId()+"-"+items.getItemTotal());
                     }
              }
              System.out.println("--------------------------------");
              query = session.getNamedQuery("SQL_GET_ALL_CART");
        List<Object[]> addressObjArray = query.list();
        for(Object[] row : addressObjArray){
            for(Object obj : row){
               System.out.print(obj + "::");
            }
            System.out.println("\n");
        }
              tx.commit();
              }catch(Exception e){
                     System.out.println("Exception occured. "+e.getMessage());
                     e.printStackTrace();
              }finally{
                     if(!sessionFactory.isClosed()){
                           System.out.println("Closing SessionFactory");
                           sessionFactory.close();
                     }
              }
       }
}
 


Author: Shambhu Kumar

Hello, I am Author, decode to know more: In commodo magna nisl, ac porta turpis blandit quis. Lorem ipsum dolor sit amet, consectetur adipiscing elit. In commodo magna nisl, ac porta turpis blandit quis. Lorem ipsum dolor sit amet.

0 comments:

Recent Articles

© 2014 Learning Java. WP themonic converted by Bloggertheme9. Published By Gooyaabi Templates
TOP