Hibernate -- use annotations to configure mappings, and hibernate -- Annotations

Source: Internet
Author: User

Hibernate -- use annotations to configure mappings, and hibernate -- Annotations

Preface:

  You can configure the ing between object classes and databases in either of the following ways:

1. Use *. hbm. xml;

2. Use @ Annotation

 

I. annotation method:

1. @ Entity

Before the class, declare the class as a persistent class.

2. @ Table

Before the class, declare the associated table name for the class. If the table name and class name are the same, you can omit them.

Eg: @ Table (name = "tbl_role ")

  

3. @ Proxy

Before the class, modify the category-level Indexing policy. The default value is delayed loading.

Eg: @ Proxy (lazy = false) // change to load now

  

4. @ ID

Add it before the get/set Method of the primary key, or add it before the property to supplement the property:

@ GeneratedValue-primary key auto-increment policy,

@ GeneratedValue (Strategy = GenerationType. Auto). The default value is Auto.

Generally used with @ GenericGenerator:

Eg: @ GenaratedValue (generator = "native_name ")

@ GenericGenerator (name = "native_name", strategy = "native") // The primary key generation policy is native.

Note that the value of generator is consistent with that in name.

     

5. @ Column

If the attribute name and the corresponding database table column name are different, or the attribute name corresponding to the get/set method is not the column name, you need to add it before the get/set method, or add it before the attribute

Eg: @ Column (name = "RoleName ")

 

  Association:

===

6. @ ManyToOne -------- multi-to-one (foreign key) Association

Eg: @ ManyToOne (targetEntity = class_name.class)

@ JoinColumn (name = "c_name") // multiple pairs of foreign key column names

 

 

7. @ onetoworkflow -------- one-to-Multiple Association attributes

Eg: @ onetoty (targetEntity = class_name.class, mappedBy = "property_name", cascade = {CascadeType. ALL })

If the Set is not generic, you need to add targetEntity mappedBy to the attribute associated with targetEntity to maintain the foreign key relationship.

 

8. @ manytoyun-many-to-many

Eg: @ manytoty (targetEntity = Function. class)

@ JoinTable (name = "role_function_r", joinColumns = @ JoinColumn (name = "roleId"), inverseJoinColumns = @ JoinColumn (name = "functionId "))

In the figure (1), it indicates the name of the intermediate table with many-to-many relationships, and (2) it indicates the foreign key relationship between the intermediate table and the current object-class table. (3) indicates the foreign key relationship between the intermediate table and another table.

 

===

9. @ Transcient

Add it before the get/set method, or add it before the property to declare the property does not need to be persisted.

 

  

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