在功能方面是本地SQL>HQL>EJBQL>QBC>QBE

 

假想一个BBS。

 

板块:

package com.bjsxt.hibernate;

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;
    }
}

 

主题:

package com.bjsxt.hibernate;

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

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;

@Entity
@NamedQueries(
        {
            @NamedQuery(name="topic.selectCertainTopic", query="from Topic t where t.id = :id")
        }
        )
/*
@NamedNativeQueries(
        {
            @NamedNativeQuery(name="topic.select2_5Topic", query="select * from topic limit 2, 5")
        }
        )
*/

public class Topic {
    private int id;
    private String title;
    private Category category;
    private Date createDate;
    private List<Msg> msgs = new ArrayList<Msg>();
    @OneToMany(mappedBy="topic")
    public List<Msg> getMsgs() {
       
        return msgs;
    }
    public void setMsgs(List<Msg> msgs) {
        this.msgs = msgs;
    }
    public Date getCreateDate() {
        return createDate;
    }
    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }
    @ManyToOne(fetch=FetchType.LAZY)
    public Category getCategory() {
        return category;
    }
    public void setCategory(Category category) {
        this.category = category;
    }
    @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;
    }
   
}

 

回复:

package com.bjsxt.hibernate;

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

@Entity
public class Msg {
    private int id;
    private String cont;
    private Topic topic;
    @ManyToOne
    public Topic getTopic() {
        return topic;
    }
    public void setTopic(Topic topic) {
        this.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;
    }
   
}

 

EJBQL语句中创建对象的类:

 

package com.bjsxt.hibernate;

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

public class MsgInfo { //VO DTO Value Object username p1 p2 UserInfo->User->DB
    private int id;
    private String cont;
    private String topicName;
    private String categoryName;
    public MsgInfo(int id, String cont, String topicName, String categoryName) {
        super();
        this.id = id;
        this.cont = cont;
        this.topicName = topicName;
        this.categoryName = categoryName;
    }
    public String getTopicName() {
        return topicName;
    }
    public void setTopicName(String topicName) {
        this.topicName = topicName;
    }
    public String getCategoryName() {
        return categoryName;
    }
    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }
    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;
    }
   
}

 

EJBQL测试类1:

package com.bjsxt.hibernate;

import java.util.Date;

import java.util.List;

import org.hibernate.Query;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.AnnotationConfiguration;

import org.hibernate.tool.hbm2ddl.SchemaExport;

import org.junit.AfterClass;

import org.junit.BeforeClass;

import org.junit.Test;

public class HibernateQLTest {

private static SessionFactory sf;

@BeforeClass

public static void beforeClass() {

sf = new AnnotationConfiguration().configure().buildSessionFactory();

}

@AfterClass

public static void afterClass() {

sf.close();

}

@Test

public void testSchemaExport() {

new SchemaExport(new AnnotationConfiguration().configure()).create(false, 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.setCategory(c);

t.setTitle("t" + i);

t.setCreateDate(new Date());

session.save(t);

}

for(int i=0; i<10; i++) {

Topic t = new Topic();

t.setId(1);

Msg m = new Msg();

m.setCont("m" + i);

m.setTopic(t);

session.save(m);

}

session.getTransaction().commit();

session.close();

}

@Test

public void testHQL_01() {

Session session = sf.openSession();

session.beginTransaction();

Query q = session.createQuery("from Category");//类名

List<Category> categories = (List<Category>)q.list();

for(Category c : categories) {

System.out.println(c.getName());

}

session.getTransaction().commit();

session.close();

}

@Test

public void testHQL_02() {

Session session = sf.openSession();

session.beginTransaction();

Query q = session.createQuery("from Category c where c.name > 'c5'");

List<Category> categories = (List<Category>)q.list();

for(Category c : categories) {

System.out.println(c.getName());

}

session.getTransaction().commit();

session.close();

}

@Test

public void testHQL_03() {

Session session = sf.openSession();

session.beginTransaction();

Query q = session.createQuery("from Category c order by c.name desc");

List<Category> categories = (List<Category>)q.list();

for(Category c : categories) {

System.out.println(c.getName());

}

session.getTransaction().commit();

session.close();

}

@Test

public void testHQL_04() {

Session session = sf.openSession();

session.beginTransaction();

//distinct

Query q = session.createQuery("select distinct c from Category c order by c.name desc");

List<Category> categories = (List<Category>)q.list();

for(Category c : categories) {

System.out.println(c.getName());

}

session.getTransaction().commit();

session.close();

}

@Test

public void testHQL_05() {

Session session = sf.openSession();

session.beginTransaction();

/*Query q = session.createQuery("from Category c where c.id > :min and c.id < :max");

//q.setParameter("min", 2);

//q.setParameter("max", 8);

q.setInteger("min", 2);

q.setInteger("max", 8);*/

Query q = session.createQuery("from Category c where c.id > :min and c.id < :max")//冒号是个占位符

.setInteger("min", 2)

.setInteger("max", 8);

List<Category> categories = (List<Category>)q.list();

for(Category c : categories) {

System.out.println(c.getId() + "-" + c.getName());

}

session.getTransaction().commit();

session.close();

}

@Test

public void testHQL_06() {

Session session = sf.openSession();

session.beginTransaction();

Query q = session.createQuery("from Category c where c.id > ? and c.id < ?");

q.setParameter(0, 2)

.setParameter(1, 8);

// q.setParameter(1, 8);

List<Category> categories = (List<Category>)q.list();

for(Category c : categories) {

System.out.println(c.getId() + "-" + c.getName());

}

session.getTransaction().commit();

session.close();

}

//分页

@Test

public void testHQL_07() {

Session session = sf.openSession();

session.beginTransaction();

Query q = session.createQuery("from Category c order by c.name desc");

q.setMaxResults(4);//最多4条

q.setFirstResult(2);//从下标为2的那条开始,包括那条

List<Category> categories = (List<Category>)q.list();

for(Category c : categories) {

System.out.println(c.getId() + "-" + c.getName());

}

session.getTransaction().commit();

session.close();

}

@Test

public void testHQL_08() {

Session session = sf.openSession();

session.beginTransaction();

Query q = session.createQuery("select c.id, c.name from Category c order by c.name desc");

List<Object[]> categories = (List<Object[]>)q.list();//每一条当成一个对象数组

for(Object[] o : categories) {

System.out.println(o[0] + "-" + o[1]);

}

session.getTransaction().commit();

session.close();

}

//设定fetch type 为lazy后将不会有第二条sql语句

@Test

public void testHQL_09() {

Session session = sf.openSession();

session.beginTransaction();

Query q = session.createQuery("from Topic t where t.category.id = 1");

List<Topic> topics = (List<Topic>)q.list();

for(Topic t : topics) {

System.out.println(t.getTitle());

//System.out.println(t.getCategory().getName());

}

session.getTransaction().commit();

session.close();

}

//设定fetch type 为lazy后将不会有第二条sql语句

@Test

public void testHQL_10() {

Session session = sf.openSession();

session.beginTransaction();

Query q = session.createQuery("from Topic t where t.category.id = 1");

List<Topic> topics = (List<Topic>)q.list();

for(Topic t : topics) {

System.out.println(t.getTitle());

}

session.getTransaction().commit();

session.close();

}

@Test

public void testHQL_11() {

Session session = sf.openSession();

session.beginTransaction();

Query q = session.createQuery("from Msg m where m.topic.category.id = 1");

for(Object o : q.list()) {

Msg m = (Msg)o;

System.out.println(m.getCont());

}

session.getTransaction().commit();

session.close();

}

//了解即可

//VO Value Object

//DTO data transfer object

@Test

public void testHQL_12() {

Session session = sf.openSession();

session.beginTransaction();

Query q = session.createQuery("select new com.bjsxt.hibernate.MsgInfo(m.id, m.cont, m.topic.title, m.topic.category.name) from Msg");

for(Object o : q.list()) {

MsgInfo m = (MsgInfo)o;

System.out.println(m.getCont());

}

session.getTransaction().commit();

session.close();

}

//动手测试left right join

//为什么不能直接写Category名,而必须写t.category

//因为有可能存在多个成员变量(同一个类),需要指明用哪一个成员变量的连接条件来做连接

@Test

public void testHQL_13() {

Session session = sf.openSession();

session.beginTransaction();

Query q = session.createQuery("select t.title, c.name from Topic t join t.category c "); //join Category c

for(Object o : q.list()) {

Object[] m = (Object[])o;

System.out.println(m[0] + "-" + m[1]);

}

session.getTransaction().commit();

session.close();

}

//学习使用uniqueResult

@Test

public void testHQL_14() {

Session session = sf.openSession();

session.beginTransaction();

Query q = session.createQuery("from Msg m where m = :MsgToSearch "); //不重要

Msg m = new Msg();

m.setId(1);

q.setParameter("MsgToSearch", m);

Msg mResult = (Msg)q.uniqueResult();

System.out.println(mResult.getCont());

session.getTransaction().commit();

session.close();

}

@Test

public void testHQL_15() {

Session session = sf.openSession();

session.beginTransaction();

Query q = session.createQuery("select count(*) from Msg m");

long count = (Long)q.uniqueResult();

System.out.println(count);

session.getTransaction().commit();

session.close();

}

@Test

public void testHQL_16() {

Session session = sf.openSession();

session.beginTransaction();

Query q = session.createQuery("select max(m.id), min(m.id), avg(m.id), sum(m.id) from Msg m");

Object[] o = (Object[])q.uniqueResult();

System.out.println(o[0] + "-" + o[1] + "-" + o[2] + "-" + o[3]);

session.getTransaction().commit();

session.close();

}

@Test

public void testHQL_17() {

Session session = sf.openSession();

session.beginTransaction();

Query q = session.createQuery("from Msg m where m.id between 3 and 5");

for(Object o : q.list()) {

Msg m = (Msg)o;

System.out.println(m.getId() + "-" + m.getCont());

}

session.getTransaction().commit();

session.close();

}

@Test

public void testHQL_18() {

Session session = sf.openSession();

session.beginTransaction();

Query q = session.createQuery("from Msg m where m.id in (3,4, 5)");

for(Object o : q.list()) {

Msg m = (Msg)o;

System.out.println(m.getId() + "-" + m.getCont());

}

session.getTransaction().commit();

session.close();

}

//is null 与 is not null

@Test

public void testHQL_19() {

Session session = sf.openSession();

session.beginTransaction();

Query q = session.createQuery("from Msg m where m.cont is not null");

for(Object o : q.list()) {

Msg m = (Msg)o;

System.out.println(m.getId() + "-" + m.getCont());

}

session.getTransaction().commit();

session.close();

}

public static void main(String[] args) {

beforeClass();

}

}

 

 

EJBQL测试类2:

package com.bjsxt.hibernate;

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.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class HibernateQLTest {
    private static SessionFactory sf;
   
    @BeforeClass
    public static void beforeClass() {
        sf = new AnnotationConfiguration().configure().buildSessionFactory();
    }
    @AfterClass
    public static void afterClass() {
        sf.close();
    }
   
    @Test
    public void testSchemaExport() {
        new SchemaExport(new AnnotationConfiguration().configure()).create(false, 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.setCategory(c);
            t.setTitle("t" + i);
            t.setCreateDate(new Date());
            session.save(t);
           
        }
       
        for(int i=0; i<10; i++) {
           
            Topic t = new Topic();
            t.setId(1);
            Msg m = new Msg();
            m.setCont("m" + i);
            m.setTopic(t);
            session.save(m);
           
        }
       
       
       
       
        session.getTransaction().commit();
        session.close();
    }
   
    //is empty and is not empty
    @Test
    public void testHQL_20() {
        Session session = sf.openSession();
        session.beginTransaction();
        Query q = session.createQuery("from Topic t where t.msgs is empty");
       
        for(Object o : q.list()) {
            Topic t = (Topic)o;
            System.out.println(t.getId() + "-" + t.getTitle());
        }
        session.getTransaction().commit();
        session.close();
       
    }
   
    @Test
    public void testHQL_21() {
        Session session = sf.openSession();
        session.beginTransaction();
        Query q = session.createQuery("from Topic t where t.title like '%5'");
       
        for(Object o : q.list()) {
            Topic t = (Topic)o;
            System.out.println(t.getId() + "-" + t.getTitle());
        }
        session.getTransaction().commit();
        session.close();
       
    }
   
    @Test
    public void testHQL_22() {
        Session session = sf.openSession();
        session.beginTransaction();
        Query q = session.createQuery("from Topic t where t.title like '_5'");
       
        for(Object o : q.list()) {
            Topic t = (Topic)o;
            System.out.println(t.getId() + "-" + t.getTitle());
        }
        session.getTransaction().commit();
        session.close();
       
    }
    //不重要
    @Test
    public void testHQL_23() {
        Session session = sf.openSession();
        session.beginTransaction();
        Query q = session.createQuery("select lower(t.title)," +
                                             "upper(t.title)," +
                                             "trim(t.title)," +
                                             "concat(t.title, '***')," +
                                             "length(t.title)" +
                                             " from Topic t ");
       
        for(Object o : q.list()) {
            Object[] arr = (Object[])o;
            System.out.println(arr[0] + "-" + arr[1] + "-" + arr[2] + "-" + arr[3] + "-" + arr[4] + "-");
        }
        session.getTransaction().commit();
        session.close();
       
    }
   
    @Test
    public void testHQL_24() {
        Session session = sf.openSession();
        session.beginTransaction();
        Query q = session.createQuery("select abs(t.id)," +
                                             "sqrt(t.id)," +
                                             "mod(t.id, 2)" +
                                             " from Topic t ");
       
        for(Object o : q.list()) {
            Object[] arr = (Object[])o;
            System.out.println(arr[0] + "-" + arr[1] + "-" + arr[2] );
        }
        session.getTransaction().commit();
        session.close();
       
    }
   
    @Test
    public void testHQL_25() {
        Session session = sf.openSession();
        session.beginTransaction();
        Query q = session.createQuery("select current_date, current_time, current_timestamp, t.id from Topic t");
       
        for(Object o : q.list()) {
            Object[] arr = (Object[])o;
            System.out.println(arr[0] + " | " + arr[1] + " | " + arr[2] + " | " + arr[3]);
        }
        session.getTransaction().commit();
        session.close();
       
    }
   
    @Test
    public void testHQL_26() {
        Session session = sf.openSession();
        session.beginTransaction();
        Query q = session.createQuery("from Topic t where t.createDate < :date");
        q.setParameter("date", new Date());
        for(Object o : q.list()) {
            Topic t = (Topic)o;
            System.out.println(t.getTitle());
        }
        session.getTransaction().commit();
        session.close();
       
    }
   
    @Test
    public void testHQL_27() {
        Session session = sf.openSession();
        session.beginTransaction();
        Query q = session.createQuery("select t.title, count(*) from Topic t group by t.title") ;
        for(Object o : q.list()) {
            Object[] arr = (Object[])o;
            System.out.println(arr[0] + "|" + arr[1]);
        }
        session.getTransaction().commit();
        session.close();
       
    }
   
    @Test
    public void testHQL_28() {
        Session session = sf.openSession();
        session.beginTransaction();
        Query q = session.createQuery("select t.title, count(*) from Topic t group by t.title having count(*) >= 1") ;
        for(Object o : q.list()) {
            Object[] arr = (Object[])o;
            System.out.println(arr[0] + "|" + arr[1]);
        }
        session.getTransaction().commit();
        session.close();
       
    }
   
    @Test
    public void testHQL_29() {
        Session session = sf.openSession();
        session.beginTransaction();
        Query q = session.createQuery("from Topic t where t.id < (select avg(t.id) from Topic t)") ;
        for(Object o : q.list()) {
            Topic t = (Topic)o;
            System.out.println(t.getTitle());
        }
        session.getTransaction().commit();
        session.close();
       
    }
   
    @Test
    public void testHQL_30() {
        Session session = sf.openSession();
        session.beginTransaction();
        Query q = session.createQuery("from Topic t where t.id < ALL (select t.id from Topic t where mod(t.id, 2)= 0) ") ;
        for(Object o : q.list()) {
            Topic t = (Topic)o;
            System.out.println(t.getTitle());
        }
        session.getTransaction().commit();
        session.close();
       
    }
   
    //用in 可以实现exists的功能
    //但是exists执行效率高
    @Test
    public void testHQL_31() {
        Session session = sf.openSession();
        session.beginTransaction();// t.id not in (1)
        Query q = session.createQuery("from Topic t where not exists (select m.id from Msg m where m.topic.id=t.id)") ;
//        Query q = session.createQuery("from Topic t where exists (select m.id from Msg m where m.topic.id=t.id)") ;
        for(Object o : q.list()) {
            Topic t = (Topic)o;
            System.out.println(t.getTitle());
        }
        session.getTransaction().commit();
        session.close();
       
    }
   
    //update and delete
    //规范并没有说明是不是要更新persistent object,所以如果要使用,建议在单独的trasaction中执行
   
    @Test
    public void testHQL_32() {
        Session session = sf.openSession();
        session.beginTransaction();
        Query q = session.createQuery("update Topic t set t.title = upper(t.title)") ;
       
        q.executeUpdate();
        q = session.createQuery("from Topic");
        for(Object o : q.list()) {
            Topic t = (Topic)o;
            System.out.println(t.getTitle());
        }
        session.createQuery("update Topic t set t.title = lower(t.title)")
            .executeUpdate();
        session.getTransaction().commit();
        session.close();
       
    }
   
    //不重要
    @Test
    public void testHQL_33() {
        Session session = sf.openSession();
        session.beginTransaction();
        Query q = session.getNamedQuery("topic.selectCertainTopic");
        q.setParameter("id", 5);
        Topic t = (Topic)q.uniqueResult();
        System.out.println(t.getTitle());
        session.getTransaction().commit();
        session.close();
       
    }
   
    //Native(了解)
    @Test
    public void testHQL_34() {
        Session session = sf.openSession();
        session.beginTransaction();
        SQLQuery q = session.createSQLQuery("select * from category limit 2,4").addEntity(Category.class);
        List<Category> categories = (List<Category>)q.list();
        for(Category c : categories) {
            System.out.println(c.getName());
        }
        session.getTransaction().commit();
        session.close();
       
    }
   
    @Test
    public void testHQL_35() {
        //尚未实现JPA命名的NativeSQL
       
    }
   
    public static void main(String[] args) {
        beforeClass();
    }
}

 

 

QBC:

package com.bjsxt.hibernate;

import java.util.Date;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.annotations.FetchMode;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.criterion.Restrictions;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class HibernateQLTest {
    private static SessionFactory sf;
   
    @BeforeClass
    public static void beforeClass() {
        sf = new AnnotationConfiguration().configure().buildSessionFactory();
    }
    @AfterClass
    public static void afterClass() {
        sf.close();
    }
   
    @Test
    public void testSchemaExport() {
        new SchemaExport(new AnnotationConfiguration().configure()).create(false, 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.setCategory(c);
            t.setTitle("t" + i);
            t.setCreateDate(new Date());
            session.save(t);
           
        }
       
        for(int i=0; i<10; i++) {
           
            Topic t = new Topic();
            t.setId(1);
            Msg m = new Msg();
            m.setCont("m" + i);
            m.setTopic(t);
            session.save(m);
           
        }
       
       
       
       
        session.getTransaction().commit();
        session.close();
    }
   
    //is empty and is not empty
    @Test
    public void testQBC() {
        Session session = sf.openSession();
        session.beginTransaction();
        //criterion 标准/准则/约束
        Criteria c = session.createCriteria(Topic.class) //from Topic
                    
                     .add(Restrictions.gt("id", 2)) //greater than = id > 2
                     .add(Restrictions.lt("id", 8)) //little than = id < 8
                     .add(Restrictions.like("title", "t_"))
                     .createCriteria("category")
                     .add(Restrictions.between("id", 3, 5)) //category.id >= 3 and category.id <=5
                     ;
        //DetachedCriterea
        for(Object o : c.list()) {
            Topic t = (Topic)o;
            System.out.println(t.getId() + "-" + t.getTitle());
        }
        session.getTransaction().commit();
        session.close();
       
    }
   
    public static void main(String[] args) {
        beforeClass();
    }
}

 

QBE被QBC包含,是QBC的一部分:

package com.bjsxt.hibernate;

import java.util.Date;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Restrictions;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class HibernateQLTest {
    private static SessionFactory sf;
   
    @BeforeClass
    public static void beforeClass() {
        sf = new AnnotationConfiguration().configure().buildSessionFactory();
    }
    @AfterClass
    public static void afterClass() {
        sf.close();
    }
   
    @Test
    public void testSchemaExport() {
        new SchemaExport(new AnnotationConfiguration().configure()).create(false, 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.setCategory(c);
            t.setTitle("t" + i);
            t.setCreateDate(new Date());
            session.save(t);
           
        }
       
        for(int i=0; i<10; i++) {
           
            Topic t = new Topic();
            t.setId(1);
            Msg m = new Msg();
            m.setCont("m" + i);
            m.setTopic(t);
            session.save(m);
           
        }
       
       
       
       
        session.getTransaction().commit();
        session.close();
    }
   
    //is empty and is not empty
    //query by criteria query by example
    @Test
    public void testQBE() {
        Session session = sf.openSession();
        session.beginTransaction();
        Topic tExample = new Topic();
        tExample.setTitle("T_");
       
        Example e = Example.create(tExample)
                    .ignoreCase().enableLike();
        Criteria c = session.createCriteria(Topic.class)
                     .add(Restrictions.gt("id", 2))
                     .add(Restrictions.lt("id", 8))
                     .add(e)
                     ;
                    
       
        for(Object o : c.list()) {
            Topic t = (Topic)o;
            System.out.println(t.getId() + "-" + t.getTitle());
        }
        session.getTransaction().commit();
        session.close();
       
    }
   
    public static void main(String[] args) {
        beforeClass();
    }
}

hibernate EJBQL QBC QBE的更多相关文章

  1. Hibernate的QBC检索方式

    Hibernate的QBC检索方式 一直习惯了Hibernate的HQL查询,一直也觉得挺方便,对于最近项目里出现的QBC(org.hibernate.Criteria接口)也是报着一种看看的心理,因 ...

  2. Hibernate 查询方式(HQL/QBC/QBE)汇总

    作为老牌的 ORM 框架,Hibernate 在推动数据库持久化层所做出的贡献有目共睹. 它所提供的数据查询方式也越来越丰富,从 SQL 到自创的 HQL,再到面向对象的标准化查询. 虽然查询方式有点 ...

  3. Hibernate的几种查询方式-HQL,QBC,QBE,离线查询,复合查询,分页查询

    HQL查询方式 这一种我最常用,也是最喜欢用的,因为它写起来灵活直观,而且与所熟悉的SQL的语法差不太多.条件查询.分页查询.连接查询.嵌套查询,写起来与SQL语法基本一致,唯一不同的就是把表名换成了 ...

  4. Hibernate之QBC查询与本地SQL查询

    1. QBC查询:     QBC 查询就是通过使用Hibernate提供的QueryByCriteria API 来查询对象,这种API封装了SQL语句的动态拼装,对查询提供了更加面向对象的功能接口 ...

  5. Hibernate之QBC检索和本地SQL检索

    QBC查询就是通过使用Hibernate提供的Query By Criteria API来查询对象,这种API封装了SQL语句的动态拼装,对查询提供了更加面向对象的功能接口 本地SQL查询来完善HQL ...

  6. Hibernate学习---第十节:Hibernate之QBC、样例查询&离线查询

    一.QBC (Query By Criteria) 主要有Criteria,Criterion,Oder,Restrictions类组成 1.java 代码如下: /** * 查询所有 */ @Tes ...

  7. Hibernate Restrictions QBC运算符

    HQL运算符 QBC运算符 含义 = Restrictions.eq() 等于equal <>  Restrictions.ne() 不等于not equal >  Restrict ...

  8. Hibernate 之QBC

    转自:http://blog.csdn.net/agromach/article/details/1932290 一.Hibernate 中聚合函数的使用 Criteria接口的Projections ...

  9. hibernate的QBC查询之Criteria用法

    //return (DeliverCost) super.getSession().createCriteria(getMyClass()).add(Restrictions.eq("isd ...

随机推荐

  1. PHP单例类

    单例模式按字面来看就是某一个类只有一个实例,这样做的好处还是很大的,比如说数据库的连接,我们只需要实例化一次,不需要每次都去new了,这样极大的降低了资源的耗费. 单例类至少拥有以下三种公共元素: 必 ...

  2. Codeforces 610C:Harmony Analysis(构造)

    [题目链接] http://codeforces.com/problemset/problem/610/C [题目大意] 构造出2^n个由1和-1组成的串使得其两两点积为0 [题解] 我们可以构造这样 ...

  3. 【拓扑排序】CDOJ1635 琵琶弦上说相思,当时明月在,曾照彩云归

    对于两个相邻的字符串 Si和Si+1 ,如果它们的前k-1位都相同,第k位不相同,那么,在字典序中 Si,k一定在 Si+1,k前面 建立有向边从 Si,k到 Si+1,k,进行拓扑排序 为了保证字典 ...

  4. 【FFT】HDU4609-3 idiots

    ..退化为一天两题了,药丸.. [题目大意] 给出n根木棍的长度,求从其中取出3根能组成三角形的概率. [思路] 然后枚举求前缀和,枚举最长边.假设最长边为l,先求出所有两边之和大于它的情况数.然后减 ...

  5. Perl正则表达式

    perl正则表达式就是通过一串特别设计的字符串,可以按照我们的需求匹配.替换.转化目标字符串.本文主要是对一些常用的正则表达以及语法的总结以及举例,供广大喜爱Perl的同学交流学习. 操作符: =~ ...

  6. [转] 浅谈ssh(struts,spring,hibernate三大框架)整合的意义及其精髓

      hibernate工作原理 原理: 1.读取并解析配置文件 2.读取并解析映射信息,创建SessionFactory 3.打开Sesssion 4.创建事务Transation 5.持久化操作 6 ...

  7. opensuse搜狐镜像

    openSUSE-13.1-Update-sohu-mirror http://mirrors.sohu.com/opensuse/update/13.1/ openSUSE-13.1-Oss-soh ...

  8. Apache 优化配置10条建议

    之前VPS使用的是默认的Apache配置,感觉还行,不过随着博客的人流量上升,显然这种配置无法满足需求了:下面是Apache官方手册中给出的几条优化配置建议,笔者将其整理出来,对Apache服务器的运 ...

  9. webpack与browser-sync热更新原理深度讲解

    本文首发于CSDN网站,下面的版本又经过进一步的修订.原文:webpack与browser-sync热更新原理深度讲解本文包含如下内容: webpack-hot-middleware EventSou ...

  10. phpok -- 域名问题

    nginx会改变连接的baseurl, 所以要改变nginx的server name的配置. 将网站改为静态也需配置nginx.