Step 1: define the pojo and NamedQueries, e.g.
@Entity
@NamedQueries({
@NamedQuery(name = "findEmployeeByEmail", query = "select myEmployee from Employee myEmployee where myEmployee.email = ?1"),
….
@NamedQuery(name = "updateEmployeeByCity",
query = "UPDATE Employee e " +
"SET e.address.city = :newCity " +
"WHERE e.address.city = :oldCity")
@Table(schema = "CLASSICCARS", name = "EMPLOYEE")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace = "MySpring2/com/tantalus/domain", name = "Employee")
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
…..}
Step 2: use it in the DAO implement, e.g.
/**
* DAO to manage Employee entities.
*
*/
@Repository("EmployeeDAO")
@Transactional
public class EmployeeDAOImpl extends AbstractJpaDao<Employee> implements
EmployeeDAO {
….
@SuppressWarnings("unchecked")
@Transactional
public Set<Employee> findEmployeeByEmail(String email) throws DataAccessException {
Query query = getEntityManager().createNamedQuery("findEmployeeByEmail", email);
return new LinkedHashSet<Employee>(query.getResultList());
}
@SuppressWarnings("unchecked")
@Transactional
public void updateEmployeeByCity(String newCity,String oldCity) throws DataAccessException {
Query query = entityManager.createNamedQuery("updateCustomersByCity");
query.setParameter("oldCity", " oldCity ");
query.setParameter("newCity", " newCity ");
query.executeUpdate();
}
….
}
Comments
Post a Comment