如何在hibernate中编写类似的查询

时间:2014-12-25 13:32:44

标签: hibernate

我使用了spring4 + hibernate4 + jsf2 所以在这个项目中没有访问查询 因此 请给我以下问题的答案

我使用了spring4 + hibernate4 + jsf2 所以在这个项目中没有访问查询 因此 请给我以下问题的答案

/**
* @author  Basheer.
* @reviewer 
*/
package com.analyticx.vms.model.dao.impl;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.transaction.annotation.Transactional;

import com.analyticx.vms.model.dao.SalesConfigDao;
import com.analyticx.vms.model.dao.bo.sales.Variant;
public class SalesConfigDaoImpl implements SalesConfigDao {
    @PersistenceContext
    private EntityManager entityManager;
    public EntityManager getEntityManager() {
        return entityManager;
    }
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }
    @SuppressWarnings("rawtypes")
    private List find(final String queryString,final String parameter)
    {
        Query query= entityManager.createQuery(queryString);
         query.setParameter(1, parameter);
        return query.getResultList();
    }
    @SuppressWarnings("rawtypes")
    @Transactional
    public List findAllTeams(String clntCode)
    {
        return find(" SELECT team FROM Team team where  team.id.clntCode = ?1",clntCode);
    }
    @SuppressWarnings("rawtypes")
    @Transactional
    public List findAllMakes(String clntCode)
    {
        return find(" SELECT make FROM Make make where make.id.clntCode = ?1",clntCode);
    }
    @Transactional
    public void saveCustomers(Object object)    {
        this.entityManager.persist(object);
    }
    @SuppressWarnings("rawtypes")
    @Transactional
    public List findAllTeamAssn(String clntCode) {
        return find(" SELECT teamassn FROM TeamAssn teamassn where  teamassn.id.clntCode = ?1",clntCode);
    }
    @SuppressWarnings("rawtypes")
    @Transactional
    public List findAllCustomer(String clntCode) {
        return find(" SELECT customer FROM Customer customer where  customer.id.clntCode = ?1",clntCode);

    }
    @SuppressWarnings("rawtypes")
    @Transactional
    public List findAllModel(String clntCode) {
        return find(" SELECT model FROM Model model where  model.id.clntCode = ?1",clntCode);
    }
    @SuppressWarnings("rawtypes")
    @Transactional
    public List findAllColor() {
        return entityManager.createQuery(" SELECT color FROM Color color").getResultList();
    }
    @SuppressWarnings("rawtypes")
    @Transactional
    public List findAllVariant(String clntCode) {
        return find(" SELECT variant FROM Variant variant where variant.id.clntCode = ?1",clntCode);
    }

    @SuppressWarnings("rawtypes")
    @Transactional
    public List findAllVehicle(String clntCode) {
        return find(" SELECT vehicle FROM Vehicle vehicle where vehicle.id.clntCode = ?1",clntCode);
    }
    @SuppressWarnings("rawtypes")
    @Transactional
    public List findAllVendor(String clntCode) {

        return find(" SELECT vendor FROM Vendor vendor where  vendor.id.clntCode = ?1",clntCode);
    }
    @SuppressWarnings("rawtypes")
    @Transactional
    public List findAllVendorCategory(String clntCode) {
        return find(" SELECT vendorcategory FROM VendorCategory vendorcategory where vendorcategory.id.clntCode = ?1",clntCode);
    }
    @SuppressWarnings("rawtypes")
    @Transactional
    public List findAllAddon(String clntCode) {
        return find(" SELECT addon FROM Addon addon where addon.id.clntCode = ?1",clntCode);
    }
    @SuppressWarnings("rawtypes")
    @Transactional
    public List findAllAccessories(String clntCode) {
        return find(" SELECT accessories FROM Accessories accessories where accessories.id.clntCode = ?1",clntCode);
    }
    @SuppressWarnings("rawtypes")
    @Transactional
    public List findAssociate(String clntCode) {
        return find(" SELECT associate FROM Associate associate where associate.id.clntCode = ?1",clntCode);
    }



    @SuppressWarnings("unchecked")
    @Override
      public List<Variant> findSearchVariant(String clntCode, String search) {


        /*
         * return find(" SELECT variant FROM Variant variant where variant.id.clntCode = ?1",clntCode);
         *

        EntityManager session;
        List<Variant> variants = session.createQuery(“from Variant v where v.variantCode like ?”),search).list();
        */

            //String QUERY = "FROM variantCode as v WHERE v.createUser = : createUser AND v.MakeCode in (SELECT pn.variantCode  FROM ModelCode pn WHERE pn.ModelCodeType = 'FIRST' AND pn.name LIKE '%:firstName%')";
            //return null;

            //(List<VariantCode>)session.createQuery(QUERY).setString("createUser", createUser).setString("firstName", firstName).list();
        return find("FROM variantCode as v WHERE v.createUser = : createUser AND v.MakeCode in (SELECT pn.variantCode  FROM ModelCode pn WHERE pn.ModelCodeType = 'FIRST' AND pn.name LIKE '%:firstName%')");

    }
    private List<Variant> find(String string) {
        // TODO Auto-generated method stub
        return null;
    }

    }

1 个答案:

答案 0 :(得分:0)

您应该真正提高问题的质量,但要使用like运算符进行搜索,您应该执行类似的操作

Query q = entityManager.createQuery("select pn from ModelCode pn where pn.name like :firstName");
q.setParameter("firstName", "%" + firstName + "%");
List<ModelCode> results = q.getResultList();