Hi am new to java server side to create JSON API, am using ManytoMany mapping in hibernate to join the two tables.I have two classes one is Product.class and Offers.class.
Product.class
@Entity
@Table(name = "products")
public class Product {
@Column(name = "merchant_code")
private String merchant_code;
@Column(name = "branch_code")
private String branch_code;
@Column(name = "product_category_code")
private String product_category_code;
@Id @GeneratedValue
@Column(name = "product_code")
private String product_code;
@Column(name = "product_short_desc")
private String product_short_desc;
@Column(name = "product_long_desc")
private String product_long_desc;
@Column(name = "image")
private String image;
@Column(name = "price")
private String price;
@Column(name = "Active_Inactive")
private String Active_Inactive;
@ManyToMany(mappedBy = "offer_relation_code", fetch = FetchType.EAGER)
@Where(clause = "offer_type_code = 1")
private List<Offers> offer;
//here my getter setter
}
Offers.class
@Entity
@Table(name = "offers")
public class Offers {
@Id @GeneratedValue
@Column(name = "offer_code")
private int offer_code;
@Column(name = "offer_type_code")
private int offer_type_code;
@Column(name = "offer_relation_code")
private int offer_relation_code;
@Column(name = "branch_code")
private int branch_code;
@Column(name = "valid_from")
private String valid_from;
@Column(name = "valid_until")
private String valid_until;
@Column(name = "offer_value")
private int offer_value;
@Column(name = "offer_desc")
private String offer_desc;
//here my getter setter     
}
To fetch data
factory = cfg.configure().addAnnotatedClass(Product.class).buildSessionFactory(registry);
    Session session = factory.openSession();
    Transaction tx = null;
    try {
        tx = session.beginTransaction();
        Criteria criteria = session.createCriteria(Product.class);
        criteria.setFetchMode("product",FetchMode.JOIN);
        Criterion merchant_code_Criterion = Restrictions.eq("merchant_code", new String(merchant_code));
        Criterion branch_code_Criterion = Restrictions.eq("branch_code", new String(branch_code));
        LogicalExpression andExp = Restrictions.and(merchant_code_Criterion,branch_code_Criterion);
        criteria.add(andExp);
        search_products = (ArrayList<Product>) criteria.list();
        tx.commit();
    } catch (HibernateException e) {
        // TODO: handle exception
        if (tx != null)
            tx.rollback();
        e.printStackTrace();
    } finally {
        session.close();
    }
Am join the offer table with product table like @ManyToMany(mappedBy = "offer_relation_code", fetch = FetchType.EAGER) am search it in Google ,many of them said don't use EAGER, it leads to some issue, but when i am using @ManyToMany(mappedBy = "offer_relation_code", fetch = FetchType.LAZY) is shows error like failed to lazily initialize a collection of role: could not initialize proxy - no Session. When am using EAGER its working fine without error.Using EAGER is good or bad.Can any one Explain.
 
     
     
    