One-way and two-way relationships in Hibernate

We all know the answer to the question of what relationships between entities in Hibernate and JPA can be . There are only four options:





  • OneToOne - one to one





  • OneToMany - one to many





  • ManyToOne - many to one





  • ManyToMany - many to many





Each of the relationships has its own annotation and, it would seem, you can end the conversation here, but it's not that simple. Anyway, can there be something simple in Hibernate ;) Each of the above relations can be unidirectional or bidirectional, and if you do not take this into account, you can face a lot of problems and oddities.





For example, let's take two simple entities: a user and a contact. Obviously, each contact is associated with a user in a many-to-one relationship, and a user with contacts in a one-to-many relationship.





Unilateral relationship

A one-way relationship is a relationship owned by only one of the two parties. Hence the name. It should be noted that the other side knows nothing about this relationship. Hibernate will consider the entity in which the relationship annotation will be delivered as the owner of the relationship.





Let's try to make the owner of the relationship the contact side. The entities will look like this.





@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;

    //   , ,   ..
}
      
      



If you run this code, Hibernate will create the following table structure, which looks quite familiar to us. The relationship between the tables is created using the user_id reference field in the contacts table .





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 .





! , , ! , !





Perhaps there will be a sequel!








All Articles