/src/main/java/com/google/ie/business/dao/impl/BaseDaoImpl.java

http://thoughtsite.googlecode.com/ · Java · 62 lines · 20 code · 9 blank · 33 comment · 4 complexity · 82aa294b7467a4869f3267fe3b0da918 MD5 · raw file

  1. /* Copyright 2010 Google Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS.
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License
  14. */
  15. package com.google.ie.business.dao.impl;
  16. import com.google.ie.business.dao.BaseDao;
  17. import org.springframework.orm.jdo.support.JdoDaoSupport;
  18. import org.springframework.transaction.annotation.Transactional;
  19. /**
  20. * A data access object specification base class for all data access objects
  21. * that use JDO to interact with the datastore.
  22. *
  23. * @author Charanjeet singh
  24. */
  25. @Transactional(readOnly = true)
  26. public abstract class BaseDaoImpl extends JdoDaoSupport implements BaseDao {
  27. /**
  28. * Finds an entity by primary key.
  29. *
  30. * @param clazz the class type of the entity to be fetched
  31. * @param key the key of the entity to be fetched
  32. * @return the fetched entity
  33. */
  34. @Override
  35. public <T> T findEntityByPrimaryKey(java.lang.Class<T> clazz, String key) {
  36. return key == null ? null : getJdoTemplate().getObjectById(clazz, key);
  37. }
  38. /**
  39. * Persist entity after checking persist manager availability.
  40. *
  41. * @param object the object to be persisted
  42. * @return the persisted object
  43. */
  44. public <T> T persist(T object) {
  45. if (javax.jdo.JDOHelper.getPersistenceManager(object) == null) {
  46. getJdoTemplate().makePersistent(object);
  47. } else {
  48. javax.jdo.JDOHelper.getPersistenceManager(object)
  49. .makePersistent(object);
  50. }
  51. return object;
  52. }
  53. }