-->
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.  [ 6 posts ] 
Author Message
 Post subject: Question on using Hibernate with Struts and DAO's
PostPosted: Thu Feb 10, 2005 4:59 pm 
Newbie

Joined: Fri Feb 04, 2005 4:11 pm
Posts: 10
Read the rules before posting!
http://www.hibernate.org/ForumMailingli ... AskForHelp

I am working on a new project and we want to use Hibernate for the Object relational mapping. I have downloaded and tried out some good Hibernate examples. The application is using Struts and Oracle 9i.
It appears that I can externalize the query strings into the hibernate mapping file, and then retrieve them by using the getNamedQuery call.
Is this a good way to do it, or should one use DAO's to hold the queries?
If DAO's are to be used, that will add another layer of classes to be coded. Right now I already have the beans that correspond to the tables, the action forms, the actions, and the hiberante mapping files for each table.
I would like to know if there is any advantage to using DAO's. If I use DAO's, how will the session factory object be availabe to them? Right now I retrieve it in the action form as follows:

sessionfactory =
(SessionFactory)servlet.getServletContext()
.getAttribute(HibernateStrutsPlugIn.KEY_NAME);

session = sessionfactory.openSession();


Thanks.



Hibernate version: 2.1.8

Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.sample.beans.Company" table="MGMT_COMPANIES">

<id name="id" type="long" column="MCP_ID_PK" >
<generator class="native">
<param name="sequence">COMPANYSEQ</param>

</generator>
</id>

<!-- A cat has to have a name, but it shouldn' be too long. -->
<property name="shrtName">
<column name="MCP_NUM_PK" />
</property>
<property name="address">
<column name="MCP_ADDRESS" />
</property>
<property name="city">
<column name="MCP_CITY" />
</property>
</class>
<query name="com.sample.whereMgmtCoEq">
<![CDATA[from com.sample.beans.Company
as company
where
company.shrtName = :shrtName]]>
</query>
</hibernate-mapping>



Code between sessionFactory.openSession() and session.close():
sessionfactory =
(SessionFactory)servlet.getServletContext()
.getAttribute(HibernateStrutsPlugIn.KEY_NAME);

session = sessionfactory.openSession();



Full stack trace of any exception that occurs:

Name and version of the database you are using:Oracle9i

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 11, 2005 5:57 am 
Beginner
Beginner

Joined: Tue Jan 11, 2005 5:50 am
Posts: 43
Location: Zurich (Suisse)
Hello

At my current project (hibernate, struts) I am using hibernate as follows:

- Named queries in the mapping document
- DAO's to call the named queries, set its parameters, set transactions when necessary, and persister methods.
- For the session factory I use the HibernateUtil code as described in the hibernate reference guide.

Each Value Object is used for hibernate, as well as for struts (extends ActionForm).

Sofar I am pretty happy with this design because it keeps it pretty clean, as well as changes in the queries can be done without recompiling.


Regards
Tarik


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 11, 2005 10:32 am 
Newbie

Joined: Fri Feb 04, 2005 4:11 pm
Posts: 10
Thanks Tarik. When you say Value objects, do you mean a stright java class that has the getters and setters instead of a java bean?
Leena


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 11, 2005 10:40 am 
Beginner
Beginner

Joined: Tue Jan 11, 2005 5:50 am
Posts: 43
Location: Zurich (Suisse)
Yes, I mean that even though it still is a bean.

Like that, (CommonForm extends ActionForm):

Code:

package com.mobilelogix.MMSCAdmin.beans;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.builder.ToStringBuilder;

import com.someCompany.MMSCAdmin.struts.common.CommonForm;

public class MMSAbo extends CommonForm implements Serializable {

    /** identifier field */
    private Long mmsaboID;

    /** nullable persistent field */
    private Date senddateStart;

    /** nullable persistent field */
    private Date senddateEnd;

    /** persistent field */
    private long serviceID;

    /** persistent field */
    private long composedID;

    /** transient field */
    private transient Long[] serviceIDs;

    /** transient field */
    private transient Map params;

    private final static SimpleDateFormat FULL_DATE = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss");

    private final static SimpleDateFormat DATE = new SimpleDateFormat(
            "dd.MM.yyyy");

    /** full constructor */
    public MMSAbo(Long mmsaboID, Date senddateStart, Date senddateEnd,
            long serviceID, long composedID) {
        this.mmsaboID = mmsaboID;
        this.senddateStart = senddateStart;
        this.senddateEnd = senddateEnd;
        this.serviceID = serviceID;
        this.composedID = composedID;
    }

    /** default constructor */
    public MMSAbo() {
    }

    /** minimal constructor */
    public MMSAbo(Long mmsaboID, long serviceID, long composedID) {
        this.mmsaboID = mmsaboID;
        this.serviceID = serviceID;
        this.composedID = composedID;
    }

    /**
     * @return Returns the serviceIDs.
     */
    public Long[] getServiceIDs() {
        return this.serviceIDs;
    }

    /**
     * @param serviceIDs
     *            The serviceIDs to set.
     */
    public void setServiceIDs(Long[] serviceIDs) {
        this.serviceIDs = serviceIDs;
    }

    /**
     * @return Returns the params.
     */
    public Map getParams() {
        this.params = new HashMap();
        params.put("mmsaboID", this.mmsaboID);
        params.put("serviceID", new Long(this.serviceID));
        return this.params;
    }

    /**
     * @param params
     *            The params to set.
     */
    public void setParams(Map params) {
        this.params = params;
    }

    public Long getMmsaboID() {
        return this.mmsaboID;
    }

    public void setMmsaboID(Long mmsaboID) {
        this.mmsaboID = mmsaboID;
    }

    public Date getSenddateStart() {
        return this.senddateStart;
    }

    public void setSenddateStart(Date senddateStart) {
        this.senddateStart = senddateStart;
    }

    public String getSenddateStartAsString() {
        if (this.senddateStart != null)
            return FULL_DATE.format(this.senddateStart);
        else
            return "";
    }

    public void setSenddateStartAsString(String senddateStart) {
        if(!senddateStart.equals(""))
        try {
            this.senddateStart = new Date(FULL_DATE.parse(senddateStart)
                    .getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    public Date getSenddateEnd() {
        return this.senddateEnd;
    }

    public void setSenddateEnd(Date senddateEnd) {
        this.senddateEnd = senddateEnd;
    }

    public String getSenddateEndAsString() {
        if (this.senddateEnd != null)
            return FULL_DATE.format(this.senddateEnd);
        else
            return "";
    }

    public void setSenddateEndAsString(String senddateEnd) {
        if(!senddateEnd.equals(""))
        try {
            this.senddateEnd = new Date(FULL_DATE.parse(senddateEnd).getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    /**
     * @return Returns the endDate.
     */
    public String getEndDateAsString() {
        if (this.senddateEnd != null)
            return DATE.format(this.senddateEnd);
        else
            return "";
    }

    /**
     * @return Returns the startDate.
     */
    public String getStartDateAsString() {
        if (this.senddateEnd != null)
            return DATE.format(this.senddateStart);
        else
            return "";
    }

    /**
     * @param endDate
     *            The endDate to set.
     */
    public void setEndDateAsString(Date endDate) {

    }

    /**
     * @param startDate
     *            The startDate to set.
     */
    public void setStartDateAsString(Date startDate) {

    }

    /**
     * @return
     */
    public long getServiceID() {
        return this.serviceID;
    }

    public void setServiceID(long serviceID) {
        this.serviceID = serviceID;
    }

    public long getComposedID() {
        return this.composedID;
    }

    public void setComposedID(long composedID) {
        this.composedID = composedID;
    }

    public String toString() {
        return new ToStringBuilder(this).append("mmsaboID : ", getMmsaboID())
                .append("Starts Date object : ", senddateStart).append("Ends Full Date object : ", senddateEnd)
                .append("Starts short Date : ", getStartDateAsString()).append("Ends short Date : ", getEndDateAsString())
                .append("Starts long Date : ", getSenddateStartAsString()).append("Ends long Date : ", getSenddateEndAsString())
                .toString();
    }

}



Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 11, 2005 10:44 am 
Newbie

Joined: Fri Feb 04, 2005 4:11 pm
Posts: 10
Thanks a lot for your help.....


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 11, 2005 10:54 am 
Beginner
Beginner

Joined: Tue Jan 11, 2005 5:50 am
Posts: 43
Location: Zurich (Suisse)
You are welcome.

Tarik


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.