subCategory did not passing data in database?

0 votes
asked Nov 15, 2016 in Hibernate by dipu_j (120 points)
package AddHandlar;

import Dao.AddDao;
import Dao.ListDao;
import entity.Category;
import entity.SubCategory;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import org.hibernate.HibernateException;

@ManagedBean
@SessionScoped
public class AddSubCategoryMB {

Category cat = new Category();
SubCategory subcat = new SubCategory();

String catName;
List<Category> listcat;

public Category getCat() {
    return cat;
}

public void setCat(Category cat) {
    this.cat = cat;
}

public String getCatName() {
    return catName;
}

public void setCatName(String catName) {
    this.catName = catName;
}

public List<Category> getListcat() {
    return listcat;
}

public void setListcat(List<Category> listcat) {
    this.listcat = listcat;
}

public SubCategory getSubCat() {
    return subcat;
}

public void setSubCat(SubCategory subcat) {
    this.subcat = subcat;
}

public String addSubCategory() {
    try {
        subcat.setSubCatId(subcat.getSubCatId());
        listcat = new ListDao().cListByName(catName);

        System.out.println(listcat.get(0).getCatId());
        cat.setCatId(listcat.get(0).getCatId());

        subcat.setSubCatName((String)subcat.getSubCatName());
        subcat.setSubCatDesc((String)subcat.getSubCatDesc());

        boolean status = new AddDao().addSubcategory(subcat);
        if (status) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Done", "Data Saved"));

        } else {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Not Done", "Date Not Saved"));
        }

    } catch (HibernateException e) {
        System.err.println(e);

    }
    return null;
}

public List<SelectItem> getCagtegoryName() {
    List<SelectItem> catname = new ListDao().catlist();
    return catname;
}

}

package Dao;

import entity.Category;
import entity.SubCategory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import util.HibernateUtil;

public class AddDao {

public boolean addCategory(Category cat) {

    try {

        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session session = factory.openSession();
        session.beginTransaction();
        session.save(cat);
        session.getTransaction().commit();
        session.close();

        return true;

    } catch (HibernateException e) {
        System.err.println(e.getMessage());
        return false;
    }

}

public boolean addSubcategory(SubCategory subcat) {

    try {

        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session session = factory.openSession();
        session.beginTransaction();
        session.save(subcat);
        session.getTransaction().commit();
        session.close();
        return true;
    } catch (HibernateException ex) {
        System.err.println(ex.getMessage());
        return false;
    }

}

}

package Dao;

import entity.Category;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import util.HibernateUtil;

public class ListDao {

public List catlist() {
    SessionFactory factory = HibernateUtil.getSessionFactory();
    Session session = factory.openSession();

    List<Category> cList = session.createQuery("SELECT al.catName from Category al").list();
    cList.toString();
    session.close();
    return cList;
}

public List<Category> cListByName(String name) {
    SessionFactory factory = HibernateUtil.getSessionFactory();
    Session session = factory.openSession();

    List<Category> cList = session.createQuery("SELECT al.catName from Category al WHERE lower(catName) = '"+name.toLowerCase()+"'").list();
    cList.toString();
    session.close();
    return cList;
}

}

<?xml version='1.0' encoding='UTF-8' ?>

<!DOCTYPE html>


SubCategory

    <h:form>
        <p:messages id="msg" closable="true" showDetail="true" ></p:messages>
        <p:panelGrid columns="2">
            <h:outputLabel value="Subcategory Id"></h:outputLabel>
            <p:inputText value="#{addSubCategoryMB.subCat.subCatId}"></p:inputText>
            <h:outputLabel value="Select Category Name"></h:outputLabel>

            <p:selectOneMenu value="#{addSubCategoryMB.catName}">
                <f:selectItems value="#{addSubCategoryMB.cagtegoryName}"></f:selectItems>
            </p:selectOneMenu>


            <h:outputLabel value="SubCategory Name"></h:outputLabel>
            <p:inputText value="#{addSubCategoryMB.subCat.subCatName}"></p:inputText>
            <h:outputLabel value="SubCategory Description"></h:outputLabel>
            <p:inputText value="#{addSubCategoryMB.subCat.subCatDesc}"></p:inputText>
            <p:commandButton action="#{addSubCategoryMB.addSubCategory()}" value="Save" update="msg" icon ="ui-icon-arrowstop-1-n"></p:commandButton>
        </p:panelGrid>
    </h:form>
</h:body>

1 Answer

0 votes
answered Nov 17, 2016 by smiraj (460 points)

change the following code:

public List cListByName(String name) {
SessionFactory factory = HibernateUtil.getSessionFactory();
Session session = factory.openSession();
List cList = session.createQuery("SELECT a1 FROM Category a1 where lower(catName)='" + name.toLowerCase() + "'").list();
cList.toString();
session.close();
return cList;
}

722 questions

656 answers

179 comments

4,747 users

Welcome to IDB Scholars Forum, where you can ask questions and receive answers from other members of the community. It's 100% free.
আইডিবি স্কলারস ফোরামে আপনাকে স্বাগত - প্রোগ্রামিং, ডিজাইন, ডেভেলপমেন্ট, ব্লগিং, ইন্টারনেটসহ তথ্য প্রযুক্তির যাবতীয় বিষয় নিয়ে আলোচনার একটি মুক্ত মঞ্চ হলো এই ফোরাম । এখানে আইসিটি বিষয়ক আপনার প্রশ্ন, উত্তর এবং অভিজ্ঞতা শেয়ার করুন, আলোচনা করুন এবং সমাধান নিন। দেশের তরুণ প্রজন্মকে আইসিটিতে আগ্রহী করাই আমাদের মূল লক্ষ্য।
** ইংলিশ বা বাংলা যে কোনো ভাষায় আপনি এই ফোরামে আলোচনা করতে পারবেন। তবে বাংলাকে ইংরেজি অক্ষরে লিখবেন না।
  1. tulbadex

    110 points

    0 answers

  2. JettaQoa3381

    100 points

    0 answers

  3. clutch7conga

    100 points

    0 answers

  4. WhitneyMauer

    100 points

    0 answers

  5. pajamazipper55

    100 points

    0 answers

...