...
Objects persisted via JPA are generally referred to as Managed Objects. In uPortal all domain objects are first defined by a public interface and then a concrete implementation with the JPA annotations is created. An example interface and implementation are included at the bottom of this section.
These guidelines should must be used when creating Managed Objects in uPortal
- Define the domain object methods in an Interface
- The domain object should be data focused, it must not also encapsulate buisness logic.
- The implementation should be package private and exist in the same package as the DAO implementation and the DAO implementation should be the only class ever creating new instances of the managed object.
- A surrogate key must be defined as the primary @Id for the object, business keys must never be used as the primary @Id.
- An immutable natural key shouldbe defined. This is one or more fields that make up a unique business identity for the object.
- These fields should be annotated with @NaturalId to gain significant performance improvements when querying using the natural id fields.
- These fields should be marked as final and passed in via the constructor
- All
nullable=false
fields must be passed in via the constructor to prevent invalid object creation .and should be marked as final - A private no-arg constructor is required to allow Hibernate to create the object using reflection. This constructor is private to protect against invalid object creation.
- Both
@TableGenerator
and @SequenceGenerator
annotations should be used to define the generator for the @GeneratedValue
annotation. This allows for the maximum flexibility and consistency across databases. - equals and hashCode must be implemented but must only ever include the fields that make up the unique business identity of the object.
- For objects defined by an interface all efforts should be made to allow two different implementations of the same object interface to be equal to each other
- toString should be implemented and should as much information as is needed to easily identify the object but not too much that it clutters log files.
- Objects should define a version field annotated with
@Version
to support optimistic locking and data-integrity checks
...
Expand |
---|
| JpaPortletTypeDao.java |
---|
| JpaPortletTypeDao.java |
---|
|
Code Block |
---|
| @Repository/**
public class* JpaPortletTypeDaoLicensed extendsto BaseJpaDaoJasig implementsunder IPortletTypeDaoone {or more privatecontributor CriteriaQuery<PortletTypeImpl>license
findAllTypesQuery; * privateagreements. CriteriaQuery<PortletTypeImpl>See findTypeByNameQuery;the NOTICE privatefile ParameterExpression<String>distributed nameParameter;with this privatework
EntityManager entityManager;* for @PersistenceContext(unitNameadditional = "uPortalPersistence")
public final void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
@Override
protected EntityManager getEntityManager() {
return this.entityManager;
}
@Override
public void afterPropertiesSet() throws Exception {
this.nameParameter = this.createParameterExpression(String.class, "name");
this.findAllTypesQuery = this.createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<PortletTypeImpl>>() {
@Override
public CriteriaQuery<PortletTypeImpl> apply(CriteriaBuilder cb) {
final CriteriaQuery<PortletTypeImpl> criteriaQuery = cb.createQuery(PortletTypeImpl.class);
criteriaQuery.from(PortletTypeImpl.class);
return criteriaQuery;
}
});
this.findTypeByNameQuery = this.createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<PortletTypeImpl>>() {
@Override
public CriteriaQuery<PortletTypeImpl> apply(CriteriaBuilder cb) {
final CriteriaQuery<PortletTypeImpl> criteriaQuery = cb.createQuery(PortletTypeImpl.class);
final Root<PortletTypeImpl> typeRoot = criteriaQuery.from(PortletTypeImpl.class);
criteriaQuery.select(typeRoot);
criteriaQuery.where(
cb.equal(typeRoot.get(PortletTypeImpl_.name), nameParameter)
);
return criteriaQuery;
}
});
}
@Override
@Transactional
public void deletePortletType(IPortletType type) {
Validate.notNull(type, "definitioninformation regarding copyright ownership.
* Jasig licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a
* copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jasig.portal.portlet.dao.jpa;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import org.apache.commons.lang.Validate;
import org.jasig.portal.jpa.BasePortalJpaDao;
import org.jasig.portal.jpa.OpenEntityManager;
import org.jasig.portal.portlet.dao.IPortletTypeDao;
import org.jasig.portal.portlet.om.IPortletType;
import org.springframework.stereotype.Repository;
import com.google.common.base.Function;
/**
* JPA/Hibernate implementation of IChannelTypeDao. This DAO handles
* channel types and is not yet integrated with the channel definition persistence
* code.
*
* @author Jen Bourey, jbourey@unicon.net
* @revision $Revision$
*/
@Repository
public class JpaPortletTypeDao extends BasePortalJpaDao implements IPortletTypeDao {
private CriteriaQuery<PortletTypeImpl> findAllTypesQuery;
@Override
public void afterPropertiesSet() throws Exception {
this.findAllTypesQuery = this.createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<PortletTypeImpl>>() {
@Override
public CriteriaQuery<PortletTypeImpl> apply(CriteriaBuilder cb) {
final CriteriaQuery<PortletTypeImpl> criteriaQuery = cb.createQuery(PortletTypeImpl.class);
criteriaQuery.from(PortletTypeImpl.class);
return criteriaQuery;
}
});
}
@Override
@PortalTransactional
public void deletePortletType(IPortletType type) {
Validate.notNull(type, "definition can not be null");
final IPortletType persistentChanneltype;
final EntityManager entityManager = this.getEntityManager();
if (entityManager.contains(type)) {
persistentChanneltype = type;
}
else {
persistentChanneltype = entityManager.merge(type);
}
entityManager.remove(persistentChanneltype);
}
@Override
@PortalTransactional
public IPortletType createPortletType(String name, String cpdUri) {
Validate.notEmpty(name, "name can not be null");
final IPortletType persistentChanneltype; if (this.entityManager.contains(type)) {
persistentChanneltype = typeValidate.notEmpty(cpdUri, "cpdUri can not be null");
} else { persistentChanneltype = this.entityManager.merge(type);
} this.entityManager.remove(persistentChanneltype); } @Override final @TransactionalPortletTypeImpl channelType public= IPortletTypenew createPortletTypePortletTypeImpl(String name, String cpdUri);
{ Validate.notEmpty(name, "name can not
be null"); Validate.notEmpty(cpdUri, "cpdUri can not be null" this.getEntityManager().persist(channelType);
final PortletTypeImpl channelType = new PortletTypeImpl(name,
cpdUri); this.entityManager.persist(channelType); return channelType;
}
@Override
public IPortletType getPortletType(int id) {
return this.entityManagergetEntityManager().find(PortletTypeImpl.class, id);
}
@OpenEntityManager(unitName = PERSISTENCE_UNIT_NAME)
@Override
public IPortletType getPortletType(String name) {
final TypedQuery<PortletTypeImpl>NaturalIdQuery<PortletTypeImpl> query = this.createCachedQuerycreateNaturalIdQuery(thisPortletTypeImpl.findTypeByNameQueryclass);
query.setParameterusing(thisPortletTypeImpl_.nameParametername, name);
final List<PortletTypeImpl> channelTypes =return query.getResultListload();
return DataAccessUtils.uniqueResult(channelTypes); }
@Override
public List<IPortletType> getPortletTypes() {
final TypedQuery<PortletTypeImpl> query = this.createCachedQuery(this.findAllTypesQuery);;
final List<PortletTypeImpl> portletTypes = query.getResultList();
return new ArrayList<IPortletType>(portletTypes);
}
@Override
@PortalTransactional
@Transactional public IPortletType updatePortletType(IPortletType type) {
Validate.notNull(type, "type can not be null");
this.entityManagergetEntityManager().persist(type);
return type;
}
}
|
|