`

HQL inner join

阅读更多
package com.bjsxt.hibernate;

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

public class Customer {

	private int id;
	private String name;
	private Set<Order> orders = new HashSet<Order>();
	
	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;
	}
	public Set<Order> getOrders() {
		return orders;
	}
	public void setOrders(Set<Order> orders) {
		this.orders = orders;
	}	
	
}



package com.bjsxt.hibernate;

public class Order {

	private int id;
	private String number;
	private Customer customer;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		this.number = number;
	}
	public Customer getCustomer() {
		return customer;
	}
	public void setCustomer(Customer customer) {
		this.customer = customer;
	}	
	
}



<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
	<class name="com.bjsxt.hibernate.Customer" table="t_customers">
		<id name="id">
			<generator class="native"></generator>
		</id>
		
		<property name="name"></property>
		<set name="orders">
			<key column="customerId"></key>
			<one-to-many class="com.bjsxt.hibernate.Order"/>
		</set>
    </class>
	
</hibernate-mapping>


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
	<class name="com.bjsxt.hibernate.Order" table="t_orders">
		<id name="id">
			<generator class="native"></generator>
		</id>
		
		<property name="number"></property>
		<many-to-one name="customer" column="customerId" class="com.bjsxt.hibernate.Customer" cascade="save-update" />
    </class>
</hibernate-mapping>

package com.bjsxt.test;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import com.bjsxt.hibernate.Customer;
import com.bjsxt.hibernate.Order;

public class HibernateORMappingTest {
	private static SessionFactory sessionFactory;
	
	@BeforeClass
	public static void beforeClass() {
			sessionFactory = new Configuration().configure().buildSessionFactory();
	}
	@AfterClass
	public static void afterClass() {
		sessionFactory.close();
	}
	
	@Test
	public void testSave() {
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		Customer customer = new Customer();
		customer.setName("rose");
		Order order = new Order();
		order.setNumber("2011012401");
		order.setCustomer(customer);
		session.save(order);
		tx.commit();
		session.close();
	}
	
	@Test
	public void testQuery() {
		Session session = sessionFactory.openSession();
		String hql = "from Customer c inner join c.orders as o where c.id=1";
		Query query = session.createQuery(hql);
		List<Object[]> result = query.list();
		for(Object[] obj : result) {
			Customer customer = (Customer)obj[0];
			Order order = (Order)obj[1];
			System.out.print(customer.getId() + "  " + customer.getName() + "  "
					+ order.getNumber());
			System.out.println();
		}
	}
	
	@Test
	public void testQuery2() {
		Session session = sessionFactory.openSession();
		String hql = "select c.id,c.name,o.number from Customer c inner join c.orders as o";
		Query query = session.createQuery(hql);
		List<Object[]> result = query.list();
		for(Object[] row : result) {
			for(int i = 0; i < row.length; i++) {
				System.out.print(row[i] + "  ");
			}
			System.out.println();
		}
	}
	
	@Test
	public void testSchemaExport() {
		new SchemaExport(new Configuration().configure()).create(false, true);
	}
	
	public static void main(String[] args) {
		beforeClass();
	}
}






  • 大小: 52.5 KB
  • 大小: 41.5 KB
分享到:
评论

相关推荐

    hql语句大全hql语句大全

    这表示,将`Cat`实体与`mate`和`kittens`关联的实体进行连接,其中`INNER JOIN`确保只有当关联实体存在时才返回`Cat`实体,而`LEFT OUTER JOIN`则保证即使没有关联实体,`Cat`实体也会被返回。 ##### 4. FETCH子句 ...

    HQL详解

    其中,`INNER JOIN`、`LEFT OUTER JOIN`和`RIGHT OUTER JOIN`可以简化为: ``` FROM Cat AS cat JOIN cat.mate AS mate LEFT JOIN cat.kittens AS kitten ``` 此外,`FETCH`连接允许在一个选择语句中同时初始化父...

    HQL查询语言基础知识

    HQL 中的 JOIN 语句用于关联多个表,包括 INNER JOIN、LEFT JOIN、RIGHT JOIN 和 FULL JOIN。 * INNER JOIN:select employee.ID as id1,employee.Name as name1,department.ID as id2,department.Name as name2 ...

    hibernateHQL关联查询

    1. **内连接(Inner Join)**:`INNER JOIN` 或 `JOIN` ```java String hql = "SELECT s.stuName, e.examScore FROM Student s JOIN Exam e ON s.stuID = e.stuID"; ``` 2. **左外连接(Left Outer Join)**:`...

    HQL语言教程[归类].pdf

    HQL支持多种类型的连接,包括内连接(`inner join`)、左外连接(`left outer join`)、右外连接(`right outer join`)和全连接(`full join`)。简写形式如`join`和`left join`也得到广泛应用。例如,`from Cat as cat ...

    hql语句经典教程

    连接类型参照ANSI SQL,包括`inner join`、`left outer join`、`right outer join`和`full join`。简写形式如`join`和`left join`也是允许的。 ### 4. `with`关键字 `with`关键字允许你在连接时添加额外条件,例如...

    HQL查询语言基础.

    这里的JOIN条件是`employee.DepNo = department.ID`,它等价于SQL中的INNER JOIN。 **LEFT (OUTER) JOIN**: LEFT JOIN(或LEFT OUTER JOIN)返回左表(Employee)的所有记录,即使右表(Department)中没有匹配的...

    HQL连接查询和注解使用总结

    - 示例:`SELECT e FROM Employee e INNER JOIN Department d ON e.departmentId = d.id` - **迫切内连接(Eager Inner Join)** - 语法:`[inner]join fetch` - 描述:不仅指定了连接查询方式,还指定了关联...

    Hibernate查询语言(HQL)

    - **隐式 JOIN**:在 HQL 中,通过属性引用的方式进行 JOIN 称为隐式 JOIN。 ```sql FROM com.example.Cat AS cat WHERE cat.mate.name LIKE '%s%'; ``` 隐式 JOIN 实际上相当于 SQL 中的 INNER JOIN。 ####...

    HQL Hibernate查询语言

    语句 inner join、left outer join 以及 right outer join 可以简写。例如:from Cat as cat join cat.mate as mate left join cat.kittens as kitten。 此外,还有一个"fetch"连接允许仅仅使用一个选择语句就将相...

    Hibernate之HQL查询

    HQL支持内连接(INNER JOIN)、左连接(LEFT JOIN)等,可以方便地处理关联关系。例如: ```sql SELECT s, c FROM Student s LEFT JOIN s.courses c ``` 这个查询将返回每个学生及其所有关联的课程。 **6. HQL的聚合...

    HQL语句大全

    - 示例:`from Cat as cat join cat.mate as mate`,这里的`join`等同于`inner join`。 - **连接类型**: - `inner join`(内连接) - `left outer join`(左外连接) - `right outer join`(右外连接) - `...

    常用的hql语句用法解析

    还有左外连接(`left outer join`)、右外连接(`right outer join`)和全连接(`full join`),但全连接在HQL中并不常用。简写形式如`join`和`left join`可用于内连接和左外连接。`fetch`连接允许在查询中初始化相...

    Hibernate HQL.doc

    HQL支持四种类型的连接:inner join(内连接)、left outer join(左外连接)、right outer join(右外连接)和full join(全连接)。简写形式如`join`和`left join`也是允许的。`fetch`连接用于一次性初始化相关联...

    Hibernate-HQL语句多对多写法

    2. 在多对多关系的情况下,需要使用 left join 或 inner join 来连接实体类。 3. 使用 where 子句可以过滤出特定的数据。 4. 在 HQL 语句中,使用 join 子句可以连接多个实体类。 5. 在多对多关系的情况下,需要使用...

    另类查询 Hibernate HQL 深度历险

    例如,查询所有`Customer`的`Order`,即使`Customer`可能有子类,可以写成`from Customer c inner join c.orders`,HQL会自动处理多态性。 总的来说,HQL提供了一种强大的工具,使得开发者无需直接操作SQL,也能...

    Hibernate_HQL大全

    4. **JOIN与ASSOCIATION**:HQL支持INNER JOIN、LEFT OUTER JOIN、RIGHT OUTER JOIN和FULL JOIN四种连接类型。通过JOIN关键字,HQL能够处理实体间的关联,从而实现对关联实体的查询。例如,“JOIN cat.mate as mate...

    Hibernate hql查询语法总结

    select a.id, b.id from A a inner join B b on a.id = b.id ``` 等价于SQL中的`INNER JOIN`。查询结果通常为`Object[]`数组,可以通过自定义封装将其转换为更具体的对象。 总之,Hibernate HQL提供了丰富的查询...

    HIBERNATE HQL

    - 支持多种连接类型,包括 `inner join`(内连接)、`left outer join`(左外连接)、`right outer join`(右外连接)和 `full join`(全连接)。其中,`full join` 不太常用。 - `join` 关键字还可以简写,例如 `...

    HQL语句的用法

    HQL支持多种类型的关联查询,如内连接(inner join)、左连接(left join)、右连接(right join)和全连接(full join)。例如,`from Order as o inner join o.products as p`表示对Order与Product之间的关联进行...

Global site tag (gtag.js) - Google Analytics