I could Category module in JSF but I couldn't add Category because of hibernate issue. Here are the code below.
Category.xhtml
Code:
<p:panelGrid columns="2">
<p:outputLabel value="Category Name "></p:outputLabel>
<p:inputText value="#{categoryMB.name}"></p:inputText>
<p:outputLabel value="Category Description"></p:outputLabel>
<p:inputText value="#{categoryMB.catDesc}"></p:inputText>
<p:commandButton value="Add Category" action="#{categoryMB.addCategory()}"></p:commandButton>
</p:panelGrid>
Category.java
Code:
public class Category implements Serializable{
@Id
@SequenceGenerator(name = "catseq", sequenceName = "seqCatSEQ", allocationSize = 1)
@GeneratedValue(generator = "catseq", strategy = GenerationType.SEQUENCE)
@Column(name = "ID")
private Integer id;
@Column(name = "NAME")
private String name;
@Column(name = "CAT_DESC")
private String catDesc;
public Category() {
super();
// TODO Auto-generated constructor stub
}
public Category(String name, String catDesc) {
super();
this.name = name;
this.catDesc = catDesc;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCatDesc() {
return catDesc;
}
public void setCatDesc(String catDesc) {
this.catDesc = catDesc;
}
HibernteUtil.java
Code:
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
return configuration.buildSessionFactory(
new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build());
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
CategoryMB.java
Code:
public class CategoryMB {
Category category = new Category();
private String name;
private String catDesc;
private List<Category> categorylist = new ArrayList<Category>();
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCatDesc() {
return catDesc;
}
public void setCatDesc(String catDesc) {
this.catDesc = catDesc;
}
public List<Category> getCategorylist() {
return categorylist;
}
public void setCategorylist(List<Category> categorylist) {
this.categorylist = categorylist;
}
public void addCategory(){
category.setName(getName());
category.setCatDesc(getCatDesc());
new CategoryDAO().addCategory(category);
}
}
CategoryDAO.java
Code:
public class CategoryDAO implements ICategoryDAO{
@Override
public void addCategory(Category category) {
// TODO Auto-generated method stub
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction trx = session.getTransaction();
try {
trx.begin();
session.persist(category);
trx.commit();
} catch (Exception e) {
// TODO: handle exception
System.out.println(e);
if (trx != null)
trx.rollback();
} finally {
session.close();
}
}
@Override
public void deleteCategory(int categoryId) {
// TODO Auto-generated method stub
}
@Override
public void updateCategory(int categoryId) {
// TODO Auto-generated method stub
}
@Override
public List<Category> getAllcategories() {
// TODO Auto-generated method stub
Session session = HibernateUtil.getSessionFactory().openSession();
try {
List<Category> liste = session.createQuery("from Category C order by C.id asc").list();
return liste;
} catch (Exception e) {
// TODO: handle exception
}finally {
session.close();
}
return null;
}
@Override
public Category getCategoryById(int categoryId) {
// TODO Auto-generated method stub
return null;
}
}