com.siriusit.spezg.multilib.storage.jpa.JpaBinaryAttachmentRepository.java Source code

Java tutorial

Introduction

Here is the source code for com.siriusit.spezg.multilib.storage.jpa.JpaBinaryAttachmentRepository.java

Source

/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF 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 com.siriusit.spezg.multilib.storage.jpa;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;

import com.siriusit.spezg.multilib.domain.BinaryAttachment;
import com.siriusit.spezg.multilib.domain.Unit;
import com.siriusit.spezg.multilib.domain.jpa.BinaryAttachmentDomainObject;
import com.siriusit.spezg.multilib.storage.BinaryAttachmentRepository;

/**
 * Created by IntelliJ IDEA. User: landssve Date: 09.sep.2010 Time: 07:28:31 To
 * change this template use File | Settings | File Templates.
 */
@Repository("binaryAttachmentRepository")
public class JpaBinaryAttachmentRepository implements BinaryAttachmentRepository {

    @PersistenceContext(unitName = "multilib-db")
    private EntityManager entityManager;

    public EntityManager getEntityManager() {
        return entityManager;
    }

    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    @Override
    public BinaryAttachment createBinaryAttachment(Unit unit, String type, String mimeType, byte[] content) {
        BinaryAttachment binaryAttachment = new BinaryAttachmentDomainObject();
        binaryAttachment.setUnit(unit);
        binaryAttachment.setType(type);
        binaryAttachment.setMimeType(mimeType);
        binaryAttachment.setContent(content);
        entityManager.persist(binaryAttachment);
        return binaryAttachment;
    }

    @Override
    public BinaryAttachment findBinaryAttachmentByUnit(Unit unit) {
        TypedQuery<BinaryAttachmentDomainObject> query = entityManager
                .createNamedQuery("findAttachmentByUnitAndType", BinaryAttachmentDomainObject.class);
        query.setParameter("unit", unit);
        return query.getSingleResult();
    }
}