Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public interface User
- {
- int getId();
- void setId(int id);
- UserRole getUserRole();
- void setUserRole(UserRole userRole);
- String getName();
- void setName(String name);
- String getPhone();
- void setPhone(String phone);
- String getEmail();
- void setEmail(String email);
- }
- @Entity
- @NamedQuery(name = "GenericUser.findAll", query = "SELECT u FROM GenericUser u")
- public class GenericUser implements User {
- @Id
- @GeneratedValue()
- private int id;
- private UserRole userRole;
- private String name;
- private String phone;
- private String email;
- public GenericUser(int id, UserRole userRole, String name, String phone, String email) {
- this.id = id;
- this.userRole = userRole;
- this.name = name;
- this.phone = phone;
- this.email = email;
- }
- public GenericUser(UserRole userRole, String name, String phone, String email){
- this.userRole = userRole;
- this.name = name;
- this.phone = phone;
- this.email = email;
- }
- public GenericUser(){
- }
- ....get/set....
- public interface DAO<T>
- {
- T findById(int id);
- Collection<T> findAll();
- void save(T t);
- void update(T t);
- void delete(T t);
- void delete(Collection <T> tCollection);
- }
- @TransactionManagement(value= TransactionManagementType.CONTAINER)
- @TransactionAttribute(value=REQUIRED)
- public abstract class AbstractJpaDao<T> implements DAO<T>
- {
- private static final Logger LOG = Logger.getLogger(AbstractJpaDao.class);
- @PersistenceContext(unitName = "cwPU")
- protected EntityManager entityManager;
- @Override
- public abstract T findById(int id);
- @Override
- public abstract Collection<T> findAll();
- @Override
- public void save(T t){
- LOG.debug("saving " + t);
- entityManager.persist(t);
- }
- @Override
- public void update(T t){
- entityManager.refresh(t);
- }
- @Override
- public void delete(T t){
- delete(Collections.singletonList(t));
- }
- @Override
- public void delete(Collection<T> collection){
- for (T t : collection){
- entityManager.remove(t);
- }
- }
- @PreDestroy
- private void closeEntityManager(){
- entityManager.close();
- }
- }
- @Stateless
- @Local(cw.data.DAO.class)
- public class GenericUserJpaDao extends AbstractJpaDao<User>
- {
- private static final Logger LOG = Logger.getLogger(GenericUserJpaDao.class);
- @Override
- public User findById(int id){
- LOG.debug("find by id: " + id);
- return entityManager.find(GenericUser.class, id);
- }
- @Override
- public Collection<User> findAll(){
- TypedQuery<GenericUser> genericOrderTypedQuery = entityManager.createNamedQuery("GenericUser.findAll", GenericUser.class);
- Collection<User> usersToReturn = new ArrayList<User>();
- usersToReturn.addAll(genericOrderTypedQuery.getResultList());
- return usersToReturn;
- }
- @Override
- public void save(User user) {
- LOG.debug("saving user " + user.getId());
- super.save(user);
- }
- }
- public class CreateUserObject extends HttpServlet {
- @EJB
- DAO<User> userDAO;
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- GenericUser genericUser = new GenericUser();
- genericUser.setId(0);
- genericUser.setEmail("sample");
- genericUser.setName("name");
- genericUser.setPhone("1");
- genericUser.setUserRole(UserRole.ADMIN);
- userDAO.save(genericUser);
- Collection<User> users = userDAO.findAll();
- resp.getWriter().write("size - " + users.size());
- for (User user : users){
- resp.getWriter().write("id - " + user.getId());
- }
- }
- }
- <persistence-unit name="cwPU" transaction-type="JTA">
- <provider>org.hibernate.ejb.HibernatePersistence</provider>
- <properties>
- <property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/>
- <property name="hibernate.connection.url" value="jdbc:oracle:thin:@localhost:1521:xe"/>
- <property name="hibernate.connection.username" value="cw"/>
- <property name="hibernate.connection.password" value="cw"/>
- <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
- <property name="hibernate.hbm2ddl.auto" value="create"/>
- </properties>
- </persistence-unit>
Add Comment
Please, Sign In to add comment