Односторонние и двусторонние отношения в Hibernate

, Hibernate JPA. :





  • OneToOne -





  • OneToMany -





  • ManyToOne -





  • ManyToMany -





, , , . , - Hibernate ;) (unidirectional) (bidirectional), , .





: . , , .





, . . , . Hibernate , .





. .





@Entity
@Table(name = "contacts")
public class Contact {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column
    private String type;

    @Column
    private String data;

    @ManyToOne
    private User user;
    
    //   , ,   ..
}

@Entity
@Table(name = "users")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column
    private String username;

    //   , ,   ..
}
      
      



, Hibernate , . user_id contacts.





create table contacts (
    id bigint not null auto_increment,
    data varchar(255),
    type varchar(255),
    user_id bigint,
    primary key (id)
) engine=InnoDB;
    
create table users (
    id bigint not null auto_increment,
    username varchar(128) not null,
    primary key (id)
) engine=InnoDB
      
      



Contact . , , . . user Contact User. .





@Entity
@Table(name = "contacts")
public class Contact {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column
    private String type;

    @Column
    private String data;
    
    //   , ,   ..
}

@Entity
@Table(name = "users")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column
    private String username;

    @OneToMany
    private List<Contact> contacts;

    //   , ,   ..
}
      
      



, , Hibernate , .





create table contacts (
    id bigint not null auto_increment,
    data varchar(255),
    type varchar(255),
    primary key (id)
) engine=InnoDB;

create table users (
    id bigint not null auto_increment,
    username varchar(128) not null,
    primary key (id)
) engine=InnoDB;

create table users_contacts (
    User_id bigint not null,
    contacts_id bigint not null
) engine=InnoDB;
      
      



Hibernate (join table) users_contacts, contacts, . , , Hibernate . , - .





JoinColumn contacts.





    @OneToMany
    @JoinColumn(name = "user_id")
    private List<Contact> contacts;
      
      



user_id contacts, .





- (owning side) (inverse side). .. . , , . . .





@Entity
@Table(name = "users")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column
    private String username;

    @ManyToMany
    private List<Role> roles;

    //   , ,   ..
}

@Entity
@Table(name = "roles")
public class Role {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column
    private String name;

    @ManyToMany
    private List<User> users;

    //   , ,   ..
}
      
      



. Hibernate , .





create table roles_users (
    Role_id bigint not null,
    users_id bigint not null
) engine=InnoDB;

create table users_roles (
    User_id bigint not null,
    roles_id bigint not null
) engine=InnoDB;
      
      



, . . Hibernate , , , . mappedBy. , , .





. . users Role .





    //   mappedBy -      - 
    @ManyToMany(mappedBy = "roles")
    private List<User> users;
      
      



Hibernate users_roles.





. , - (many), mappedBy @OneToMany



. ( Contact).





@Entity
@Table(name = "contacts")
public class Contact {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column
    private String type;

    @Column
    private String data;

    @ManyToOne
    private User user;
    
    //   , ,   ..
}

@Entity
@Table(name = "users")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column
    private String username;

    @OneToMany(mappedBy = "user")
    private List<Contact> contacts;

    //   , ,   ..
}
      
      



Hibernate .





! , , ! , !





, !








All Articles