[Import] Hibernate Study Notes

Source: Internet
Author: User

What is Hibernate Baidu encyclopedia http://baike.baidu.com/view/7291.html? Wtp = tt

Summary
Hibernate is an open-source object relationship ing framework that encapsulates JDBC objects in a lightweight manner, so that Java programmers can use the object programming thinking to manipulate the database as they wish. Hibernate can be used in any situation where JDBC is used. It can be used in Java client programs or Servlet/JSP Web applications. The most revolutionary is that, hibernate can replace CMP in the J2EE architecture of application EJB to fulfill the task of data persistence.

Hibernate has five core interfaces: Session, SessionFactory, Transaction, Query, and Configuration. These five core interfaces are used in any development. Through these interfaces, you can not only access persistent objects, but also control transactions.
These five core interfaces are described below.

· Session interface: The Session interface is responsible for executing the CRUD operation on the Persistent Object (the CRUD task is to complete the communication with the database, including many common SQL statements .). However, it should be noted that the Session object is non-thread-safe. At the same time, the Hibernate session is different from the HttpSession In the JSP application. Here, when we use the term session, it actually refers to the session in Hibernate, and later we will call the httsemi sion object a user session.
· SessionFactory interface: SessionFactroy interface is used to initialize Hibernate. It acts as a proxy for the data storage source and is responsible for creating Session objects. The factory model is used here. Note that SessionFactory is not lightweight, because generally, only one SessionFactory is required for a project. When you need to operate multiple databases, you can specify a SessionFactory for each database.
· Configuration interface: the Configuration interface is responsible for configuring and starting Hibernate to create the SessionFactory object. During Hibernate startup, the Configuration class instance first locates the ing document location, reads the Configuration, and then creates the SessionFactory object.
· Transaction interface: the Transaction interface is responsible for Transaction-related operations. It is optional. developers can also design and write their own underlying transaction processing code.
· Query and Criteria interfaces: Query and Criteria interfaces are used to perform various database queries. It can be expressed in HQL or SQL statements.

Proficient in Hibernate Study Notes NO.1 http://zpthomes.spaces.live.com/Blog/cns! 6AB705608FFE25A3! 334. entry

Summary
What is Hibernate? There are different interpretations from different perspectives:
1. It is a middleware that connects Java applications and relational databases.
2. It encapsulates JDBC APIs and is responsible for Java object persistence.
3. In a Layered software architecture, the persistence layer encapsulates all database access details. The business logic layer can focus on implementing business logic.
4. It is an ORM (Object Relational Model) ing tool. It can build mappings between Object-Oriented Domain Models and relational data models.

The persistence layer encapsulates data access details and provides object-oriented APIs for the business logic layer. The complete persistence layer should achieve the following objectives:
1. The code is highly reusable and can complete all database access operations.
2. Support multiple database platforms if needed.
3. It is relatively independent. when the implementation of the persistent layer changes, the implementation of the upper layer is not affected.

 

Hibernate Study Notes http://blog.csdn.net/kangwei8655990/archive/2007/04/13/1563706.aspx
Chapter 8 Introduction to Hibernate http://blog.csdn.net/weijie_search/archive/2008/03/13/2175917.aspx

Summary
10. How to learn about Hibernate
(1) Hibernate documentation
The Hibernate documentation is full of experience and best practices for designing the persistence layer. The Hibernate documentation is accurate. Most of the content is about the persistence layer design of objects, rather than simply using Hibernate.

(2) learning points
So learning about Hibernate is mainly to learn the design pattern of the persistence layer. If you have read all the Hibernate documents, you will only mention the Hibernate configuration and Hibernate class call problems all day long, I don't think such a person is actually getting started.

When learning Hibernate, you can concentrate on understanding the operating principles of Hibernate and master the principles and skills that should be grasped in the persistence layer design, these are the most reusable things for me. If you are studying Hibernate time, you will focus on how to configure and use tools to generate hbm files. If you focus on this, basically, it is equivalent to learning Hibernate.
This is because, after learning about Hibernate, it will certainly improve the programming of JDBC, not to mention the Framework Design of The Persistence Layer of the J2EE architecture, basically, it is easy to understand. Even if you change the API in the future, you don't need Hibernate. Instead, you can use JDO, Castor or something. These experiences are just the same.

(3) The essence of Hibernate is its unparalleled and Smart Object persistence layer design.
These persistence layer design experiences won't be lost because you don't need Hibernate. I learned Hibernate myself and have obviously felt that the persistence layer design capability has gained a lot of experience, these experiences can be used not only in Java, but also in. net.
Because, no matter JDO, Hibernate, TopLink, CocoBase, or Castor, or Torque, OJB, the use and configuration methods of the software can be different, but it is essentially an ORM, it is encapsulation of the persistent layer of JDBC objects, so it never changes.

 

(More comprehensive introduction) http://feelingsea.blog.hexun.com/7294376_d.html

Summary
16. An important term: Type
(1) A Type object can map a Java Type to a field in a table in the database.
Hibernate designers invented the term "Type", which is a very basic and powerful element in the entire architecture. A Type object can map a Java Type to fields in a table in the database (in fact, it can be mapped to multiple fields in the table ).

<Hibernate-mapping>
<Class name = "Event" table = "EVENTS">
<Id name = "id" column = "EVENT_ID">
<Generator class = "increment"/>
</Id>
<Property name = "date" type = "timestamp" column = "EVENT_DATE"/>
<Property name = "title"/>
</Class>
</Hibernate-mapping>

It is Hibernate mapping types. It is not Java data type, as we suppose, but also SQL database type. These types are called Hibernate mapping types. They convert data types from Java to SQL data types.
All attributes of a persistent class correspond to a type. This design idea uses Hibernate for high flexibility and scalability.


(2) about the default type
If the type is not set for the ing parameter, Hibernate will try to determine the correct type conversion and Its ing type. In some cases, this automatic detection (using the reflection mechanism on Java class) will not generate the default values you expect or need ---- therefore, we 'd better set the type.
The preceding example describes the date attribute. Hibernate cannot know which of the following types should this attribute be mapped to: SQL date, timestamp, and time. We declare the property ing timestamp to indicate that we want to save all information about date and time.

 

Corresponding table http://hi.baidu.com/sunjoe/blog/item/aa1ddabf07c5380f19d81feb.html between java, hibernate, standard SQL data types

Summary
Java data type, hibernate data type, corresponding tables between standard SQL data types

Java data type Hibernate data type standard SQL data type (PS: may be different for different databases)
Byte, java. lang. Byte byte TINYINT
Short, java. lang. Short short SMALLINT
Int, java. lang. Integer integer INGEGER
Long, java. lang. Long long BIGINT
Float, java. lang. Float float FLOAT
Double, java. lang. Double double DOUBLE
Java. math. BigDecimal big_decimal NUMERIC
Char, java. lang. Character character CHAR (1)
Boolean, java. lang. Boolean boolean BIT
Java. lang. String string VARCHAR
Boolean, java. lang. Boolean yes_no CHAR (1) ('y' or 'N ')
Boolean, java. lang. Boolean true_false CHAR (1) ('y' or 'N ')
Java. util. Date, java. SQL. Date date DATE
Java. util. Date, java. SQL. Time time TIME
Java. util. Date, java. SQL. Timestamp timestamp
Java. util. Calendar calendarTIMES TAMP
Java. util. Calendar calendar_date DATE
Byte [] binary VARBINARY, BLOB
Java. lang. String text CLOB
Java. io. Serializable serializable VARBINARY, BLOB
Java. SQL. Clob clob CLOB
Java. SQL. Blob blob
Java. lang. Class class VARCHAR
Java. util. Locale locale VARCHAR
Java. util. TimeZone timezone VARCHAR
Java. util. Currency currency VARCHAR

Hibernate 3.2 Integer --> Long

Summary
Instructions on the use of select count (*) return values in Hibernate
Since I am using Hibernate 3.2, it has been confirmed that this version has changed the previously returned Integer to Long,
Because the return value in JPA is defined as Long, Hibernate modifies the return value to ensure compatibility.

If you upgrade from Hibernate 3.0.x/3.1.x to the latest version 3.2, be sure to note that many SQL functions of version 3.2, such as count (), sum () the unique return value of has changed from Integer to Long. If you do not upgrade the code, you will get a ClassCastException.

This change is intended to be compatible with JPA and can be found in the latest document of hibernate.org.

Hibernate Team also provides a solution compatible with the original version:

Configuration classicCfg = new Configuration ();
ClassicCfg. addSqlFunction ("count", new ClassicCountFunction ());
ClassicCfg. addSqlFunction ("avg", new ClassicAvgFunction ());
ClassicCfg. addSqlFunction ("sum", new ClassicSumFunction ());
SessionFactory classicSf = classicCfg. buildSessionFactory ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.