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:
Open the Code page.
Select
DAO (with Interface) for
Persistent API.
For users who generate code through the wizard:
In the Generate Code page, select
DAO (with Interface) for
Persistent API.

Generated Code
In comparison to the original DAO, DAO (with Interface) features the following:
The DAO class become an interface, and the implementation is moved to the DAOImpl class
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:

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
VP-UML Project file
-
Persistent
API for DAO (with Interface)
Resources