- 浏览: 108891 次
- 性别:
- 来自: 大连
文章分类
- 全部博客 (159)
- 前端开发 (6)
- jsp (4)
- jquery (17)
- 配置文件 (2)
- Spring (10)
- java (34)
- hibernate (9)
- VC++ win32 application (6)
- C++ (6)
- MFC文件夹对话框 (1)
- C (4)
- MFC (1)
- C# (3)
- 两个路由器怎么连接 (1)
- Maven3 (3)
- Servlet (10)
- Struts2 (6)
- jdom (1)
- Struts1 (1)
- Spring MVC (2)
- Ant (5)
- SSH (6)
- oracle 10g (1)
- ibatis (3)
- JPA (3)
- EJB (2)
- XML知识 (3)
- javascript (2)
- android (1)
- FreeMarker (1)
- dwr (1)
- Ext (10)
- EXCEL (1)
最新评论
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(); } }
发表评论
-
hibernate_1300_one2many_uni
2013-03-27 15:02 922package com.bjsxt.hibernate; ... -
Hibernate3.3.2_多对一单向关联
2013-03-27 14:25 667package com.bjsxt.hibernate; ... -
hibernate_1700_one2many_many2one_bi_crud(5)
2012-11-21 20:21 628hibernate delete -
hibernate load 方法与get方法
2012-11-21 09:43 7591.get()采用立即加载方式,而load()采用延迟加载; ... -
hibernate_1700_one2many_many2one_bi_crud(4)
2012-11-19 23:23 700还是那个例子 package com.bit.hibern ... -
hibernate_1700_one2many_many2one_bi_crud(3)
2012-11-19 22:28 651还是group和user的one to many many t ... -
hibernate_1700_one2many_many2one_bi_crud(2)
2012-11-19 22:21 571package com.bit.hibernate; imp ... -
hibernate_1700_one2many_many2one_bi_crud
2012-11-14 20:50 712这是一个hibernate 多对一 一对多 基于外键双向关联 ...
相关推荐
这表示,将`Cat`实体与`mate`和`kittens`关联的实体进行连接,其中`INNER JOIN`确保只有当关联实体存在时才返回`Cat`实体,而`LEFT OUTER JOIN`则保证即使没有关联实体,`Cat`实体也会被返回。 ##### 4. FETCH子句 ...
其中,`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 中的 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 ...
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支持多种类型的连接,包括内连接(`inner join`)、左外连接(`left outer join`)、右外连接(`right outer join`)和全连接(`full join`)。简写形式如`join`和`left join`也得到广泛应用。例如,`from Cat as cat ...
连接类型参照ANSI SQL,包括`inner join`、`left outer join`、`right outer join`和`full join`。简写形式如`join`和`left join`也是允许的。 ### 4. `with`关键字 `with`关键字允许你在连接时添加额外条件,例如...
这里的JOIN条件是`employee.DepNo = department.ID`,它等价于SQL中的INNER JOIN。 **LEFT (OUTER) JOIN**: LEFT JOIN(或LEFT OUTER JOIN)返回左表(Employee)的所有记录,即使右表(Department)中没有匹配的...
- 示例:`SELECT e FROM Employee e INNER JOIN Department d ON e.departmentId = d.id` - **迫切内连接(Eager Inner Join)** - 语法:`[inner]join fetch` - 描述:不仅指定了连接查询方式,还指定了关联...
- **隐式 JOIN**:在 HQL 中,通过属性引用的方式进行 JOIN 称为隐式 JOIN。 ```sql FROM com.example.Cat AS cat WHERE cat.mate.name LIKE '%s%'; ``` 隐式 JOIN 实际上相当于 SQL 中的 INNER JOIN。 ####...
语句 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支持内连接(INNER JOIN)、左连接(LEFT JOIN)等,可以方便地处理关联关系。例如: ```sql SELECT s, c FROM Student s LEFT JOIN s.courses c ``` 这个查询将返回每个学生及其所有关联的课程。 **6. HQL的聚合...
- 示例:`from Cat as cat join cat.mate as mate`,这里的`join`等同于`inner join`。 - **连接类型**: - `inner join`(内连接) - `left outer join`(左外连接) - `right outer join`(右外连接) - `...
还有左外连接(`left outer join`)、右外连接(`right outer join`)和全连接(`full join`),但全连接在HQL中并不常用。简写形式如`join`和`left join`可用于内连接和左外连接。`fetch`连接允许在查询中初始化相...
HQL支持四种类型的连接:inner join(内连接)、left outer join(左外连接)、right outer join(右外连接)和full join(全连接)。简写形式如`join`和`left join`也是允许的。`fetch`连接用于一次性初始化相关联...
2. 在多对多关系的情况下,需要使用 left join 或 inner join 来连接实体类。 3. 使用 where 子句可以过滤出特定的数据。 4. 在 HQL 语句中,使用 join 子句可以连接多个实体类。 5. 在多对多关系的情况下,需要使用...
例如,查询所有`Customer`的`Order`,即使`Customer`可能有子类,可以写成`from Customer c inner join c.orders`,HQL会自动处理多态性。 总的来说,HQL提供了一种强大的工具,使得开发者无需直接操作SQL,也能...
4. **JOIN与ASSOCIATION**:HQL支持INNER JOIN、LEFT OUTER JOIN、RIGHT OUTER JOIN和FULL JOIN四种连接类型。通过JOIN关键字,HQL能够处理实体间的关联,从而实现对关联实体的查询。例如,“JOIN cat.mate as mate...
select a.id, b.id from A a inner join B b on a.id = b.id ``` 等价于SQL中的`INNER JOIN`。查询结果通常为`Object[]`数组,可以通过自定义封装将其转换为更具体的对象。 总之,Hibernate HQL提供了丰富的查询...
- 支持多种连接类型,包括 `inner join`(内连接)、`left outer join`(左外连接)、`right outer join`(右外连接)和 `full join`(全连接)。其中,`full join` 不太常用。 - `join` 关键字还可以简写,例如 `...
HQL支持多种类型的关联查询,如内连接(inner join)、左连接(left join)、右连接(right join)和全连接(full join)。例如,`from Order as o inner join o.products as p`表示对Order与Product之间的关联进行...