-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Initial SessionFactory creation failed.java.lang.NoSuchMetho
PostPosted: Mon Aug 22, 2016 3:57 pm 
Newbie

Joined: Mon Aug 22, 2016 3:50 pm
Posts: 1
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;
   }

}




Top
 Profile  
 
 Post subject: Re: Initial SessionFactory creation failed.java.lang.NoSuchMetho
PostPosted: Mon Aug 29, 2016 5:40 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1630
Location: Romania
Can you post the stack trace?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.