package orm;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;
import javax.persistence.Query;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.jpa.JpaCallback;
import org.springframework.orm.jpa.support.JpaDaoSupport;
/**
* A data access object (DAO) providing persistence and search support for
* Userinfo entities. Transaction control of the save(), update() and delete()
* operations can directly support Spring container-managed transactions or they
* can be augmented to handle user-managed Spring transactions. Each of these
* methods provides additional information for how to configure it for the
* desired type of transaction control.
*
* @see orm.Userinfo
* @author MyEclipse Persistence Tools
*/
public class UserinfoDAO extends JpaDaoSupport {
// property constants
public static final String USERNAME = "username";
public static final String PASSWORD = "password";
public static final String AGE = "age";
/**
* Perform an initial save of a previously unsaved Userinfo entity. All
* subsequent persist actions of this entity should use the #update()
* method. This operation must be performed within the a database
* transaction context for the entity's data to be permanently saved to the
* persistence store, i.e., database. This method uses the
* {@link javax.persistence.EntityManager#persist(Object)
* EntityManager#persist} operation.
* <p>
* User-managed Spring transaction example:
*
* <pre>
* TransactionStatus txn = txManager
* .getTransaction(new DefaultTransactionDefinition());
* UserinfoDAO.save(entity);
* txManager.commit(txn);
* </pre>
*
* @see <a href =
* "http://www.myeclipseide.com/documentation/quickstarts/jpaspring#containermanaged">Spring
* container-managed transaction examples</a>
* @param entity
* Userinfo entity to persist
* @throws RuntimeException
* when the operation fails
*/
public void save(Userinfo entity) {
logger.info("saving Userinfo instance");
try {
getJpaTemplate().persist(entity);
logger.info("save successful");
} catch (RuntimeException re) {
logger.error("save failed", re);
throw re;
}
}
/**
* Delete a persistent Userinfo entity. This operation must be performed
* within the a database transaction context for the entity's data to be
* permanently deleted from the persistence store, i.e., database. This
* method uses the {@link javax.persistence.EntityManager#remove(Object)
* EntityManager#delete} operation.
* <p>
* User-managed Spring transaction example:
*
* <pre>
* TransactionStatus txn = txManager
* .getTransaction(new DefaultTransactionDefinition());
* UserinfoDAO.delete(entity);
* txManager.commit(txn);
* entity = null;
* </pre>
*
* @see <a href =
* "http://www.myeclipseide.com/documentation/quickstarts/jpaspring#containermanaged">Spring
* container-managed transaction examples</a>
* @param entity
* Userinfo entity to delete
* @throws RuntimeException
* when the operation fails
*/
public void delete(Userinfo entity) {
logger.info("deleting Userinfo instance");
try {
entity = getJpaTemplate().getReference(Userinfo.class,
entity.getId());
getJpaTemplate().remove(entity);
logger.info("delete successful");
} catch (RuntimeException re) {
logger.error("delete failed", re);
throw re;
}
}
/**
* Persist a previously saved Userinfo entity and return it or a copy of it
* to the sender. A copy of the Userinfo entity parameter is returned when
* the JPA persistence mechanism has not previously been tracking the
* updated entity. This operation must be performed within the a database
* transaction context for the entity's data to be permanently saved to the
* persistence store, i.e., database. This method uses the
* {@link javax.persistence.EntityManager#merge(Object) EntityManager#merge}
* operation.
* <p>
* User-managed Spring transaction example:
*
* <pre>
* TransactionStatus txn = txManager
* .getTransaction(new DefaultTransactionDefinition());
* entity = UserinfoDAO.update(entity);
* txManager.commit(txn);
* </pre>
*
* @see <a href =
* "http://www.myeclipseide.com/documentation/quickstarts/jpaspring#containermanaged">Spring
* container-managed transaction examples</a>
* @param entity
* Userinfo entity to update
* @return Userinfo the persisted Userinfo entity instance, may not be the
* same
* @throws RuntimeException
* if the operation fails
*/
public Userinfo update(Userinfo entity) {
logger.info("updating Userinfo instance");
try {
Userinfo result = getJpaTemplate().merge(entity);
logger.info("update successful");
return result;
} catch (RuntimeException re) {
logger.error("update failed", re);
throw re;
}
}
public Userinfo findById(Long id) {
logger.info("finding Userinfo instance with id: " + id);
try {
Userinfo instance = getJpaTemplate().find(Userinfo.class, id);
return instance;
} catch (RuntimeException re) {
logger.error("find failed", re);
throw re;
}
}
/**
* Find all Userinfo entities with a specific property value.
*
* @param propertyName
* the name of the Userinfo property to query
* @param value
* the property value to match
* @param rowStartIdxAndCount
* Optional int varargs. rowStartIdxAndCount[0] specifies the the
* row index in the query result-set to begin collecting the
* results. rowStartIdxAndCount[1] specifies the the maximum
* number of results to return.
* @return List<Userinfo> found by query
*/
@SuppressWarnings("unchecked")
public List<Userinfo> findByProperty(String propertyName,
final Object value, final int... rowStartIdxAndCount) {
logger.info("finding Userinfo instance with property: " + propertyName
+ ", value: " + value);
try {
final String queryString = "select model from Userinfo model where model."
+ propertyName + "= :propertyValue";
return getJpaTemplate().executeFind(new JpaCallback() {
public Object doInJpa(EntityManager em)
throws PersistenceException {
Query query = em.createQuery(queryString);
query.setParameter("propertyValue", value);
if (rowStartIdxAndCount != null
&& rowStartIdxAndCount.length > 0) {
int rowStartIdx = Math.max(0, rowStartIdxAndCount[0]);
if (rowStartIdx > 0) {
query.setFirstResult(rowStartIdx);
}
if (rowStartIdxAndCount.length > 1) {
int rowCount = Math.max(0, rowStartIdxAndCount[1]);
if (rowCount > 0) {
query.setMaxResults(rowCount);
}
}
}
return query.getResultList();
}
});
} catch (RuntimeException re) {
logger.error("find by property name failed", re);
throw re;
}
}
public List<Userinfo> findByUsername(Object username,
int... rowStartIdxAndCount) {
return findByProperty(USERNAME, username, rowStartIdxAndCount);
}
public List<Userinfo> findByPassword(Object password,
int... rowStartIdxAndCount) {
return findByProperty(PASSWORD, password, rowStartIdxAndCount);
}
public List<Userinfo> findByAge(Object age, int... rowStartIdxAndCount) {
return findByProperty(AGE, age, rowStartIdxAndCount);
}
/**
* Find all Userinfo entities.
*
* @param rowStartIdxAndCount
* Optional int varargs. rowStartIdxAndCount[0] specifies the the
* row index in the query result-set to begin collecting the
* results. rowStartIdxAndCount[1] specifies the the maximum
* count of results to return.
* @return List<Userinfo> all Userinfo entities
*/
@SuppressWarnings("unchecked")
public List<Userinfo> findAll(final int... rowStartIdxAndCount) {
logger.info("finding all Userinfo