Hibernate5 (13) annotation map [5] One-to-one shared primary Key association

Source: Internet
Author: User

Share primary key one-to-one

Below we will explain the shared primary key configuration directly through an example:
Primary KEY Master: article

 PackageCom.zeng2.model;@Table(name ="T_article2")@Entity Public  class article {    @Id    @GeneratedValue(strategy = Generationtype.auto)PrivateInteger ID;PrivateString title;@OneToOne(cascade = Cascadetype.all,fetch = Fetchtype.lazy,optional =false)@PrimaryKeyJoinColumn//Configure shared primary key, otherwise additional foreign key association columns are generated    PrivateArticlecontent articlecontent;//Ignore Get and set methods}

Reference primary Key party: Articlecontent. This is the key configuration location where the shared primary key is associated

 PackageCom.zeng2.model;@Table(name ="T_article_content")@Entity Public  class articlecontent {    @Id    @GenericGenerator(name ="ForeignKey",//Generator nameStrategy ="foreign",//using Hibernate for foreign key policiesParameters =@Parameter(Value ="article", name ="Property"))////Specifies the primary key of the class where the article in the member property is the primary key for this class, where the parameter property name must be    @GeneratedValue(Generator ="ForeignKey")//Use the ID generator defined above    PrivateInteger ID;@Lob    PrivateString content;//If you attempt to form a one-way association without this annotation, an exception is thrown,    //Because the shared primary key is set here whether the Mapperby is configured to abandon the maintenance association relationship has lost its role.     @OneToOne(cascade = Cascadetype.all)@PrimaryKeyJoinColumn//If this annotation is not added, Hibernate generates a article_id property by default in the database    PrivateArticle article;//Ignore Get and set methods

Here are our test methods:

new Article();article.setTitle("title"new ArticleContent();articleContent.setContent("content");article.setArticleContent(articleContent);articleContent.setArticle(article);//必须双方设置session.save(article);

Here are a few things to keep in mind:

  1. When setting the property ID must pay attention to the length of the field, as I use Oracle's sequence to generate ID, its length is 14 bits long, you should choose Hibernate type Long, corresponding entity should choose long, so there is no overflow situation.

  2. It's important to note that there is a one-to-one relationship between the two tables when testing, so we can't just write articleContent.setArticle(article); and ignore the articleContent.setArticle(article); attempted to assign IDs from null one-to-one when inserting. : Address error.

  3. If you do not write cascade= "all" or cascade= "none", even if it is written article.setArticleContent(articleContent); and articleContent.setArticle(article); will not happen, only the user will be stored.

  4. One-to-one efficiency problems ——-one-to-one when querying, always find the table associated with the main table, and one-to-one's lazy property is only false proxy no-proxy three kinds, no true. Outer-join= "False" is only an increase in the number of query statements, the original SQL statement into a number of lines. So in the case of one-to-one relationship one-to-one (one-to-one relationship is always to find out a few of these related tables), or in the case of multiple one-to-one in a single table, Use the best one-to-many to replace one-to-one.

Hibernate5 (13) annotation map [5] One-to-one shared primary Key association

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.