New Features
Enhanced Features
Revamped Features
O/R Mapping
Tips and Tricks
UML Diagrams
VP Suite

Generate DAO in Java Interface

There are several types of Persistent API available for ORM code generation. The decision to which API should be chosen is depending on the practice of coding. One of the Persistent API is called DAO, which helps client to create, retrieve and persists data to the Persistent Object through static methods. Starting from this release, a new Persistent API is introduced for Java ORM. It is a variation of the DAO, and is called DAO (with Interface).

Selecting DAO (with Interface) as Persistent API

For users who generate code through the Database Code Generation dialog:

  1. Open the Code page.
  2. Select DAO (with Interface) for Persistent API.
    generate_dao_java_interface_1.jpg

For users who generate code through the wizard:

  1. In the Generate Code page, select DAO (with Interface) for Persistent API.
    generate_dao_java_interface_2.jpg

Generated Code

In comparison to the original DAO, DAO (with Interface) features the following:

  1. The DAO class become an interface, and the implementation is moved to the DAOImpl class
  2. Instance methods are used instead of Static methods

With this changes, you can define your own DAO objects and swap between DAO implementations easily.

Consider this class diagram:
generate_dao_java_interface_3.jpg

Persistent API in DAO (with Interface) form is generated from this diagram. Here is the generated code:

SCHOOLDAO.java

import org.orm.*;
import org.hibernate.Query;
import java.util.List;
 
public interface SCHOOLDAO {
	public SCHOOL loadSCHOOLByORMID(int ID) throws PersistentException;
	public SCHOOL loadSCHOOLByORMID(PersistentSession session, int ID) throws PersistentException;
	public SCHOOL[] listSCHOOLByQuery(String condition, String orderBy) throws PersistentException;
	public SCHOOL[] listSCHOOLByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException;
	public SCHOOL loadSCHOOLByQuery(String condition, String orderBy) throws PersistentException;
	public SCHOOL loadSCHOOLByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException;
	public SCHOOL createSCHOOL();
	public boolean save(SCHOOL sCHOOL);
	public boolean delete(SCHOOL sCHOOL);
	public boolean deleteAndDissociate(SCHOOL sCHOOL);
	public boolean refresh(SCHOOL sCHOOL);
	public SCHOOL loadSCHOOLByCriteria(SCHOOLCriteria sCHOOLCriteria);
	public SCHOOL[] listSCHOOLByCriteria(SCHOOLCriteria sCHOOLCriteria);
}


SCHOOLDAOImpl.java

import org.orm.*;
import org.hibernate.Query;
import java.util.List;
 
public class SCHOOLDAOImpl implements SCHOOLDAO {
	public SCHOOL loadSCHOOLByORMID(int ID) throws PersistentException  {
		PersistentSession session = SchoolPersistentManager.instance().getSession();
		return loadSCHOOLByORMID(session, ID);
	}
	
	public SCHOOL loadSCHOOLByORMID(PersistentSession session, int ID) throws PersistentException {
		return (SCHOOL) session.load(SCHOOL.class, new Integer(ID));
	}
	
	public SCHOOL[] listSCHOOLByQuery(String condition, String orderBy) throws PersistentException  {
		PersistentSession session = SchoolPersistentManager.instance().getSession();
		return listSCHOOLByQuery(session, condition, orderBy);
	}
	
	public SCHOOL[] listSCHOOLByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		StringBuffer sb = new StringBuffer("From SCHOOL as SCHOOL");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		Query query = session.createQuery(sb.toString());
		try {
			List list = query.list();
			return (SCHOOL[]) list.toArray(new SCHOOL[list.size()]);
		}
		catch (Exception e) {
			return null;
		}
	}
	
	public SCHOOL loadSCHOOLByQuery(String condition, String orderBy) throws PersistentException {
		PersistentSession session = SchoolPersistentManager.instance().getSession();
		return loadSCHOOLByQuery(session, condition, orderBy);
	}
	
	public SCHOOL loadSCHOOLByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		SCHOOL[] sCHOOLs = listSCHOOLByQuery(session, condition, orderBy);
		if (sCHOOLs != null && sCHOOLs.length > 0)
			return sCHOOLs[0];
		else
			return null;
	}
	
	public static java.util.Iterator iterateSCHOOLByQuery(String condition, String orderBy) throws PersistentException  {
		PersistentSession session = SchoolPersistentManager.instance().getSession();
		return iterateSCHOOLByQuery(session, condition, orderBy);
	}
	
	public static java.util.Iterator iterateSCHOOLByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		StringBuffer sb = new StringBuffer("From SCHOOL as SCHOOL");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		Query query = session.createQuery(sb.toString());
		try {
			return query.iterate();
		}
		catch (Exception e) {
			return null;
		}
	}
	
	public SCHOOL createSCHOOL() {
		return new SCHOOL();
	}
	
	public boolean save(SCHOOL sCHOOL) {
		try {
			SchoolPersistentManager.instance().saveObject(sCHOOL);
			return true;
		}
		catch (Exception e) {
			return false;
		}
	}
	
	public boolean delete(SCHOOL sCHOOL) {
		try {
			SchoolPersistentManager.instance().deleteObject(sCHOOL);
			return true;
		}
		catch (Exception e) {
			return false;
		}
	}
	
	public boolean deleteAndDissociate(SCHOOL sCHOOL) {
		try {
			STUDENT[] lSTUDENTs = sCHOOL.sTUDENT.toArray();
			for(int i = 0; i < lSTUDENTs.length; i++) {
				lSTUDENTs[i].setSCHOOL(null);
			}
			return delete(sCHOOL);
		}
		catch(Exception e) {
			return false;
		}
	}
	
	public boolean refresh(SCHOOL sCHOOL) {
		try {
			SchoolPersistentManager.instance().getSession().refresh(sCHOOL);
			return true;
		}
		catch (Exception e) {
			return false;
		}
	}
	
	public SCHOOL loadSCHOOLByCriteria(SCHOOLCriteria sCHOOLCriteria) {
		SCHOOL[] sCHOOLs = listSCHOOLByCriteria(sCHOOLCriteria);
		if(sCHOOLs == null || sCHOOLs.length == 0) {
			return null;
		}
		return sCHOOLs[0];
	}
	
	public SCHOOL[] listSCHOOLByCriteria(SCHOOLCriteria sCHOOLCriteria) {
		return sCHOOLCriteria.listSCHOOL();
	}
}
 

Downloads

Resources

 
 
Last modified: 2006/10/24 07:25
 
 
Home | Recent Topics | Highlights | UML Diagrams | Tips and Tricks | Object-Relational Mapping
visual-paradigm.com Home | Training Center | UML Center | VP Gallery | Discussion Forum | UML Open Directory