AnimalDaoBase.java
// license-header java merge-point
//
// Attention: Generated code! Do not modify by hand!
// Generated by DaoBase.vsl in andromda-ejb3-cartridge on 08/08/2014 12:21:06.
//
package org.andromda.demo.ejb3.animal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.annotation.Resource;
import javax.ejb.Local;
import javax.ejb.SessionContext;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import org.hibernate.Session;
/**
* <p>
* Base EJB3 DAO Class: is able to create, update, remove, load, and find
* objects of type <code>Animal</code>.
* </p>
*
* @see AnimalDao
*/
@TransactionAttribute(TransactionAttributeType.REQUIRED)
@Local({AnimalDao.class})
public abstract class AnimalDaoBase
implements AnimalDao
{
/** Session Context Injection */
@Resource
protected SessionContext context;
/**
* Inject persistence context demo-ejb3
*/
@PersistenceContext(unitName = "demo-ejb3")
protected EntityManager emanager;
/**
* Inject Hibernate Session
*/
@PersistenceContext(unitName = "demo-ejb3")
protected Session hibernateSession;
/**
* @see AnimalDao#load
*/
@Override
public Object load(final int transform, final String name)
throws AnimalDaoException
{
if (name == null)
{
throw new IllegalArgumentException(
"Animal.load - 'name' can not be null");
}
try
{
final Animal entity = this.emanager.find(Animal.class, name);
return transformEntity(transform, entity);
}
catch (Exception ex)
{
throw new AnimalDaoException(ex);
}
}
/**
* @see AnimalDao#load( String)
*/
@Override
public Animal load( final String name)
throws AnimalDaoException
{
return (Animal)this.load(TRANSFORM_NONE, name);
}
/**
* @see AnimalDao#loadAll()
*/
@Override
@SuppressWarnings({"unchecked"})
public Collection<Animal> loadAll()
throws AnimalDaoException
{
return this.loadAll(TRANSFORM_NONE);
}
/**
* @see AnimalDao#loadAll(int)
*/
@Override
public Collection loadAll(final int transform)
throws AnimalDaoException
{
try
{
TypedQuery<Animal> query = this.emanager.createNamedQuery("Animal.findAll", Animal.class);
List<Animal> results = query.getResultList();
this.transformEntities(transform, results);
return results;
}
catch (Exception ex)
{
throw new AnimalDaoException(ex);
}
}
/**
* Create Animal with no VO transformation
* @see AnimalDao#create(Animal)
*/
@Override
public Animal create(Animal animal)
throws AnimalDaoException
{
return (Animal)this.create(TRANSFORM_NONE, animal);
}
/**
* Create Animal with VO transformation
* @see AnimalDao#create(int, Animal)
*/
@Override
public Object create(final int transform, final Animal animal)
throws AnimalDaoException
{
if (animal == null)
{
throw new IllegalArgumentException(
"Animal.create - 'animal' can not be null");
}
try
{
this.emanager.persist(animal);
this.emanager.flush();
return this.transformEntity(transform, animal);
}
catch (Exception ex)
{
throw new AnimalDaoException(ex);
}
}
/**
* Create a Collection of Animal with no VO transformation
* @see AnimalDao#create(Collection)
*/
@Override
@SuppressWarnings({"unchecked"})
public Collection<Animal> create(final Collection<Animal> entities)
throws AnimalDaoException
{
return create(TRANSFORM_NONE, entities);
}
/**
* Create a Collection of Animal with VO transformation
* @see AnimalDao#create(int, Collection)
*/
@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public Collection create(final int transform, final Collection<Animal> entities)
throws AnimalDaoException
{
if (entities == null)
{
throw new IllegalArgumentException(
"Animal.create - 'entities' can not be null");
}
Collection results = new ArrayList();
try
{
for (final Animal entity : entities)
{
results.add(create(transform, entity));
}
}
catch (Exception ex)
{
throw new AnimalDaoException(ex);
}
return results;
}
/**
* Create Entity Animal using instance attributes with no VO transformation
* @see AnimalDao#create(Collection<String>, boolean)
*/
@Override
public Animal create(
Collection<String> types,
boolean carnivor)
throws AnimalDaoException
{
return (Animal)this.create(TRANSFORM_NONE, types, carnivor);
}
/**
* Create Entity Animal using instance attributes with VO transformation
* @see AnimalDao#create(int, Collection<String>, boolean)
* composite=false identifiers=1
*/
@Override
public Object create(
final int transform,
Collection<String> types,
boolean carnivor)
throws AnimalDaoException
{
Animal entity = new Animal();
entity.setTypes(types);
entity.setCarnivor(carnivor);
return this.create(transform, entity);
}
/**
* @see AnimalDao#update(Animal)
*/
@Override
public void update(Animal animal)
throws AnimalDaoException
{
if (animal == null)
{
throw new IllegalArgumentException(
"Animal.update - 'animal' can not be null");
}
try
{
this.emanager.merge(animal);
this.emanager.flush();
}
catch (Exception ex)
{
throw new AnimalDaoException(ex);
}
}
/**
* @see AnimalDao#update(Collection)
*/
@Override
public void update(final Collection<Animal> entities)
throws AnimalDaoException
{
if (entities == null)
{
throw new IllegalArgumentException(
"Animal.update - 'entities' can not be null");
}
try
{
for (final Animal entity : entities)
{
update(entity);
}
}
catch (Exception ex)
{
throw new AnimalDaoException(ex);
}
}
/**
* @see AnimalDao#remove(Animal)
*/
@Override
public void remove(Animal animal)
throws AnimalDaoException
{
if (animal == null)
{
throw new IllegalArgumentException(
"Animal.remove - 'animal' can not be null");
}
try
{
this.emanager.remove(animal);
this.emanager.flush();
}
catch (Exception ex)
{
throw new AnimalDaoException(ex);
}
}
/**
* @see AnimalDao#remove(String)
*/
@Override
public void remove(String name)
throws AnimalDaoException
{
if (name == null)
{
throw new IllegalArgumentException(
"Animal.remove - 'name' can not be null");
}
try
{
final Animal entity = this.load(name);
if (entity != null)
{
this.remove(entity);
}
}
catch (Exception ex)
{
throw new AnimalDaoException(ex);
}
}
/**
* @see AnimalDao#remove(Collection)
*/
@Override
public void remove(Collection<Animal> entities)
throws AnimalDaoException
{
if (entities == null)
{
throw new IllegalArgumentException(
"Animal.remove - 'entities' can not be null");
}
try
{
for (final Animal entity : entities)
{
remove(entity);
}
}
catch (Exception ex)
{
throw new AnimalDaoException(ex);
}
}
/**
* @see AnimalDao#findByType(String)
*/
@Override
public Collection findByType(String type)
throws AnimalDaoException
{
return this.findByType(TRANSFORM_NONE, type);
}
/**
* @see AnimalDao#findByType(String, String)
*/
@Override
public Collection findByType(final String queryString, final String type)
throws AnimalDaoException
{
return this.findByType(TRANSFORM_NONE, queryString, type);
}
/**
* @see AnimalDao#findByType(int, String)
*/
@Override
public List findByType(final int transform, final String type)
throws AnimalDaoException
{
try
{
TypedQuery<Animal> queryObject = this.emanager.createNamedQuery("Animal.findByType", Animal.class);
queryObject.setParameter("type", type);
List results = queryObject.getResultList();
transformEntities(transform, results);
return results;
}
catch (Exception ex)
{
throw new AnimalDaoException(ex);
}
}
/**
* @see AnimalDao#findByType(int, String, String)
*/
@Override
public List findByType(final int transform, final String queryString, final String type)
throws AnimalDaoException
{
try
{
TypedQuery<Animal> queryObject = this.emanager.createQuery(queryString, Animal.class);
queryObject.setParameter("type", type);
List results = queryObject.getResultList();
transformEntities(transform, results);
return results;
}
catch (Exception ex)
{
throw new AnimalDaoException(ex);
}
}
/**
* @see AnimalDao#deleteByCarnivor(boolean)
*/
@Override
public void deleteByCarnivor(boolean carnivor)
throws AnimalDaoException
{
this.deleteByCarnivor(TRANSFORM_NONE, carnivor);
}
/**
* @see AnimalDao#deleteByCarnivor(String, boolean)
*/
@Override
public void deleteByCarnivor(final String queryString, final boolean carnivor)
throws AnimalDaoException
{
this.deleteByCarnivor(TRANSFORM_NONE, queryString, carnivor);
}
/**
* @see AnimalDao#deleteByCarnivor(int, boolean)
*/
@Override
public void deleteByCarnivor(final int transform, final boolean carnivor)
throws AnimalDaoException
{
try
{
TypedQuery<Animal> queryObject = this.emanager.createNamedQuery("Animal.deleteByCarnivor", Animal.class);
queryObject.setParameter("carnivor", new Boolean(carnivor));
queryObject.executeUpdate();
}
catch (Exception ex)
{
throw new AnimalDaoException(ex);
}
}
/**
* @see AnimalDao#deleteByCarnivor(int, String, boolean)
*/
@Override
public void deleteByCarnivor(final int transform, final String queryString, final boolean carnivor)
throws AnimalDaoException
{
try
{
TypedQuery<Animal> queryObject = this.emanager.createQuery(queryString, Animal.class);
queryObject.setParameter("carnivor", new Boolean(carnivor));
queryObject.executeUpdate();
}
catch (Exception ex)
{
throw new AnimalDaoException(ex);
}
}
/**
* Allows transformation of entities into value objects
* (or something else for that matter), when the <code>transform</code>
* flag is set to one of the constants defined in <code>AnimalDao</code>, please note
* that the {@link #TRANSFORM_NONE} constant denotes no transformation, so the entity itself
* will be returned.
*
* If the integer argument value is unknown {@link #TRANSFORM_NONE} is assumed.
*
* @param transform one of the constants declared in {@link AnimalDao}
* @param entity an entity that was found
* @return the transformed entity (i.e. new value object, etc)
* @see #transformEntities(int,Collection)
*/
protected Object transformEntity(final int transform, final Animal entity)
{
Object target = null;
if (entity != null)
{
switch (transform)
{
case TRANSFORM_NONE : // fall-through
default:
target = entity;
}
}
return target;
}
/**
* Transforms a collection of entities using the
* {@link #transformEntity(int, Animal)}
* method. This method does not instantiate a new collection.
* <p/>
* Transforms into the same collection as the argument, but this time containing the transformed entities
* This method is to be used internally only.
*
* @param transform one of the constants declared in <code>AnimalDao</code>
* @param entities the collection of entities to transform
* @see #transformEntity(int, Animal)
*/
protected void transformEntities(final int transform, final Collection<Animal> entities)
{
switch (transform)
{
case TRANSFORM_NONE : // fall-through
default:
// do nothing;
}
}
// For unit testing outside of container - persistence context not injected
/**
* @return the context
*/
public SessionContext getContext()
{
return this.context;
}
/**
* @param contextIn the context to set
*/
public void setContext(SessionContext contextIn)
{
this.context = contextIn;
}
/**
* @return the emanager
*/
public EntityManager getEmanager()
{
return this.emanager;
}
/**
* @param emanagerIn the emanager to set
*/
public void setEmanager(EntityManager emanagerIn)
{
this.emanager = emanagerIn;
}
/**
* @return the hibernateSession
*/
public Session getHibernateSession()
{
return this.hibernateSession;
}
/**
* @param hibernateSessionIn the hibernateSession to set
*/
public void setHibernateSession(Session hibernateSessionIn)
{
this.hibernateSession = hibernateSessionIn;
}
}