0 Replies Latest reply: Mar 7, 2012 10:26 PM by 922236 RSS

    Many-to-one relationship using annotations in hibernates...

    922236
      my pojo class is M_Product_Category
      ----------------------------------------------------
      package com.netree.model;
      import java.io.Serializable;
      import java.util.HashSet;
      import java.util.Set;

      import javax.persistence.CascadeType;
      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.FetchType;
      import javax.persistence.GeneratedValue;
      import javax.persistence.Id;
      import javax.persistence.Table;
      import javax.persistence.OneToMany;
      import javax.persistence.JoinColumn;
      @Entity
      @Table(name="m_product_category")
      public class M_Product_Category implements Serializable{
           
           @Id
           @Column(name="MPC_ID")
           @GeneratedValue
           private int mpc_Id;
           
           
           @Column(name="IsActive")
           private int isActive;
           
           @Column(name="Category_Name")
           private String category_Name;
           
           @Column(name="Category_Description")
           private String category_Description;
           
           
           @Column(name="Parent_MPC_ID")
           private int parent_MPC_ID;
           
           
           public M_Product_Category(){
                
           }

           public M_Product_Category(int mpc_Id, int isActive, String category_Name,
                     String category_Description, int parent_MPC_ID) {
                super();
                this.mpc_Id = mpc_Id;
                this.isActive = isActive;
                this.category_Name = category_Name;
                this.category_Description = category_Description;
                this.parent_MPC_ID = parent_MPC_ID;
           }

           public int getMpc_Id() {
                return mpc_Id;
           }

           public void setMpc_Id(int mpc_Id) {
                this.mpc_Id = mpc_Id;
           }

           public int getIsActive() {
                return isActive;
           }

           public void setIsActive(int isActive) {
                this.isActive = isActive;
           }

           public String getCategory_Name() {
                return category_Name;
           }

           public void setCategory_Name(String category_Name) {
                this.category_Name = category_Name;
           }

           public String getCategory_Description() {
                return category_Description;
           }

           public void setCategory_Description(String category_Description) {
                this.category_Description = category_Description;
           }

           
           @OneToMany(cascade=CascadeType.ALL)
           @JoinColumn(name="Parent_MPC_ID", referencedColumnName="MPC_ID")
           
           public int getParent_MPC_ID() {
                return parent_MPC_ID;
           }

           public void setParent_MPC_ID(int parent_MPC_ID) {
                this.parent_MPC_ID = parent_MPC_ID;
           }

           
      }



      This is anothe pojo class i.e., ProductMaster:
      -----------------------------------------------------------------
      package com.netree.model;

      import java.io.Serializable;

      import javax.persistence.CascadeType;
      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.Id;
      import javax.persistence.JoinColumn;

      import javax.persistence.ManyToOne;
      import javax.persistence.Table;

      @Entity
      @Table(name = "M_Product_Master")
      public class ProductMaster implements Serializable {
           @Id
           @GeneratedValue
           @Column(name = "MPM_ID")
           private int mpmID;

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

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

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

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

           @Column(name = "MPC_ID")
           private int mpc_id;

           public int getMpc_id() {
                return mpc_id;
           }

           public void setMpc_id(int mpc_id) {
                this.mpc_id = mpc_id;
           }

           private M_Product_Category product_Category;

           @ManyToOne(cascade = CascadeType.ALL)
           @JoinColumn(name = "mpc_id", referencedColumnName = "MPC_ID")
           public M_Product_Category getProduct_Category() {
                return product_Category;
           }

           public void setProduct_Category(M_Product_Category product_Category) {
                this.product_Category = product_Category;
           }

           public int getMpmID() {
                return mpmID;
           }

           public void setMpmID(int mpmID) {
                this.mpmID = mpmID;
           }

           public String getProductName() {
                return productName;
           }

           public void setProductName(String productName) {
                this.productName = productName;
           }

           public String getProductDescription() {
                return productDescription;
           }

           public void setProductDescription(String productDescription) {
                this.productDescription = productDescription;
           }

           public String getPrice() {
                return price;
           }

           public void setPrice(String price) {
                this.price = price;
           }

           public String getImage() {
                return image;
           }

           public void setImage(String image) {
                this.image = image;
           }

      }

      -------------------------------------------
      util class...
      -------------------------------------------

      package com.netree.Util;


      import org.hibernate.SessionFactory;
      import org.hibernate.cfg.AnnotationConfiguration;

      public class HibernateUtil {
           
           public static SessionFactory getSessionFactory() {
                System.out.println("sessionFactory");
                AnnotationConfiguration annotationConfiguration = new AnnotationConfiguration();
                System.out.println("annotationConfiguration");
                annotationConfiguration=annotationConfiguration.configure("com/netree/Cfg/Hibernate.cfg.xml");
                SessionFactory sessionFactory = annotationConfiguration.buildSessionFactory();

                return sessionFactory;

      }
      }

      -------------------------------------------------------------

      package com.netree.DaoImpl;

      import java.util.Iterator;
      import java.util.List;

      import org.hibernate.Criteria;
      import org.hibernate.Session;
      import org.hibernate.SessionFactory;
      import org.hibernate.Transaction;
      import org.hibernate.criterion.Restrictions;

      import com.netree.Util.HibernateUtil;
      import com.netree.model.ProductMaster;

      public class ProductMasterImpl {

      public static void main(String args[]) {

                               try{
                                    System.out.println("sessionFactory starts");
                SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
                System.out.println("sessionFactory ends");
                Session session=sessionFactory.getCurrentSession();
                
                                    Transaction transaction=session.beginTransaction();
           Criteria ctr=session.createCriteria(ProductMaster.class).add(Restrictions.eq("mpc_id",new Integer(39)));
           
           
           List<ProductMaster> list=ctr.list();
           

                     @SuppressWarnings("rawtypes")
                                    Iterator iterator = list.iterator();

                     while (iterator.hasNext()) {
                          ProductMaster productMaster = (ProductMaster) iterator.next();
      System.out.println(productMaster.getMpmID());
      System.out.println(productMaster.getProductName());
      System.out.println(productMaster.getProductDescription());
      System.out.println(productMaster.getPrice());
      System.out.println(productMaster.getImage());

      System.out.println(productMaster.getProduct_Category().getMpc_Id());
      System.out.println(productMaster.getProduct_Category().getCategoryName());
      System.out.println(productMaster.getProduct_Category().getCategory_Description());

                     }
                     session.close();
                               }
                     
           catch(Exception ex){
           System.out.println(ex.getMessage());
           }
                          finally{
                               
                          }
                               }
      }
      Already i have two tables in database with the suitable records... I want to retrieve the data by using MPC_ID from both tables....

      when i run this program i am getting query but it is showing exception i.e....

      Hibernate: select this_.MPM_ID as MPM1_1_0_, this_.Image_URL_Big as Image2_1_0_, this_.MPC_ID as MPC3_1_0_, this_.Standard_Cost as Standard4_1_0_, this_.Product_Description as Product5_1_0_, this_.Product_Name as Product6_1_0_, this_.product_Category as product7_1_0_ from M_Product_Master this_ where this_.MPC_ID=?
      could not execute query


      could u pls help me in this regard.....