Java from getting started to giving up: Configuring Hibernate mappings using annotations

Source: Internet
Author: User

Previously, we used the configuration file to generate the code, although compared with JDBC is a lot simpler, but each time in the modification need to change both the entity class and the mapping file. Still a little bit of trouble.

So, in this article, let's say we use annotations to configure mappings on entity classes.


The first step: Create a new project, or delete the entity classes, mapping files, and mapping tags in hibernate from the previous project, and then generate the entity classes again in Dbbrowser. Such as:

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/9E/F2/wKioL1maUzmi09tQAAEWd6m-wt4387.png "title=" 11. PNG "style=" width:400px;height:455px; "width=" "vspace=" 0 "hspace=" 0 "height=" 455 "border=" 0 "alt=" Wkiol1mauzmi09tqaaewd6m-wt4387.png "/>

The options in the red box indicate that the mappings are added directly to the Pojo as annotations . Note that the hibernate version in parentheses must be 3.2 and above .

Generated entity class (author and blog) code I will not copy it up, too much content. The annotations used are as follows:

@Entity
@Table (name = "Author", Catalog = "Blogdb")

@Id
@GeneratedValue
@Column (name = "id", unique = true, Nullable = False)

@OneToMany (cascade = cascadetype.all, fetch = fetchtype.lazy, Mappedby = "Author")

@ManyToOne (fetch = Fetchtype.lazy)
@JoinColumn (name = "author_id")


These annotations are not introduced. The main explanation is the annotation of the mapping relationship.

The value of the Cascade property corresponds to several options with Javax.persistence.CascadeType.

PERSIST: Cascade when added.

MERGE: Cascade at update time.

Remove: cascade when deleting.

All: All operations are cascaded (default).

If you set a value other than all, you will find that there is no cascade at all!!! 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0041.gif "alt=" J_0041.gif "/>

Liar, LIAR,!!!!!.

So, if you only need to cascade when adding or updating, this is generally the case.

@Cascade (value = {cascadetype.save_update}), note that the cascadetype here is org.hibernate.annotations.CascadeType.

Fetch Property : whether to delay loading. Values are: Fetchtype.lazy and Fetchtype.eager.

Mappedby = "Author": equivalent to the previous inverse= "true".

Other things will have nothing to say, guess can guess the effect, haha!!! 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0028.gif "alt=" J_0028.gif "/>

"Software Thinking" blog address:

http://softi.blog.51cto.com/

http://www.cnblogs.com/yixueyuan/

Interested partners can go to the above two address to see the relevant other blog posts.

This article is from the "Software Thinking" blog, please be sure to keep this source http://softi.blog.51cto.com/13093971/1957981

Java from getting started to giving up: Configuring Hibernate mappings using annotations

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.