Hibernate (7) component ing and multi-to-one ing, hibernate multiple-to-one

Source: Internet
Author: User

Hibernate (7) component ing and multi-to-one ing, hibernate multiple-to-one

I. Component ing

Configure component ing with annotations:

Husband Is A ing Class. wife is a part of this class (the property cannot be the same as the property in husband. Do not write the Entity annotation or have a primary key)

Husband class: (add annotation @ Embedded on the getWife () method)

package cn.orlion.hibernate.model;import javax.persistence.Embedded;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;@Entitypublic class Husband {        private int id;        private String name;        private Wife wife;        @Id    @GeneratedValue    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    @Embedded    public Wife getWife() {        return wife;    }    public void setWife(Wife wife) {        this.wife = wife;    }}

Wife class:

package cn.orlion.hibernate.model;public class Wife {        private String wifename;    public String getWifename() {        return wifename;    }    public void setWifename(String wifename) {        this.wifename = wifename;    }}

Table creation statement:

create table Husband (        id integer not null auto_increment,        name varchar(255),        wifename varchar(255),        primary key (id)    )

 

2. Multi-to-one ing

1. Multi-to-one unidirectional ing

Annotation mode configuration: (A group has multiple users, each user corresponds to a group, and the user is multiple parties, and the group is one)

Group class:

Package cn. orlion. hibernate. model; import javax. persistence. entity; import javax. persistence. generatedValue; import javax. persistence. id; import javax. persistence. table;/** a group has multiple users. Each user corresponds to a group, with multiple users and one group */@ Entity @ Table (name = "t_group ") public class Group {private int id; private String name; @ Id @ GeneratedValue public int getId () {return id;} public void setId (int id) {this. id = id;} public String getName () {return name;} public void setName (String name) {this. name = name ;}}

 

User class:

package cn.orlion.hibernate.model;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.ManyToOne;import javax.persistence.Table;@Entity@Table(name="t_user")public class User {    private int id;        private String name;        private Group group;    @Id    @GeneratedValue    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    @ManyToOne    public Group getGroup() {        return group;    }    public void setGroup(Group group) {        this.group = group;    }}

 

SQL statement generated by hibernate:

create table t_group (        id integer not null auto_increment,        name varchar(255),        primary key (id)    )create table t_user (        id integer not null auto_increment,        name varchar(255),        group_id integer,        primary key (id)    )alter table t_user         add constraint FK_e5f24mh6aryt9hsy99oydps6g         foreign key (group_id)         references t_group (id)

 

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.