Note that there are some explanatory texts on larger screens.

plurals
  1. POBest approach for integrating Struts 2 and hibernate
    primarykey
    data
    text
    <p>I was going to integrate hibernate and struts2. Please advise which is the best approach to that, I was thinking that in Struts2, there are no official plugins to integrate the Hibernate framework. But, you can workaround with the following steps:</p> <ol> <li>Register a custom ServletContextListener.</li> <li>In the ServletContextListener class, initialize the Hibernate session and store it into the servlet context.</li> <li>In action class, get the Hibernate session from the servlet context, and perform the Hibernate task as normal.</li> </ol> <p>Please advise that my approach of servlet context for initalizing hibernate session facctory is ok or there can be othe best approch also. Here is the snapshot of the project.</p> <p>Here is the piece of code..</p> <p>The model class...</p> <pre><code>package com.mkyong.customer.model; import java.util.Date; public class Customer implements java.io.Serializable { private Long customerId; private String name; private String address; private Date createdDate; //getter and setter methods } </code></pre> <p>the hbm mapping file ..</p> <pre><code>&lt;hibernate-mapping&gt; &lt;class name="com.mkyong.customer.model.Customer" table="customer" catalog="mkyong"&gt; &lt;id name="customerId" type="java.lang.Long"&gt; &lt;column name="CUSTOMER_ID" /&gt; &lt;generator class="identity" /&gt; &lt;/id&gt; &lt;property name="name" type="string"&gt; &lt;column name="NAME" length="45" not-null="true" /&gt; &lt;/property&gt; &lt;property name="address" type="string"&gt; &lt;column name="ADDRESS" not-null="true" /&gt; &lt;/property&gt; &lt;property name="createdDate" type="timestamp"&gt; &lt;column name="CREATED_DATE" length="19" not-null="true" /&gt; &lt;/property&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p>The configuration file is...</p> <pre><code>&lt;hibernate-configuration&gt; &lt;session-factory&gt; &lt;property name="hibernate.bytecode.use_reflection_optimizer"&gt;false&lt;/property&gt; &lt;property name="hibernate.connection.password"&gt;password&lt;/property&gt; &lt;property name="hibernate.connection.url"&gt;jdbc:mysql://localhost:3306/mkyong&lt;/property&gt; &lt;property name="hibernate.connection.username"&gt;root&lt;/property&gt; &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt; &lt;property name="show_sql"&gt;true&lt;/property&gt; &lt;property name="format_sql"&gt;true&lt;/property&gt; &lt;property name="use_sql_comments"&gt;false&lt;/property&gt; &lt;mapping resource="com/mkyong/customer/hibernate/Customer.hbm.xml" /&gt; &lt;/session-factory&gt; &lt;/hibernate-configuration&gt; </code></pre> <p>The listener class...</p> <pre><code>package com.mkyong.listener; import java.net.URL; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateListener implements ServletContextListener{ private Configuration config; private SessionFactory factory; private String path = "/hibernate.cfg.xml"; private static Class clazz = HibernateListener.class; public static final String KEY_NAME = clazz.getName(); public void contextDestroyed(ServletContextEvent event) { // } public void contextInitialized(ServletContextEvent event) { try { URL url = HibernateListener.class.getResource(path); config = new Configuration().configure(url); factory = config.buildSessionFactory(); //save the Hibernate session factory into serlvet context event.getServletContext().setAttribute(KEY_NAME, factory); } catch (Exception e) { System.out.println(e.getMessage()); } } } </code></pre> <p>finally the action class..</p> <pre><code>ackage com.mkyong.customer.action; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.apache.struts2.ServletActionContext; import org.hibernate.Session; import org.hibernate.SessionFactory; import com.mkyong.customer.model.Customer; import com.mkyong.listener.HibernateListener; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; public class CustomerAction extends ActionSupport implements ModelDriven{ Customer customer = new Customer(); List&lt;Customer&gt; customerList = new ArrayList&lt;Customer&gt;(); public String execute() throws Exception { return SUCCESS; } public Object getModel() { return customer; } public List&lt;Customer&gt; getCustomerList() { return customerList; } public void setCustomerList(List&lt;Customer&gt; customerList) { this.customerList = customerList; } //save customer public String addCustomer() throws Exception{ //get hibernate session from the servlet context SessionFactory sessionFactory = (SessionFactory) ServletActionContext.getServletContext() .getAttribute(HibernateListener.KEY_NAME); Session session = sessionFactory.openSession(); //save it customer.setCreatedDate(new Date()); session.beginTransaction(); session.save(customer); session.getTransaction().commit(); //reload the customer list customerList = null; customerList = session.createQuery("from Customer").list(); return SUCCESS; } //list all customers public String listCustomer() throws Exception{ //get hibernate session from the servlet context SessionFactory sessionFactory = (SessionFactory) ServletActionContext.getServletContext() .getAttribute(HibernateListener.KEY_NAME); Session session = sessionFactory.openSession(); customerList = session.createQuery("from Customer").list(); return SUCCESS; } } </code></pre> <p>Guys please post the updated code Thanks a lot, I am stuck up on this..!!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload