hibernate通过query查询返回实体集合相关注意事项

Output实体:

import javax.persistence.*;
import java.util.Date;

@Entity
@Table(name = "Output")
public class Output {
	@Id
	@Column(name = "BuildID")
	private Integer buildId;
	
	@Transient
	private String buildName;
	
	@Transient
	private Integer success;
	
	@Transient
	private Integer failure;
	
	@Transient
	private Integer total;
	
	@Column(name = "BuildNumber")
	private Integer buildNumber;
	
	@Column(name = "TaskID")
	private Integer taskId;
	
	@Column(name = "Output")
	private String output;
	
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "LastUpdate")
	private Date lastUpdate;

	public Integer getBuildId() {
		return buildId;
	}

	public void setBuildId(Integer buildId) {
		this.buildId = buildId;
	}

	public String getBuildName() {
		return buildName;
	}

	public void setBuildName(String buildName) {
		this.buildName = buildName;
	}

	public Integer getBuildNumber() {
		return buildNumber;
	}

	public void setBuildNumber(Integer buildNumber) {
		this.buildNumber = buildNumber;
	}

	public Integer getTaskId() {
		return taskId;
	}

	public void setTaskId(Integer taskId) {
		this.taskId = taskId;
	}

	public String getOutput() {
		return output;
	}

	public void setOutput(String output) {
		this.output = output;
	}

	public Date getLastUpdate() {
		return lastUpdate;
	}

	public void setLastUpdate(Date lastUpdate) {
		this.lastUpdate = lastUpdate;
	}

	public Integer getSuccess() {
		return success;
	}

	public void setSuccess(Integer success) {
		this.success = success;
	}

	public Integer getFailure() {
		return failure;
	}

	public void setFailure(Integer failure) {
		this.failure = failure;
	}

	public Integer getTotal() {
		return total;
	}

	public void setTotal(Integer total) {
		this.total = total;
	}

    public Output(Integer buildId, Integer buildNumber, Integer taskId,String output, Date lastUpdate) {
        this.buildId = buildId;
        this.buildNumber = buildNumber;
        this.taskId = taskId;
        this.output = output;
        this.lastUpdate = lastUpdate;
    }
}

 

Repository:注意select的是类名而且传入的参数也是实体类中的变量

import com.accela.acdp.dao.OutputDao;
import com.accela.acdp.entity.Output;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;


/**
 * Created by jason.zhan on 8/12/2014.
 */
@Repository
public class OutputDaoImpl implements OutputDao {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public List getOutputByBuildId(Integer buildDi) throws Exception{
        if (buildDi != null) {
            Session session = sessionFactory.getCurrentSession();
            if (session == null) {
                session = sessionFactory.openSession();
            }
            String hql = "SELECT new Output(a.buildId, a.buildNumber,a.taskId,a.output,a.lastUpdate) FROM Output as a WHERE a.buildNumber = (SELECT MAX (b.buildNumber) FROM Output as b) AND a.buildId = 2046";
            //String hql = "SELECT a.buildId, a.buildNumber,a.taskId,a.output,a.lastUpdate FROM Output as a WHERE a.buildNumber = (SELECT MAX (b.buildNumber) FROM Output as b) AND a.buildId = 2046";


            Query query = session.createQuery(hql);
            List outputs = query.list();
            return outputs;
        }
        return null;
    }
}


 

 


 

你可能感兴趣的:(Java,数据库)