// license-header java merge-point // // Attention: Generated code! Do not modify by hand! // Generated by DaoBase.vsl in andromda-ejb3-cartridge on 09/18/2014 16:56:10. // package org.andromda.test.howto9.b; 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; /** *

* Base EJB3 DAO Class: is able to create, update, remove, load, and find * objects of type Vehicle. *

* * @see VehicleDao */ @TransactionAttribute(TransactionAttributeType.REQUIRED) @Local({VehicleDao.class}) public abstract class VehicleDaoBase implements VehicleDao { /** Session Context Injection */ @Resource protected SessionContext context; /** * Inject persistence context howtomodel */ @PersistenceContext(unitName = "howtomodel") protected EntityManager emanager; /** * @see VehicleDao#load */ @Override public Object load(final int transform, final Long id) throws VehicleDaoException { if (id == null) { throw new IllegalArgumentException( "Vehicle.load - 'id' can not be null"); } try { final Vehicle entity = this.emanager.find(Vehicle.class, id); return transformEntity(transform, entity); } catch (Exception ex) { throw new VehicleDaoException(ex); } } /** * @see VehicleDao#load( Long) */ @Override public Vehicle load( final Long id) throws VehicleDaoException { return (Vehicle)this.load(TRANSFORM_NONE, id); } /** * @see VehicleDao#loadAll() */ @Override @SuppressWarnings({"unchecked"}) public Collection loadAll() throws VehicleDaoException { return this.loadAll(TRANSFORM_NONE); } /** * @see VehicleDao#loadAll(int) */ @Override public Collection loadAll(final int transform) throws VehicleDaoException { try { TypedQuery query = this.emanager.createNamedQuery("Vehicle.findAll", Vehicle.class); List results = query.getResultList(); this.transformEntities(transform, results); return results; } catch (Exception ex) { throw new VehicleDaoException(ex); } } /** * Create Vehicle with no VO transformation * @see VehicleDao#create(Vehicle) */ @Override public Vehicle create(Vehicle vehicle) throws VehicleDaoException { return (Vehicle)this.create(TRANSFORM_NONE, vehicle); } /** * Create Vehicle with VO transformation * @see VehicleDao#create(int, Vehicle) */ @Override public Object create(final int transform, final Vehicle vehicle) throws VehicleDaoException { if (vehicle == null) { throw new IllegalArgumentException( "Vehicle.create - 'vehicle' can not be null"); } try { this.emanager.persist(vehicle); this.emanager.flush(); return this.transformEntity(transform, vehicle); } catch (Exception ex) { throw new VehicleDaoException(ex); } } /** * Create a Collection of Vehicle with no VO transformation * @see VehicleDao#create(Collection) */ @Override @SuppressWarnings({"unchecked"}) public Collection create(final Collection entities) throws VehicleDaoException { return create(TRANSFORM_NONE, entities); } /** * Create a Collection of Vehicle with VO transformation * @see VehicleDao#create(int, Collection) */ @Override @SuppressWarnings({"unchecked", "rawtypes"}) public Collection create(final int transform, final Collection entities) throws VehicleDaoException { if (entities == null) { throw new IllegalArgumentException( "Vehicle.create - 'entities' can not be null"); } Collection results = new ArrayList(); try { for (final Vehicle entity : entities) { results.add(create(transform, entity)); } } catch (Exception ex) { throw new VehicleDaoException(ex); } return results; } /** * Create Entity Vehicle using instance attributes with no VO transformation * @see VehicleDao#create(String, String, short) */ @Override public Vehicle create( String make, String model, short age) throws VehicleDaoException { return (Vehicle)this.create(TRANSFORM_NONE, make, model, age); } /** * Create Entity Vehicle using instance attributes with VO transformation * @see VehicleDao#create(int, String, String, short) * composite=false identifiers=1 */ @Override public Object create( final int transform, String make, String model, short age) throws VehicleDaoException { Vehicle entity = new Vehicle(); entity.setMake(make); entity.setModel(model); entity.setAge(age); return this.create(transform, entity); } /** * @see VehicleDao#update(Vehicle) */ @Override public void update(Vehicle vehicle) throws VehicleDaoException { if (vehicle == null) { throw new IllegalArgumentException( "Vehicle.update - 'vehicle' can not be null"); } try { this.emanager.merge(vehicle); this.emanager.flush(); } catch (Exception ex) { throw new VehicleDaoException(ex); } } /** * @see VehicleDao#update(Collection) */ @Override public void update(final Collection entities) throws VehicleDaoException { if (entities == null) { throw new IllegalArgumentException( "Vehicle.update - 'entities' can not be null"); } try { for (final Vehicle entity : entities) { update(entity); } } catch (Exception ex) { throw new VehicleDaoException(ex); } } /** * @see VehicleDao#remove(Vehicle) */ @Override public void remove(Vehicle vehicle) throws VehicleDaoException { if (vehicle == null) { throw new IllegalArgumentException( "Vehicle.remove - 'vehicle' can not be null"); } try { this.emanager.remove(vehicle); this.emanager.flush(); } catch (Exception ex) { throw new VehicleDaoException(ex); } } /** * @see VehicleDao#remove(Long) */ @Override public void remove(Long id) throws VehicleDaoException { if (id == null) { throw new IllegalArgumentException( "Vehicle.remove - 'id' can not be null"); } try { final Vehicle entity = this.load(id); if (entity != null) { this.remove(entity); } } catch (Exception ex) { throw new VehicleDaoException(ex); } } /** * @see VehicleDao#remove(Collection) */ @Override public void remove(Collection entities) throws VehicleDaoException { if (entities == null) { throw new IllegalArgumentException( "Vehicle.remove - 'entities' can not be null"); } try { for (final Vehicle entity : entities) { remove(entity); } } catch (Exception ex) { throw new VehicleDaoException(ex); } } /** * Allows transformation of entities into value objects * (or something else for that matter), when the transform * flag is set to one of the constants defined in VehicleDao, 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 VehicleDao} * @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 Vehicle 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, Vehicle)} * method. This method does not instantiate a new collection. *

* 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 VehicleDao * @param entities the collection of entities to transform * @see #transformEntity(int, Vehicle) */ protected void transformEntities(final int transform, final Collection 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; } }