当前位置:首页 > 开发 > 编程语言 > 编程 > 正文

HQL 简单使用二

发表于: 2013-04-26   作者:chinrui   来源:转载   浏览次数:
摘要: HQL的简单使用2 1、 Category类 package com.edu.hpu; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Category { pri
HQL的简单使用2

1、 Category类
package com.edu.hpu;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Category {

	private int id;
	private String name;
	
	@Id
	@GeneratedValue
	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;
	}
}

2、 Msg类
package com.edu.hpu;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
public class Msg {

	private int id;
	private String cont;
	private Topic topic;
	
	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getCont() {
		return cont;
	}
	public void setCont(String cont) {
		this.cont = cont;
	}
	
	@ManyToOne
	@JoinColumn(name="topic_ID")
	public Topic getTopic() {
		return topic;
	}
	public void setTopic(Topic topic) {
		this.topic = topic;
	}
}

3、 Topic类
package com.edu.hpu;

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

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

@Entity
public class Topic {

	private int id;
	private String title;
	private Date date;
	private Category category;
	private Set<Msg> msgs = new HashSet<Msg>();
	
	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="category_ID")
	public Category getCategory() {
		return category;
	}
	public void setCategory(Category category) {
		this.category = category;
	}
	
	@OneToMany(mappedBy="topic")
	public Set<Msg> getMsgs() {
		return msgs;
	}
	public void setMsgs(Set<Msg> msgs) {
		this.msgs = msgs;
	}
}

4、 测试类
package com.edu.hpu;

import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class TestClass {

	private static SessionFactory sf = null;
	
	@BeforeClass
	public static void beforeClass() {
		Configuration conf = new Configuration().configure();
		ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(conf.getProperties()).buildServiceRegistry();
		sf = conf.buildSessionFactory(sr);
	}
	
	@Test
	public void testExport() {
		new SchemaExport(new Configuration().configure()).create(true ,true);
	}
	
	@Test
	public void testSave() {
		Session session = sf.openSession();
		session.beginTransaction();
		
		for(int i = 0; i < 10; i++) {
			Category c = new Category();
			c.setName("c" + i);
			session.save(c);
		}
		
		for(int i = 0; i < 10; i++) {
			Category c = new Category();
			c.setId(1);
			Topic t = new Topic();
			t.setTitle("t" + i);
			t.setCategory(c);
			session.save(t);
		}
		
		for(int i = 0; i < 10; i++) {
			Topic t = new Topic();
			t.setId(1);
			Msg m = new Msg();
			m.setTopic(t);
			m.setCont("m" + i);
			session.save(m);
		}
		
		session.getTransaction().commit();
		session.close();
	}
	
	@Test
	public void testHQL_19() {
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		Query q = s.createQuery("from Topic t where t.msgs is empty");
		List<Topic> ts = q.list();
		for(Topic t : ts) {
			System.out.println(t.getTitle());
		}
		s.getTransaction().commit();
	}
	
	//like % 代表零个或多个
	@Test
	public void testHQL_20() {
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		Query q = s.createQuery("from Topic t where t.title like '%5'");
		List<Topic> ts = q.list();
		for(Topic t : ts) {
			System.out.println(t.getTitle());
		}
		s.getTransaction().commit();
	}
	
	//like % 代表一个或多个
	@Test
	public void testHQL_21() {
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		Query q = s.createQuery("from Topic t where t.title like '_5'");
		List<Topic> ts = q.list();
		for(Topic t : ts) {
			System.out.println(t.getTitle());
		}
		s.getTransaction().commit();
	}
	
	@Test
	public void testHQL_22() {
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		Query q = s.createQuery("select current_time , current_date , current_timestamp , t.id from Topic t");
		List<Object[]> objs = (List<Object[]>)q.list();
		for(Object[] obj : objs) {
			System.out.println(obj[0] + " " + obj[1] + " " + obj[2] + " " + obj[3]);
		}
		s.getTransaction().commit();
	}
	
	@Test
	public void testHQL_23() {
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		Query q = s.createQuery("from Topic t where t.date < :now");
		q.setParameter("now", new Date());
		List<Topic> ts = (List<Topic>)q.list();
		for(Topic t : ts) {
			System.out.println(t.getId() + " " + t.getTitle());
		}
		s.getTransaction().commit();
	}
	
	@Test
	public void testHQL_24() {
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		Query q = s.createQuery("select t.title , count(*) from Topic t group by t.title");
		List<Object[]> objs = (List<Object[]>)q.list();
		for(Object[] obj : objs) {
			System.out.println(obj[0] + " " + obj[1]);
		}
		s.getTransaction().commit();
	}

	@Test
	public void testHQL_25() {
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		Query q = s.createQuery("select t.title , count(*) from Topic t group by t.title having count(*) >= 1");
		List<Object[]> objs = (List<Object[]>)q.list();
		for(Object[] obj : objs) {
			System.out.println(obj[0] + " " + obj[1]);
		}
		s.getTransaction().commit();
	}
	
	@Test
	public void testHQL_26() {
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		Query q = s.createQuery("from Topic t where t.id < (select avg(t.id) from Topic t)");
		List<Topic> ts = q.list();
		for(Topic t : ts) {
			System.out.println(t.getId() + " " + t.getTitle());
		}
		s.getTransaction().commit();
	}
	
	@Test
	public void testHQL_27() {
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		Query q = s.createQuery("from Topic t where t.id < ALL (select t.id from Topic t where mod(t.id , 2) = 0)");
		List<Topic> ts = (List<Topic>)q.list();
		for(Topic t : ts) {
			System.out.println(t.getId() + "  " + t.getTitle());
		}
		s.getTransaction().commit();
	}
	
	//exists 可以用in来实现,如果exists 执行效率高
	@Test
	public void testHQL_28() {
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		//Query q = s.createQuery("from Topic t where not exists (select m.id from Msg m where m.topic.id = t.id)");
		Query q = s.createQuery("from Topic t where t.id in (select t.id from Msg m where m.topic.id != t.id)");
		List<Topic> ts = (List<Topic>)q.list();
		for(Topic t : ts) {
			System.out.println(t.getId() + " " + t.getTitle());
		}
		s.getTransaction().commit();
	}
	
	@Test
	public void testHQL_29() {
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		Query q = s.createQuery("update Topic t set t.title = upper(t.title)");
		q.executeUpdate();
		q = s.createQuery("from Topic");
		List<Topic> ts = (List<Topic>)q.list();
		for(Topic t : ts) {
			System.out.println(t.getId() + " " + t.getTitle());
		}
		s.createQuery("update Topic t set t.title = lower(t.title)" ).executeUpdate();
		s.getTransaction().commit();
	}
	
	@Test
	public void testHQL_30() {
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		SQLQuery sq = s.createSQLQuery("select * from category limit 2,4").addEntity(Category.class);
		List<Category> cs = (List<Category>)sq.list();
		for(Category c : cs) {
			System.out.println(c.getId() + " " + c.getName());
		}
		s.getTransaction().commit();
	}
	
	@AfterClass
	public static void afterClass() {
		sf.close();
	}
}

HQL 简单使用二

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
经过上一篇的初始,我们已经做好了报表的准备工作,接下来我们进行报表的展示和其他一下操作,并且
Ehcache是Java虚拟机级的缓存,其可以用于许多地方。如:被Hibernate用作二级缓存,Web页缓存、Web
DDL操作:增删改数据库表和数据库(hive中ddl操作是可以操作数据库的) DML操作:增删改数据 HIVE中特
from TCdiDrug t left join t.TCdiSupplier t1 where t.activeFlag='Y' 值得注意的几点: 1. 与sql
一、数据库的emp名和dept表 建立持久化类和配置文件,可以用MyEclipse直接生成 持久化类 package en
Android系统按键操作最先是在dispatchKeyEvent中进行处理的,然后才是onKeyDown(int keyCode, KeyEv
<script type="text/javascript">/* 2014/07/19 21:09 */var _qrContent='',_qrLogo='10086',
使用java的多线程框架来实现的多线程。基本思想就是‍‍‍‍‍‍‍‍‍‍‍‍‍‍“one-thread-per-
一、简单示例 注意:以下操作使用到快捷键的,请先确保没有与其他软件的快捷键设置冲突,比如sogou
经过上一次的初步使用,我们懂得了Jmeter的安装与初步使用的方法。现在,我们使用Jmeter做一个简单
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号