Skip to content

Instantly share code, notes, and snippets.

@danilobatistaqueiroz
Last active September 16, 2019 19:35
Show Gist options
  • Select an option

  • Save danilobatistaqueiroz/8d02b7985bd7ee6829469179fe7b1bd8 to your computer and use it in GitHub Desktop.

Select an option

Save danilobatistaqueiroz/8d02b7985bd7ee6829469179fe7b1bd8 to your computer and use it in GitHub Desktop.
Hibernate tips
/*** one to one..many ***/
create table employee (
id integer primary key autoincrement,
name varchar(50)
);
create table project (
id integer primary key autoincrement,
name varchar(50),
leader integer not null unique,
FOREIGN KEY(leader) REFERENCES employee(id)
);
/*** one to zero..many ***/
create table team (
id integer primary key autoincrement,
name varchar(50)
);
create table tournament (
id integer primary key autoincrement,
name varchar(50),
year date,
champion integer,
FOREIGN KEY(champion) REFERENCES team(id)
);
/*** one to zero..one ***/
create table employee (
id integer primary key autoincrement,
name varchar(50)
);
create table cabinet (
id integer primary key autoincrement,
name varchar(50),
employee_id integer unique,
FOREIGN KEY(employee_id) REFERENCES employee(id)
);
/*** one to one ***/
create table person (
id integer primary key autoincrement,
name varchar(50)
);
CREATE TABLE passport(
id integer PRIMARY KEY autoincrement,
number VARCHAR(255) UNIQUE,
person_id integer UNIQUE NOT NULL,
FOREIGN KEY(person_id) REFERENCES person(id)
);
/*** zero-one to zero-one bidirectional ***/
0..1 <--> 0..1
create table employee (
id integer primary key autoincrement,
name varchar(50),
parkingspace_id integer unique,
FOREIGN KEY(parkingspace_id) REFERENCES parkinspace(id)
);
create table parkingspace(
id integer primary key autoincrement,
name varchar(50),
employee_id integer unique,
FOREIGN KEY(employee_id) REFERENCES employee(id)
);
--------
1 -> 0..1
table a (pk)
table b (pk, fk unique)
1 -> 0..m
table a (pk)
table b (pk, fk)
m -> m
table a (pk)
table b (pk)
table ab (fk, fk)
-----------------------
1 -> 1
table a (pk)
table b (pk, fk not null unique)
1 -> m
table a (pk)
table b (pk, fk not null)
-----------------------
0..1 <--> 0..1
table a (pk, fk unique)
table b (pk, fk unique)

one to one

the main difference between one to many and one to one is a unique index on the foreign key column.
one employee has only one address

image

one to many

one student may have many projects

create table student (
  id integer primary key autoincrement,
  name varchar(50)
);

create table project (
  id integer primary key autoincrement,
  title varchar(50),
  student_id integer,
  FOREIGN KEY(student_id) REFERENCES student(id)
);
public class Student {
  private Integer id;
  private String name;
  @OneToMany(mappedBy = "student", targetEntity = Project.class, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
  private Set<Project> projects;
}

public class Project {
  private Integer id;
  private String title;
  @ManyToOne
  @JoinColumn(name="student_id")
  private Student student;
}

Student.hbm.xml

<set name="projects" table="project"...>
  <one-to-many ...>

Project.hbm.xml

<many-to-one name="student" class="Student"...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment