hibernate HQL语句

HQL语句的几种处理情况
建立持久化类

public class Book {
	
	private Integer book_id;
	private String book_name;
	private Set<Category> categories = new HashSet<>();
	
	public Book() {
		
	}

	public Integer getBook_id() {
		return book_id;
	}

	public void setBook_id(Integer book_id) {
		this.book_id = book_id;
	}

	public String getBook_name() {
		return book_name;
	}

	public void setBook_name(String book_name) {
		this.book_name = book_name;
	}

	public Set<Category> getCategories() {
		return categories;
	}

	public void setCategories(Set<Category> categories) {
		this.categories = categories;
	}

	@Override
	public String toString() {
		return "Book [book_id=" + book_id + ", book_name=" + book_name + ", categories=" + categories
				+ ", getBook_id()=" + getBook_id() + ", getBook_name()=" + getBook_name() + ", getCategories()="
				+ getCategories() + ", getClass()=" + getClass() + ", hashCode()=" + hashCode() + ", toString()="
				+ super.toString() + "]";
	}
	
	

}

建立持久化映射文件

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2018-11-28 16:44:57 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
    <class name="com.zking.five.hibernate.Book" table="BOOK">
        <id name="book_id" type="java.lang.Integer">
            <column name="BOOK_ID" />
            <generator class="assigned" />
        </id>
        <property name="book_name" type="java.lang.String">
            <column name="BOOK_NAME" />
        </property>
        <set name="categories" table="CATEGORY" inverse="false" lazy="true">
            <key>
                <column name="BOOK_ID" />
            </key>
            <one-to-many class="com.zking.five.hibernate.Category" />
        </set>
    </class>
</hibernate-mapping>

建立配置文件

<session-factory>
     <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    
     <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
    
     <property name="connection.username">root</property>
     
     <property name="connection.password">1920</property>
     
     <!-- 属性方言 -->
     <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
     
     <property name="show_sql">true</property>
     
     <property name="format_sql">true</property>
     
     <property name="hbm2ddl.auto">update</property>
     <!-- 本地事务(jdbc)getCurrentSession -->
     <property name="hibernate.current_session_context_class">thread</property>
     <!-- 全局事务 (jta)-->
   <!--   <property name="hibernate.current_session_context_class">jta</property> -->
     <!-- 引入实体类的映射文件 -->
     <!--书本实体类-->
    <mapping resource="com/zking/five/hibernate/Book.hbm.xml"/>
    </session-factory>

Junit测试

  1. 返回对象(包括单个对象)
public class TestHql {
	private Session session;
	private Transaction transaction;

	@Before
	public void setUp() {
		session = HibernateUtil.getSessionFactory().openSession();
		transaction = session.beginTransaction();
	}

	/**
	 * hibernate hql语句几种处理情况 返回单个对象
	 */
	@Test
	public void test1() {
		String hql = "from Book where book_id=1";
		Query query = session.createQuery(hql);
		Object singleResult = query.getSingleResult();
		//com.zking.five.hibernate.Book
		System.out.println(singleResult.getClass().getName());
		//Book [book_id=1, book_name=西游记, categories=[com.zking.five.hibernate.Category@57f8951a, com.zking.five.hibernate.Category@531ec978], getBook_id()=1, getBook_name()=西游记, getCategories()=[com.zking.five.hibernate.Category@57f8951a, com.zking.five.hibernate.Category@531ec978], getClass()=class com.zking.five.hibernate.Book, hashCode()=817994751, toString()=com.zking.five.hibernate.Book@30c19bff]
		System.out.println(singleResult);

	}

	/**
	 * hibernate hql语句几种处理情况 返回对象
	 */
	@Test
	public void test2() {
		String hql = "from Book";
		Query query = session.createQuery(hql);
		List list = query.list();
		for (Object object : list) {
			////com.zking.five.hibernate.Book
			System.out.println(object.getClass().getName());
			System.out.println(object);
		}

	}



	
	@After
	public void tearDown() {
		transaction.commit();
		session.close();
	}

}
  1. 返回字符串
/**
	 * hibernate hql语句几种处理情况 返回字符串
	 */
	@Test
	public void test3() {
		String hql = "select book_name from Book where book_id=1";
		Query query = session.createQuery(hql);
		Object singleResult = query.getSingleResult();
		// java.lang.String
		System.out.println(singleResult.getClass().getName());
		// 西游记
		System.out.println(singleResult);
	}
  1. 返回数组
/**
	 * hibernate hql语句几种处理情况 返回数组
	 */
	@Test
	public void test4() {
		String hql = "select book_name,book_id from Book where book_id=1";
		Query query = session.createQuery(hql);
		Object singleResult = query.getSingleResult();
		// [Ljava.lang.Object;
		System.out.println(singleResult.getClass().getName());
		// [西游记, 1]
		System.out.println(Arrays.toString((Object[]) singleResult));
	}

  1. 命名参数
	/**
	 * hql 命名参数
	 */
	@Test
	public void test6() {
		Book book = new Book();
		String hql = "select * from Book where book_id like :id and book_name like :name";
		Query query = session.createQuery(hql);
	    query.setParameter("id", book.getBook_id());
	    query.setParameter("name", book.getBook_name());
	    List list = query.list();
	    for (Object object : list) {
			System.out.println(object.getClass().getName());
			System.out.println(object);
		}
	}
发布了25 篇原创文章 · 获赞 5 · 访问量 4753
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览