net.groupbuy.dao.impl.DeliveryCenterDaoImpl.java Source code

Java tutorial

Introduction

Here is the source code for net.groupbuy.dao.impl.DeliveryCenterDaoImpl.java

Source

/*
 * Copyright 2005-2013 shopxx.net. All rights reserved.
 * Support: http://www.shopxx.net
 * License: http://www.shopxx.net/license
 */
package net.groupbuy.dao.impl;

import javax.persistence.FlushModeType;
import javax.persistence.NoResultException;

import net.groupbuy.dao.DeliveryCenterDao;
import net.groupbuy.entity.DeliveryCenter;

import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

/**
 * Dao - ?
 * 
 * @author SHOP++ Team
 * @version 3.0
 */
@Repository("deliveryCenterDaoImpl")
public class DeliveryCenterDaoImpl extends BaseDaoImpl<DeliveryCenter, Long> implements DeliveryCenterDao {

    public DeliveryCenter findDefault() {
        try {
            String jpql = "select deliveryCenter from DeliveryCenter deliveryCenter where deliveryCenter.isDefault = true";
            return entityManager.createQuery(jpql, DeliveryCenter.class).setFlushMode(FlushModeType.COMMIT)
                    .getSingleResult();
        } catch (NoResultException e) {
            return null;
        }
    }

    /**
     * ??
     * 
     * @param deliveryCenter
     *            ?
     */
    @Override
    public void persist(DeliveryCenter deliveryCenter) {
        Assert.notNull(deliveryCenter);
        if (deliveryCenter.getIsDefault()) {
            String jpql = "update DeliveryCenter deliveryCenter set deliveryCenter.isDefault = false where deliveryCenter.isDefault = true";
            entityManager.createQuery(jpql).setFlushMode(FlushModeType.COMMIT).executeUpdate();
        }
        super.persist(deliveryCenter);
    }

    /**
     * ?
     * 
     * @param deliveryCenter
     *            ?
     * @return ?
     */
    @Override
    public DeliveryCenter merge(DeliveryCenter deliveryCenter) {
        Assert.notNull(deliveryCenter);
        if (deliveryCenter.getIsDefault()) {
            String jpql = "update DeliveryCenter deliveryCenter set deliveryCenter.isDefault = false where deliveryCenter.isDefault = true and deliveryCenter != :deliveryCenter";
            entityManager.createQuery(jpql).setFlushMode(FlushModeType.COMMIT)
                    .setParameter("deliveryCenter", deliveryCenter).executeUpdate();
        }
        return super.merge(deliveryCenter);
    }

}