-->
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: design doubt (struts + hibernate)
PostPosted: Sat Aug 06, 2005 7:38 pm 
Beginner
Beginner

Joined: Tue Jun 28, 2005 8:51 pm
Posts: 20
Location: Bergamo, Italy
After reading some books i developed my web application (tomcat) using the following design:

I have mapped my table with hibernate then create DAO classes for accessing to them. When i run an action it calls DAO class that use Hibernate.
Do not look at Exception Handling that is a question i find very hard and i' have not understood well..
That i need to understand is my application is on the right way or if i made big mistake in design.

Tnk you very much for assistance :)
Marco

This is a bare-bone classes of my project (only model & action class):

ACTION CLASS
public class AddAttributeAction extends Action {


public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {

AddAttributeForm addAttributeForm = (AddAttributeForm) form;
AttributeDAO attributeDAO = new AttributeDAO();
if(attributeDAO.addAttribute(addAttributeForm))
return (mapping.findForward("success"));
else
return (mapping.findForward("failure"));
}

}

DAO CLASS
public class AttributeDAO {

public AttributeDAO() {

}

public Session getSession( ) throws PersistenceException {
Session session = null;
try {
session = Persistence.getSession( );
} catch (HibernateException e) {
//log.error("Exception accessing persistence session");
throw new PersistenceException(
"Exception accessing persistence session", e);
}
return session;
}
public boolean addAttribute(AddAttributeForm addAttributeForm) {

boolean save = false;

//setto l' oggetto attributo
Attribute theAttribute = new Attribute();
theAttribute.setName(addAttributeForm.getName());
theAttribute.setType(Short.valueOf(addAttributeForm.getType()));
theAttribute.setColumn(addAttributeForm.getColumn());
theAttribute.setObjectName(addAttributeForm.getMetaobject());

Transaction tx = null;

try {

tx =getSession().beginTransaction();
Query q = getSession().createQuery("from MetaObject m where m.name = :name");
q.setString("name", addAttributeForm.getMetaobject());
List result = q.list();
MetaObject theMetaObject = null;
for (int i = 0; i < result.size(); i++) {
theMetaObject = (MetaObject) result.get(i);
}

//se esiste metaobject aggiungo l' attributo al suo set.
if (theMetaObject != null){
theAttribute.setMetaObject(theMetaObject);
theMetaObject.getAttributes().add(theAttribute);
getSession().save(theMetaObject);
save = true;
}
tx.commit();

}
catch (HibernateException e) {
save = false;
System.out.println("Errore di database: forse hai inserito un nome gia' esistente");
//e.printStackTrace();
if (tx != null) Persistence.rollback(tx);
}
return save;

}
public List getAttributes() {
List result = null;
Transaction tx = null;

try {

tx =getSession().beginTransaction();
Query q = getSession().createQuery("from Attribute");
result = q.list();
tx.commit();
}
catch (HibernateException e) {
System.out.println("Error!!!");
e.printStackTrace();
if (tx != null) Persistence.rollback(tx);
}
return result;
}
}

POJO CLASSES

public class Attribute implements Serializable {

/** identifier field */
private Long id;

/** persistent field */
private String name;

/** persistent field */
private String objectName;

/** persistent field */
private short type;

/** persistent field */
private String column;

/** persistent field */
private MetaObject metaObject;

private Fuzzyattribute fuzzyattribute;

+ getter setter methods

public class MetaObject implements Serializable {

private Long id;

/** identifier field */
private String name;

/** persistent field */
private String from;

/** nullable persistent field */
private String where;

/** persistent field */
private Set attributes = new HashSet();

+ getter setter methods

HIBERNATE MAPPINGS

<hibernate-mapping>

<class name="com.full.hibernate.model.Attribute"
table="ATTRIBUTES">

<!-- Common id property. -->
<id name="id"
type="long"
column="ATTRIBUTE_ID"
unsaved-value="null">
<generator class="native" />
</id>

<property
name="name"
type="java.lang.String"
length="50"
not-null="true" >
<column name="NAME" unique-key="uk_object_name"/>
</property>

<property
name="objectName"
type="java.lang.String"
length="50"
not-null="true" >
<column name="OBJECT" unique-key="uk_object_name"/>
</property>

<property
name="type"
type="short"
column="TYPE"
not-null="true"
length="6" />

<property
name="column"
type="java.lang.String"
column="COLUMN_"
not-null="true"
length="50" />

<!-- Associations -->
<!-- derived association(s) for compound key -->
<!-- bi-directional many-to-one association to Object -->

<many-to-one name="metaObject"
class="com.full.hibernate.model.MetaObject">
<!-- update="false"-->
<!-- insert="false">-->
<column name="OBJECT_REF" />
</many-to-one>

<!--associazione uno a uno presa da manuale Hibernate in Action, con creazione di colonna apposita-->
<many-to-one name="fuzzyattribute"
class="com.full.hibernate.model.Fuzzyattribute"
column="FUZZYATTRIBUTE_REF"
cascade="all"
unique ="true"/>
</class>

<class
name="com.full.hibernate.model.MetaObject"
table="OBJECTS"
>

<!-- Common id property. -->
<id name="id"
type="long"
column="METAOBJECT_ID"
unsaved-value="null"
>
<generator class="native"/>
</id>

<property
name="name"
type="java.lang.String"
column="NAME"
not-null="true"
unique="true"
/>

<property
name="from"
type="java.lang.String"
column="FROMCLAUSOLE"
not-null="true"
length="50"

/>

<property
name="where"
type="java.lang.String"
column="WHERECLAUSOLE"
length="50"

/>

<!-- Associations -->

<!-- bi-directional one-to-many association to Attribute -->
<set
name="attributes"
lazy="true"
inverse="true"
cascade="all"
>
<key>
<column name="OBJECT_REF" />
</key>
<one-to-many
class="com.full.hibernate.model.Attribute"
/>
</set>

</class>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 08, 2005 1:38 am 
Senior
Senior

Joined: Wed Jul 13, 2005 4:31 pm
Posts: 142
Location: Seattle, WA
I usually prefer not to pass the struts form to the DAO class.
That makes the DAO layer dependant on struts form classes for compilation.

So, instead of
public boolean addAttribute(AddAttributeForm addAttributeForm)

I'd use...
public boolean addAttribute(String name, String type...etc.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 08, 2005 10:59 am 
Beginner
Beginner

Joined: Tue Jun 28, 2005 8:51 pm
Posts: 20
Location: Bergamo, Italy
OOh thank you for response, i was certainly nobody respopnse and my credit fly away :))

Howerver your suggestion is very nice and looks better than mine, i will adopt.

For you using boolean for decide if the DAO operations are ok is a good choice or it is better to use excetion with void method?


bye

ps: if anybodyelse has some comments on patern do not hesistate write it about ;)

_________________
Marco

Don't forget to give credit if you get helpful information!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 08, 2005 11:18 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
vanholy wrote:
OOh thank you for response, i was certainly nobody respopnse and my credit fly away :))

Howerver your suggestion is very nice and looks better than mine, i will adopt.

For you using boolean for decide if the DAO operations are ok is a good choice or it is better to use excetion with void method?


bye

ps: if anybodyelse has some comments on patern do not hesistate write it about ;)


I definitely think you're better off using exceptions.

The wiki section of the Hibernate main site has many patters that you should look at. A good pattern to look at for use within web applications is OpenSessionInView - http://www.hibernate.org/43.html.

Read the comments for specific suggestions regarding Struts.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 08, 2005 5:24 pm 
Beginner
Beginner

Joined: Tue Jun 28, 2005 8:51 pm
Posts: 20
Location: Bergamo, Italy
pksiv wrote:


link down?

_________________
Marco

Don't forget to give credit if you get helpful information!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 08, 2005 5:25 pm 
Expert
Expert

Joined: Thu Dec 04, 2003 12:36 pm
Posts: 275
Location: Bielefeld, Germany
vanholy wrote:
pksiv wrote:


link down?



No, just leave out the dot at the end. :)
http://www.hibernate.org/43.html

Best regards
Sven


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.