package test;
import domain.Event;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.query.Query;
import java.util.Date;
import java.util.List;
/**
* Created by leo on 17-7-4.
*/
public class EventTest {
private static StandardServiceRegistry registry = null;
private static SessionFactory sessionFactory = null;
private static Session session= null;
static {
registry = new StandardServiceRegistryBuilder()
.configure("hibernate.cfg.xml")
.build();
sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
session = sessionFactory.openSession();
}
public static void main(String[] args){
EventTest test = new EventTest();
test.add();
// test.delete();
test.update();
test.findOne();
test.findAll();
test.findOnePage(0, 10);
test.fuzzyFindByTitle("th");
test.orderByShunXu();
session.close();
}
// add
public void add(){
session.beginTransaction();
session.save(new Event("17th event.", new Date()));
session.persist(new Event("18th event.", new Date()));
session.getTransaction().commit();
}
// delete
public void delete(){
session.beginTransaction();
Event event = session.load(Event.class, 2L);
session.delete(event);
session.getTransaction().commit();
}
// update
public void update(){
session.beginTransaction();
Event event = session.load(Event.class, 1L); //取得要修改的实例
event.setDate(new Date());
session.update(event);
session.getTransaction().commit();
}
// find one
public void findOne(){
session.beginTransaction();
Event event = session.load(Event.class, 5L);
System.out.println(event);
session.getTransaction().commit();
}
// find all
public void findAll(){
session.beginTransaction();
List result = session.createQuery("from Event").list();
for(Event event:(List<Event>)result){
System.out.println(event.toString());
}
session.getTransaction().commit();
}
// find a page
public void findOnePage(int offset, int pageSize){
session.beginTransaction();
Query query = session.createQuery("from Event event order by event.id");
// query.setFetchSize(offset);
query.setFirstResult(0);
query.setMaxResults(pageSize);
List<Event> list = query.list();
for(Event event : list){
System.out.println("from " + offset + " to " + (offset+pageSize));
System.out.println(event);
}
}
// 模糊查找
public void fuzzyFindByTitle(String title){
// String hql = "from Event as event where event.title like :title and event.info like :info";
String hql = "from Event event where event.title like :title";
Query query = session.createQuery(hql);
query.setParameter("title", "%" + title + "%");
List<Event> list = query.list();
System.out.println("fuzzy find by title:");
if(list == null){
System.out.println("Nothing to show.");
} else {
for(Event event : list){
System.out.println(event);
}
}
}
// 升降序列 : desc:down asc:up
public void orderByShunXu(){
String hql = "from Event AS event order by event.date DESC";
// String hql = "from Event AS event order by event.date ASC";
Query query = session.createQuery(hql);
List<Event> list = query.list();
System.out.println("===== order by date:");
if(list == null){
System.out.println("Nothing to show.");
} else {
for(Event event : list){
System.out.println(event);
}
}
}
}
hibernate5常用的增删改查和升降排序,模糊查找实例
最新推荐文章于 2020-05-09 15:22:15 发布