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>