Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

and many more ....




编者按:Hibernate ORM(简称Hibernate)是一个对象-关系映射框架,用于将面向对象的域模型转换为传统的关系数据库。Hibernate通过将直接的持久性相关的数据库访问替换掉,而高级对象处理功能来替换,解决了对象-关系间不匹配的问题。 Hibernate是最流程的Java框架之一。为此,我们在Java Code Geeks提供了大量的教程,点击这里查看它们。 现在,我们想要创建一个独立的、具有参考性的文章,来提供一个关于如何使用Hibernate的框架,并且帮助你快速开启你的Hibernate应用开发。开始享受吧!  目录 1. 简介 2. 项目搭建 3. 基础知识 3.1. SessionFactory 和Session 3.2. 事务(Transactions) 3.3. 表(Tables) 4. 继承(Inheritance) 5. 关系(Relationships) 5.1. 一对一 5.2. 一对多 5.3. 多对多 5.4. 组件(Components) 6. 用户自定义数据类型 7. 拦截器(Interceptors) 8. 源码下载 简介 Hibernate是Java世界里最流程的对象/关系映射框架(ORM)之一。它允许开发者将普通的Java类的对象结构,映射到数据库中的关系结构。借助于ORM框架,我们的一些工作,比如将内存中的对象实例存储到持久化的数据库,和加载这些数据到原来的对象结构,都变得更加容易。 同时,这些ORM解决方案,比如Hibernate,旨在从那些用于存储数据的数据库产品中建立抽象层。这样,就能够面向不同的数据库产品使用相同的Java代码,而不需面向不同的数据库编写不同的代码,来处理其中细微的差别。 Hibernate也是一个JPA提供者,这意味着它实现了Java持久化API(JPA)。JPA是一个独立于厂商的规范,用于定义Java对象和关系型数据表的映射关系。在终结系列中另一篇文章已经详细解释了JPA,所以我们在本文将重点介绍Hibernate,因此,我们也不会使用JPA注解,而是使用Hibernate专有的配置文件。 Hibernate由三个不同的组件组成: 实体(Entities):Java类(普通Java对象),用于在Hibernate中映射到关系型数据库系统的表。 对象-关系元数据(Object-Relational metadata):实体和关系型数据库的映射信息,可以通过注解(Java 1.5开始支持)实现,或者使用传统的基于XML的配置文件。这些配置信息,用于在运行时执行Java对象和数据库数据的存储和恢复。 Hibernate查询语言(HQL):使用Hibernate时,发送到数据库的查询,不必是原生的SQL格式,而是使用Hibernate专有的查询语言。这些查询语句,会在运行时被翻译成当前应用正在使用的数据库对应方言;所以说,HQL格式的语句是独立于特定数据库供应商的。 在本教程,我们将解释这个框架的各个方面,同时开发一个简单的Java SE应用程序,功能主要包括存储和检索关系型数据库的数据。我们将要使用到的类库/环境如下: maven >= 3.0 :编译环境 Hibernate(4.3.8.Final) H2 (1.3.176):关系型数据库 项目搭建 第一步,我们通过命令行创建一个简单的Maven项目: mvn archetype:create -DgroupId=com.javacodegeeks.ultimate -DartifactId=hibernate 这个命令会在文件系统中生成如下结构: |-- src | |-- main | ...

Read More »

JSP Tutorial – The ULTIMATE Guide


EDITORIAL NOTE: JavaServer Pages (JSP) technology allows you to easily create web content that has both static and dynamic components. JSP technology makes available all the dynamic capabilities of Java Servlet technology but provides a more natural approach to creating static content. The main features of JSP technology include a language for developing JSP pages, which are text-based documents that ...

Read More »

Continuous Integration and Delivery with Docker


Continuous delivery is all about reducing risk and delivering value faster by producing reliable software in short iterations. As Martin Fowler says, you actually do continuous delivery if:                 Your software is deployable throughout its lifecycle. Your team prioritizes keeping the software deployable over working on new features. Anybody can get fast, automated ...

Read More »

If you do it do it right


This is a philosophical or ethical command. Very general. It is something like “fail fast”. The reason it came up to my mind is that I wanted to compile and release License3j using Java 8 and JavaDoc refused to compile during release build. This package is a simple license manager, which has some established user base who require that I ...

Read More »

Groovier Spring Boot Integration Testing


Recently I had a chance to use Groovy’s groovy.json.JsonSlurper in a soapUI REST project. The usage scenario in soapUI (in particular, soapUI Open Source) was very simple: in a Groovy assertion, load the content of a response into a variable and than verify it like this: import groovy.json.JsonSlurper; def slurper = new JsonSlurper() def result = slurper.parseText(messageExchange.responseContent) assert result.incidentType == ...

Read More »

Transforming Collections


Did you ever want to substitute the equals and hashCode methods a HashSet or HashMap uses? Or have a List of some element type masquerade as a List of a related type? Transforming collections make that possible and this post will show how. Overview Transforming collections are a feature of LibFX 0.3.0, which will be released any day now. This ...

Read More »

Type Safe Queries for JPA’s Native Query API


When you’re using JPA – sometimes – JPQL won’t do the trick and you’ll have to resort to native SQL. From the very beginning, ORMs like Hibernate kept an open “backdoor” for these cases and offered a similar API to Spring’s JdbcTemplate, to Apache DbUtils, or to jOOQ for plain SQL. This is useful as you can continue using your ...

Read More »

Monolithic core vs full microservice architecture


Martin Fowler recently released another article on microservices, specifically about the hype surrounding them. He states that though microservices are a hot topic right now, they add unnecessary complexity to systems that would do just fine with a single monolithic application built with good modularity. While I agree with his point that microservices do add complexity, especially when it comes ...

Read More »

A Few Thoughts on Unit Test Scaffolding


When I start to repeat myself in unit test methods by creating the same objects and preparing the data to run the test, I feel disapointed in my design. Long test methods with a lot of code duplication just don’t look right. To simplify and shorten them, there are basically two options, at least in Java: 1) private properties initialized ...

Read More »
Want to take your Java Skills to the next level?
Grab our programming books for FREE!
  • Save time by leveraging our field-tested solutions to common problems.
  • The books cover a wide range of topics, from JPA and JUnit, to JMeter and Android.
  • Each book comes as a standalone guide (with source code provided), so that you use it as reference.
Last Step ...

Where should we send the free eBooks?

Good Work!
To download the books, please verify your email address by following the instructions found on the email we just sent you.