package jpa;
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 User
* 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 jpa.User
* @author MyEclipse Persistence Tools
*/
public class UserDAO extends JpaDaoSupport implements IUserDAO {
// property constants
public static final String NAME = "name";
public static final String PASSWORD = "password";
public static final String EMAIL = "email";
/**
* Perform an initial save of a previously unsaved User 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());
* UserDAO.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
* User entity to persist
* @throws RuntimeException
* when the operation fails
*/
public void save(User entity) {
logger.info("saving User instance");
try {
getJpaTemplate().persist(entity);
logger.info("save successful");
} catch (RuntimeException re) {
logger.error("save failed", re);
throw re;
}
}
/**
* Delete a persistent User 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());
* UserDAO.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
* User entity to delete
* @throws RuntimeException
* when the operation fails
*/
public void delete(User entity) {
logger.info("deleting User instance");
try {
entity = getJpaTemplate().getReference(User.class, entity.getId());
getJpaTemplate().remove(entity);
logger.info("delete successful");
} catch (RuntimeException re) {
logger.error("delete failed", re);
throw re;
}
}
/**
* Persist a previously saved User entity and return it or a copy of it to
* the sender. A copy of the User 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 = UserDAO.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
* User entity to update
* @returns User the persisted User entity instance, may not be the same
* @throws RuntimeException
* if the operation fails
*/
public User update(User entity) {
logger.info("updating User instance");
try {
User result = getJpaTemplate().merge(entity);
logger.info("update successful");
return result;
} catch (RuntimeException re) {
logger.error("update failed", re);
throw re;
}
}
public User findById(Integer id) {
logger.info("finding User instance with id: " + id);
try {
User instance = getJpaTemplate().find(User.class, id);
return instance;
} catch (RuntimeException re) {
logger.error("find failed", re);
throw re;
}
}
/**
* Find all User entities with a specific property value.
*
* @param propertyName
* the name of the User property to query
* @param value
* the property value to match
* @return List<User> found by query
*/
@SuppressWarnings("unchecked")
public List<User> findByProperty(String propertyName, final Object value) {
logger.info("finding User instance with property: " + propertyName
+ ", value: " + value);
try {
final String queryString = "select model from User 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);
return query.getResultList();
}
});
} catch (RuntimeException re) {
logger.error("find by property name failed", re);
throw re;
}
}
public List<User> findByName(Object name) {
return findByProperty(NAME, name);
}
public List<User> findByPassword(Object password) {
return findByProperty(PASSWORD, password);
}
public List<User> findByEmail(Object email) {
return findByProperty(EMAIL, email);
}
/**
* Find all User entities.
*
* @return List<User> all User entities
*/
@SuppressWarnings("unchecked")
public List<User> findAll() {
logger.info("finding all User instances");
try {
final String queryString = "select model from User model";
return getJpaTemplate().executeFind(new JpaCallback() {
public Object doInJpa(EntityManager em)
throws PersistenceException {
Query query = em.createQuery(queryString);
return query.getResultList();
}
});
} catch (RuntimeException re) {
logger.error("find all failed", re);
throw re;
}
}
public static IUserDAO getFromApplicationContext(ApplicationContext ctx) {
return (IUserDAO) ctx.getBean("UserDAO");
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
StrutsSpringJpa-test.zip (77个子文件)
test
.mystrutsdata 250B
.springBeans 320B
.settings
com.genuitec.eclipse.persistence.jpa.prefs 304B
src
META-INF
persistence.xml 878B
log4j.properties 549B
com
struts
ApplicationResources.properties 75B
form
LoginForm.java 2KB
RegForm.java 2KB
action
LoginAction.java 3KB
RegAction.java 3KB
cn
tools
SetCharacterEncodingFilter.java 1KB
jpa
User.java 2KB
UserDAO.java 7KB
Test.java 779B
IUserDAO.java 4KB
applicationContext.xml 2KB
.project 2KB
WebRoot
META-INF
MANIFEST.MF 39B
WEB-INF
struts-nested.tld 70KB
validator-rules.xml 13KB
struts-bean.tld 9KB
struts-html.tld 72KB
struts-logic.tld 14KB
struts-tiles.tld 8KB
web.xml 2KB
.struts-config.mex 2KB
classes
META-INF
persistence.xml 878B
log4j.properties 549B
com
struts
ApplicationResources.properties 75B
form
RegForm.class 1KB
LoginForm.class 1KB
action
LoginAction.class 4KB
RegAction.class 4KB
cn
tools
SetCharacterEncodingFilter.class 2KB
jpa
Test.class 2KB
User.class 2KB
IUserDAO.class 677B
UserDAO$2.class 992B
UserDAO.class 4KB
UserDAO$1.class 1KB
applicationContext.xml 2KB
struts-config.xml 1KB
lib
log4j-1.2.14.jar 359KB
spring-agent.jar 2KB
asm-util-2.2.3.jar 35KB
commons-attributes-api.jar 36KB
spring-toplink.jar 57KB
spring-beans.jar 381KB
persistence.jar 71KB
spring-jpa.jar 96KB
spring-core.jar 179KB
spring-context.jar 158KB
mysql-connector-java-5.1.6-bin.jar 687KB
spring-jdo.jar 61KB
asm-commons-2.2.3.jar 15KB
commons-logging.jar 52KB
spring-struts.jar 25KB
aspectjweaver.jar 1.79MB
spring-dao.jar 128KB
asm-2.2.3.jar 35KB
cglib-nodep-2.1_3.jar 315KB
aspectjrt.jar 114KB
aopalliance.jar 5KB
spring-tomcat-weaver.jar 5KB
commons-attributes-compiler.jar 29KB
spring-aop.jar 302KB
spring-jdbc.jar 222KB
spring-ibatis.jar 24KB
jakarta-oro-2.0.8.jar 63KB
spring-hibernate3.jar 105KB
spring-web.jar 149KB
reg.jsp 615B
login.jsp 849B
index.jsp 1KB
logout.jsp 1KB
.mymetadata 285B
.classpath 3KB
共 77 条
- 1
资源评论
puterking888
- 粉丝: 5
- 资源: 138
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功