Example of @Where in Hibernate

本文介绍如何使用 Hibernate 的 @Where 注解过滤实体数据。通过 State 类的示例展示了如何仅加载满足条件的数据行。代码包括 State 实体类定义及 HibernateUtil 工具类的使用。

@Where in hibernate can be applied at entity level. With the help of @Where , we can use where class to fetch the data. The entity will be populated only for those data for which @Where clause returns true. Find the example below. Find the example. 

State.java

package com.concretepage.persistence;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.Where;

@Entity
@Table(name = "state")
@Where(clause="id>1") 
public class State  implements Serializable{
	private static final long serialVersionUID = 1L;
	@Id
	@Column(name = "id")
	private int id;
	@Column(name = "name") 
	private String name;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

HibernateUtil.java

package com.concretepage.util;
import java.util.List;
import java.util.Properties;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import com.concretepage.persistence.State;
public class HibernateUtil {
	private static final SessionFactory concreteSessionFactory;
		static {
		 try {
				Properties prop= new Properties();
				prop.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/hibernate");
				prop.setProperty("hibernate.connection.username", "root");
				prop.setProperty("hibernate.connection.password", "");
				prop.setProperty("hibernate.hbm2ddl.auto", "update");
				prop.setProperty("dialect", "org.hibernate.dialect.MySQLDialect");
				
				concreteSessionFactory = new AnnotationConfiguration()
			   .addPackage("com.concretepage.persistence")
					   .addProperties(prop)
					   .addAnnotatedClass(State.class)
					   .buildSessionFactory();
		  } catch (Throwable ex) {
			throw new ExceptionInInitializerError(ex);
		  }
		}
		public static Session getSession()
				throws HibernateException {
			return concreteSessionFactory.openSession();
		}
		public static void main(String... args){
			Session session=getSession();
	        List<State> states = session.createCriteria(State.class).list();
	        for(State s: states){
	        	System.out.println(s.getId()+" "+s.getName());
	        }
	    	session.close();
	   }
	}

Table: state

 1  UP 
 2  MP 
 3  HP

Output

 2 MP 
 3 HP

As we can see that in the table there are three rows but we got only two rows. This is because of @Where clause applied on the entity as @Where(clause="id>1")


转载于:https://my.oschina.net/heroShane/blog/199972

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值