How to Use Criteria and Hibernates

Viewed 25

Hello guys I need help to create a query to use in my @Repository.

This is the sql query consult

select * from sig_versions where sft_product_name like 'kiadoc-desktop' order by created_date DESC limit 1;

And this is the output

enter image description here

I don't know how to create the hibernate query using Hibernate and criteria

Now how can I call this from my @Repository

public String getSoftwareVersion(String name) throws InternalErrorException {
    logger.info("getSoftwareVersion "   + name);
    Criteria criteria = this.sessionFactory.getCurrentSession().createCriteria(this.typeParameterClass);
    criteria.add(Restrictions.eq("sft_product_name", name));
    return ((SoftwareVersion) criteria.uniqueResult()).get_Version();
}

I'm getting the InvocationTargetException

These are my files

package ar.com.lakaut.sig.core.dao;


import ar.com.lakaut.sig.core.domain.SoftwareVersion;
import com.curcico.jproject.core.exception.InternalErrorException;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Query;
import java.util.List;
import com.curcico.jproject.core.daos.BaseAuditedEntityDaoImpl;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;


@Repository
public class SoftwareVersionDaoImpl extends BaseAuditedEntityDaoImpl<SoftwareVersion> implements SoftwareVersionDao {

    public SoftwareVersionDaoImpl() { super(); }

    Logger logger = Logger.getLogger(getClass());


    public String getSoftwareVersion(String name) throws InternalErrorException {
        logger.info("getSoftwareVersion "   + name);
        Criteria criteria = this.sessionFactory.getCurrentSession().createCriteria(this.typeParameterClass);
        criteria.add(Restrictions.eq("sft_product_name", name));
        return ((SoftwareVersion) criteria.uniqueResult()).get_Version();
    }}

And the entity

package ar.com.lakaut.sig.core.domain;

import com.curcico.jproject.core.entities.BaseAuditedEntity;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.io.Serializable;

@Entity
@Table(name = "sig_versions")
@SQLDelete(sql="UPDATE sig_versions SET deleted = '1' WHERE version_id = ? and version = ?")
@Where(clause="deleted is null")
public class SoftwareVersion extends BaseAuditedEntity implements Serializable {

    public SoftwareVersion() {}

    private String product_name;
    private String sft_version;
    private String obs;

    @Id
    @SequenceGenerator(name = "id_generator", sequenceName = "sig_versions_config_seq", allocationSize = 1)
    @GeneratedValue(generator = "id_generator", strategy =GenerationType.SEQUENCE)
    @Column(name = "sft_id", unique = true, nullable = false)
    public Integer getId() { return this.id; }

    @Column(name = "sft_product_name", nullable = false)
    public String getProductName() { return product_name; }

    public void setProductName(String product_name) { this.product_name = product_name; }

    @Column(name = "sft_version", nullable = false)
    public String get_Version() { return sft_version; }

    public void set_Version(String version) { this.sft_version = version; }

    @Column(name = "sft_obs", nullable = false)
    public String getObs() { return obs; }

    public void setObs(String obs) { this.obs = obs; }


}

The @Service

    package ar.com.lakaut.sig.core.service;

import ar.com.lakaut.sig.core.domain.SoftwareVersion;
import com.curcico.jproject.core.exception.BaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.curcico.jproject.core.services.BaseAuditedEntityServiceImpl;
import ar.com.lakaut.sig.core.dao.SoftwareVersionDao;
import org.apache.log4j.Logger;

@Service("SoftwareVersionService")
public class SoftwareVersionServiceImpl extends BaseAuditedEntityServiceImpl<SoftwareVersion, SoftwareVersionDao> implements SoftwareVersionService {

    Logger logger = Logger.getLogger(SoftwareVersionServiceImpl.class);


    @Override
    public String getSoftwareVersion(String name) throws BaseException {
        logger.info("softwareVersionId: " + name);
        return dao.getSoftwareVersion(name);
    }

}

The Service

 package ar.com.lakaut.sig.core.service;

import ar.com.lakaut.sig.core.domain.SoftwareVersion;
import com.curcico.jproject.core.services.BaseAuditedEntityService;
import com.curcico.jproject.core.exception.BaseException;

public interface SoftwareVersionService extends BaseAuditedEntityService<SoftwareVersion> {

    /**
     * Params: name
     * Return
     * Throws BaseException
     */
    public String getSoftwareVersion(String name) throws BaseException;
}

The DAO

    package ar.com.lakaut.sig.core.dao;

import com.curcico.jproject.core.daos.BaseAuditedEntityDao;
import com.curcico.jproject.core.exception.BaseException;
import com.curcico.jproject.core.exception.InternalErrorException;
import ar.com.lakaut.sig.core.domain.SoftwareVersion;

public interface SoftwareVersionDao  extends BaseAuditedEntityDao<SoftwareVersion>{



    String getSoftwareVersion(String name) throws InternalErrorException;


}
0 Answers
Related