怎么使用EntityManager获得记录数

如下所示:

 

首先是要获得jpa的entityManager:

 

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class JPAUtil {
	
    private static EntityManagerFactory entityManagerFactory;
    static {
        try {
            entityManagerFactory = 
                Persistence.createEntityManagerFactory("mmsbackup");
        }
        catch(Throwable ex) {
            throw new ExceptionInInitializerError(ex);
        }
    }
 
    public static EntityManagerFactory getEntityManagerFactory() {
        return entityManagerFactory;
    }
 
    public static void shutdown() {
        getEntityManagerFactory().close();
    }
}

 

然后编写实体:

 

package net.kentop.mmsbackend.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.Transient;

import net.kentop.common.util.BlankUtil;

import org.hibernate.annotations.GenericGenerator;

/**
 * smil文件实体
 * @author lhx1026
 *
 */

@Entity
@Table(name="SMIL_FILES")
public class SmilFile implements Serializable{


	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1025423394267546744L;

	private String mmsid;
	
	private String title;
	
	private byte[] smil;
	
	private Date createDate;
	
	private String startDate;
	
	private String endDate;
	
	private Set<SmilMedia> medias = new HashSet<SmilMedia>();

	@Id
	@Column(length = 32)
	@GenericGenerator(name = "uuid", strategy = "uuid.hex")
	@GeneratedValue(generator = "uuid")
    public String getMmsid() {
		return mmsid;
	}

	public void setMmsid(String mmsid) {
		this.mmsid = mmsid;
	}

	@Column(nullable=false)
	public String getTitle() {
		return title;
	}


	public void setTitle(String title) {
		this.title = title;
	}

	@Lob
	@Column(nullable=false)
	public byte[] getSmil() {
		return smil;
	}

	public void setSmil(byte[] smil) {
		this.smil = smil;
	}

	@Column(nullable=false)
	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}
	
	
	
	@Transient
	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	@Transient
	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	@OneToMany(fetch=FetchType.LAZY,mappedBy="smilFile",cascade={CascadeType.ALL})
	public Set<SmilMedia> getMedias() {
		return medias;
	}

	public void setMedias(Set<SmilMedia> medias) {
		this.medias = medias;
	}

	public SmilFile(){
		
	}
	
	public int hashCode() {
		 return mmsid.hashCode();
	}
	
	public boolean equals(Object obj) {
		
		if (BlankUtil.isBlank(obj)) {
			return false;
		}

		if (this == obj) {
			return true;
		}

		if (!(obj instanceof SmilFile)) {
			return false;
		}

		SmilFile smil = (SmilFile) obj;

		if (BlankUtil.isBlank(this.mmsid) || BlankUtil.isBlank(smil.getMmsid())) {
			return false;
		}

		return this.mmsid.equals(smil.getMmsid());
	}
	
	public String toString() {
		
		return "id:"+mmsid+",title"+title;
	}
	
	
}

 

编写dao层中的获得记录数的方法:

 

public static Long getCountsByConditions(SmilFile file){
		
	StringBuilder builder = new StringBuilder().append("select count(*) from SmilFile po where 1=1");
		if(!BlankUtil.isBlank(file)){
			
			if(!BlankUtil.isBlank(file.getTitle())){
				
				builder.append(" and po.title = :title");
			}
			
		}
		
		EntityManager em = JPAUtil.getEntityManagerFactory().createEntityManager();
		
		Query query = em.createQuery(builder.toString());
		
		if(!BlankUtil.isBlank(file.getTitle())){
		query.setParameter("title",file.getTitle());
		}
		
		Long counts = (Long)query.getSingleResult();//就是这个getSingleResult()方法
		
		em.close();
		
		return counts;
	}
 

 

你可能感兴趣的:(DAO,Hibernate,.net,jpa)