Basic Hibernate ing and hibernate ing

Source: Internet
Author: User

Basic Hibernate ing and hibernate ing

Before talking about Hibernate ing, let's first take a look at object link ORM ing ORM. The implementation idea of ORM is to map the data of tables in relational databases into objects and present them as objects. In this way, developers can convert database operations to operations on these objects. Let's look at a picture.

From this figure, we can see that the business entity is relational data in the database, and the object in the memory. Applications can easily process objects, but it is difficult to process relational data. ORM maps relational data and object data. It can automatically generate SQL statements through ing relationships and act as a bridge between the business logic layer and the data layer.

Hibernate ing


Hibernate File
  1. Ing Class (*. java): it describes the structure of the database table. Fields in the table are described as attributes in the class. In the future, you can map the records in the table to the objects of this class.
  2. Ing file (*. hbm. xml): it specifies the relationship between database tables and ing classes, includes mappings between ing classes and database tables, between table fields and class attribute types, and between table fields and Class Attribute names.
  3. Database configuration file (*. properties /*. cfg. xml): it specifies the connection information required to connect to the database, such as the database to be connected, the user name to log on to the database, the login password, and the connection string. Of course, you can also put the address ing information of the ing class here.


Basic ing:

Specific actions

1 ing object class

// The Importance of the default null constructor public class User {public User () {// TODO Auto-generated constructor stub} public User (String id, String name) {this. id = id; this. name = name;} private String id; public String getId () {return id;} public void setId (String id) {this. id = id;} public String getName () {return name;} public void setName (String name) {this. name = name;} public String getPassword () {return password;} public void setPassword (String password) {this. password = password;} public Date getCreateTime () {return createTime;} public void setCreateTime (Date createTime) {this. createTime = createTime;} public Date getExprieTime () {return exprieTime;} public void setExprieTime (Date exprieTime) {this. exprieTime = exprieTime;} private String name; private String password; private Date createTime; private Date exprieTime ;}

Design Principles of entity classes:

* Implement the default constructor without Parameters

* Provides an identifier.

* We recommend that you do not use final to modify the object class (because the object class will be inherited to generate a proxy object when loading data with load delay)

* It is recommended to generate the getter and setter methods for the object class (if not used, use the attribute field identifier)

2. User ing file User. hbm. xml

<? Xml version = "1.0"?> <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" ""> 

3hibernate. cfg. xml configuration file

<! DOCTYPE hibernate-configuration PUBLIC "-// Hibernate/Hibernate Configuration DTD 3.0 // EN" ""> Type of the primary key generation policy of Hibernate

Manual: Assigned

Hibernate active: uuid

Database Interaction:

To interact with the database to generate IDS: guid, identity, sequence, native, foreign

Note: You need to interact with the database to generate a query.

Guid, identity: MySQL, SQLserver Generation Method

Sequence: Generation Method of Oracle and db2, auto-incrementing sequence

Native: identity + sequence, cross-platform

Foreign: Only applicable to one-to-one association ing based on shared primary keys. That is, the primary key of an object is generated by referring to the primary key of another table.


Basic Hibernate ing of Hibernate: the focus is on understanding the primary key generation policy. You can select different methods based on different databases.

Hibernate ing and java Problems

1. hibernate ing is mapped through the configuration file.
One-to-one ing
2. The comment is "//".

3. Unknown

4. Design Mode
1. FACTORY-try to catch up with MM. The chicken wings of McDonald's and the chicken wings of KFC are what MM loves. Although the taste is different, whether you take MM to McDonald's or KFC, just say "four chicken wings" to the waiter. McDonald's and KFC are Factory for chicken wings production.

Factory model: the customer class and the factory class are separated. A consumer needs a product at any time and only requests from the factory. Consumers can accept new products without modification. The disadvantage is that the factory class must be modified when the product is modified. For example, how to create and provide data to clients.

2, BUILDER-MM is the most love to hear is "I love you" this sentence, to see different places of MM, to be able to use their dialect to say this sentence to her oh, I have a multi-language translation machine with a key for each language above. To see MM, I only need to press the corresponding key, it will be able to say "I love you" in the corresponding language, and foreign MM can also easily develop, This is my "I love you" builder. (This must be better than the US military's translation server in Iraq)

Construction mode: separates the internal appearance of a product from the product generation process, so that a building process generates product objects with different internal appearances. The construction mode allows the product's internal appearance to change independently, so that the customer does not have to know the details of the product's internal composition. The construction model enforces a step-by-step construction process.

3. factory method-ask MM to go to McDonald's for a hamburger. Different MM has different tastes. It is annoying to remember every one. I usually use the Factory Method mode, take the MM to the waiter and say "I want a hamburger". What kind of hamburger should I ask the MM to tell the waiter directly.

Factory method mode: the Core factory category is no longer responsible for the creation of all products. Instead, it submits the specific creation work to the subclass to become an abstract factory role, it is only responsible for providing the interface that must be implemented by a specific factory class, without touching the details of which product class should be instantiated.

4. PROTOTYPE-chat with MM via QQ, so I must talk about some affectionate words. I have collected a lot of emotional words, and I just need to copy the words and put them in QQ, this is my prototype. (100 yuan a copy, do you want)

Original Model mode: specify the type of the object to be created by giving a prototype object, and then create more objects of the same type by copying the prototype object. The original model mode allows you to dynamically add or remove product classes. The product class does not need to have any predefined level structure. The original model mode applies to any level structure. The disadvantage is that each class must have a clone method.

5. SINGLETON-I have 6 beautiful wives. My husband is me. I am Sigleton, our husband. All they need to say is "husband, that's me (I had a dream just now. How can this be a good thing)

Singleton mode: Singleton mode ensures that a class has only one instance, and instantiate the instance and provides the singleton mode to the entire system. The Singleton mode can only be used when there is a real "Single Instance" demand.

[B: 9ceca65206] structural mode [/B: 9ceca65206]

6. ADAPTER-I met Sarah, a beautiful girl from Hong Kong at a friend's party. But I can't speak Cantonese, and she can't speak Mandarin. I have to turn to my friend kent for help, as an Adapter between me and Sarah, Sarah and I can talk to each other (I don't know if he will play with me)

Adapter (transformer) mode: converts an interface of a class into another interface that the client expects, so that the two classes that cannot work together due to interface mismatch can work together. The adaptation class can return a suitable instance to the client based on the parameters.

7. BRIDGE-if you encounter MM in the morning, you need to talk about the remaining full text in the morning.>

Hibernate relationship ing

Generally, this is quite common. If it is not said that this is not commonly used, one-to-one method can be used less often. Let's talk about my understanding below. I should try to be more popular.
Here are a few examples to understand:
1. One-to-one. For example, if a table has more than 100 fields and 40 other tables may be useful, you can split the table into two tables, in addition, the data in both tables is a record corresponding to a record, and the id of another corresponding table in one table is used to maintain the relationship between the two records, because these two records are originally a record, I don't know. Do you understand this?
2. one-to-many, such as department table and user table. Does a department have multiple users, but a single user has only one department? It is clear.
3. Multiple-to-one is equivalent to reversing the Department table and user table.
4. many-to-many is actually the relationship between one-to-many and multiple-to-one. For example, a user table and a permission table have multiple permissions, one permission corresponds to multiple users at the same time.
5. One-way and two-way is just the concept of hibernate. For example, if you use table A to maintain table B's data, but table B cannot maintain Table, this is a single item. It should be explained in two-way. Both tables can manage each other's data. For book points, a single item is managed by the parent class, and the Child class cannot be managed, and the two-way parent class can be managed.
All the relationships proposed by hibernate are configured accordingly. In fact, these relationships exist in the database, but hibernate implements these relationships through different configurations, one-way and two-way configurations are also available. Of course, these functions can be implemented without the hibernate configuration. You can maintain this relationship in the Code. Single-way and two-way, similar concepts are not found in the database, hibernate proposed this feature, and we can complete their restrictions through configuration. Think about it. If our code only provides the user table management department table function, rather than the Department management user method, is this similar to the one-way concept of hibernate?
As long as the time is limited, some of the projects are accumulated by myself. Some areas may not be deeply understood, including

Related Article

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: 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.