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