Hibernate annotation method realizes 1-n bidirectional Association

Source: Internet
Author: User

Since hibernate can be reverse engineered, tables can be created automatically in the database, so data dictionary content is not available here.

As an example of Weibo or blog, users and blogs belong to a one-to-many relationship, that is, the 1-n type, in the database we want to build the following relationship

Create user entity class users:

Import ***;/** * @author Barudisshu */@Entity @table (name = "T_user", schema = "", Catalog = "Db_blog") public class User im                 Plements Serializable {private int id;        User Auto ID private String username;        User name private String password; Password private collection<post> posts;        User Blog @Id @Column (name = "Id", nullable = False, Insertable = true, updatable = true) public int getId () {    return ID;    } public void setId (int id) {this.id = ID; } @Basic @Column (name = "Username", nullable = True, Insertable = true, updatable = true, length = 255) Public St    Ring GetUserName () {return username;    } public void Setusername (String username) {this.username = username; } @Basic @Column (name = "Password", nullable = True, Insertable = true, updatable = true, length = 255) Public St    Ring GetPassword () {return password; } public void SetPassword (String password) {This.password= password; }//Add Mappedby property, otherwise the intermediate table @OneToMany (cascade = cascadetype.persist,targetentity = Post.class,mappedby = "User") is created @    ("id") public collection<post> getposts () {return posts;    } public void Setposts (collection<post> posts) {this.posts = posts;        } @Override public boolean equals (Object o) {if (this = = O) return true;        if (o = = NULL | | getclass ()! = O.getclass ()) return false;        User user = (user) O;        if (id! = user.id) return false;        if (password! = null?!password.equals (User.password): User.password! = null) return false;        if (username! = null?!username.equals (user.username): User.username! = null) return false;    return true;        } @Override public int hashcode () {int result = ID;        result = * result + (username! = null? Username.hashcode (): 0);        result = * result + (password! = null? Password.hashcode (): 0);    return result; }    @Override public String toString () {return tostringbuilder.reflectiontostring (this); }}



It is worth noting that the table with the user as a foreign key automatically generates a corresponding field and is automatically created with @orderby values, the Mappedby property content is the corresponding associated field of the target entity, and hibernate automatically creates an intermediate table for user and post if there is no such attribute.

The following is a blog entity class post:

Import ***;/** * @author Barudisshu */@Entity @table (name = "T_post", schema = "", Catalog = "Db_blog") public class post IM         plements serializable{private int id;   Blog Automatic ID private String title; Blog title private Timestamp time;  Release time Private String digest;    Abstract private String mark; Tag private String content;   Blog body private Integer type;  Blog category private Integer views;      Number of views private user user;        Blog User @Id @Column (name = "Id", nullable = False, Insertable = true, updatable = true) public int getId () {    return ID;    } public void setId (int id) {this.id = ID; } @Basic @Column (name = "title", Nullable = True, Insertable = true, updatable = true, length = 255) Public Strin    G GetTitle () {return title;    public void Settitle (String title) {this.title = title; } @Basic @Column (name = "Time", Nullable = True, Insertable = true, updatable = true) public Timestamp getTime ()       { return time;    } public void SetTime (Timestamp time) {this.time = time; } @Basic @Column (name = "Digest", Nullable = True, Insertable = true, updatable = true, length =) public Str    ing Getdigest () {return digest;    } public void Setdigest (String digest) {this.digest = digest;  } @Basic @Column (name = "Mark", Nullable = True, Insertable = true, updatable = true, length = 255) Public String    Getmark () {return mark;    } public void Setmark (String mark) {This.mark = Mark; } @Basic @Column (name = "Content", Nullable = True, Insertable = true, updatable = true, length = 2147483647) Pub    Lic String getcontent () {return content;    The public void SetContent (String content) {this.content = content;        } @Basic @Column (name = "Type", Nullable = True, Insertable = true, updatable = true) public Integer GetType () {    return type;      } public void SetType (Integer type) {  This.type = type; } @Basic @Column (name = "views", Nullable = True, Insertable = true, updatable = true) the public Integer getviews ()    {return views;    } public void Setviews (Integer views) {this.views = views;    } @ManyToOne (cascade = cascadetype.persist,targetentity = user.class) public User GetUser () {return user;    public void SetUser (user user) {this.user = user;        } @Override public boolean equals (Object o) {if (this = = O) return true;        if (o = = NULL | | getclass ()! = O.getclass ()) return false;        Post post = (POST) o;        if (id! = post.id) return false;        if (content! = null?!content.equals (post.content): Post.content! = null) return false;        if (Digest! = null?!digest.equals (post.digest): Post.digest! = null) return false;        if (Mark! = null?!mark.equals (Post.mark): Post.mark! = null) return false; if (time! = null?!time.equals (post.time): Post.time! = null) return false;        if (title! = null?!title.equals (post.title): Post.title! = null) return false;        if (type! = null?!type.equals (Post.type): Post.type! = null) return false;        if (views! = null?!views.equals (post.views): Post.views! = null) return false;    return true;        } @Override public int hashcode () {int result = ID;        result = * result + (title! = null? Title.hashcode (): 0);        result = * result + (time! = null? Time.hashcode (): 0);        result = * result + (digest! = null? Digest.hashcode (): 0);        result = * result + (Mark! = null? Mark.hashcode (): 0);        result = * result + (content! = null? Content.hashcode (): 0);        result = * result + (type! = null? Type.hashcode (): 0);        result = * result + (views! = null? Views.hashcode (): 0);    return result;    } @Override Public String toString () {return tostringbuilder.reflectiontostring (this); }}



For the concept of the direction and the non-direction is essentially whether it is possible to obtain another entity through one entity, which generally requires consideration of cascading operations, if the actual project query complexity is too large, it is recommended to use HQL to operate, relative to the table when the establishment of a weak association.

Hibernate annotation method realizes 1-n bidirectional 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.