Spring JPA - studiofu/brain GitHub Wiki
// contain "-"
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Id
@GeneratedValue(generator="uuid")
// contain "-"
@GenericGenerator(name="uuid2", strategy = "org.hibernate.id.UUIDGenerator")
@Id
@GeneratedValue(generator="uuid2")
// https://www.jianshu.com/p/1fd8330404cc
@GenericGenerator(name="jpa-uuid", strategy="uuid")
@Id
@GeneratedValue(generator="uuid2")
@Entity
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OneToMany(mappedBy="person", cascade=CascadeType.ALL)
private List<Address> addresses;
}
@Entity
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne
@JoinColumn(name="ADDRESS_FK")
@Column(insertable=false, updatable=false)
private Person person;
}
You would do that when the responsibility of creating/updating the
related entity in question isn't in the current entity. E.g. you have a Person and an Address.
You'd like to add insertable=false, updatable=false to the @OneToMany relationship
with the Person entity in the Address entity, simply because it's not the responsibility
of the Address entity to create or update a Person. It's the other way round.
Table User (address_id is used in table user and id is in the table Address)
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "address_id", referencedColumnName = "id")
private Address address;
Table Address
@OneToOne(mappedBy = "address")
private User user;
Table User
@OneToOne(mappedBy = "user", cascade = CascadeType.ALL)
private Address address;
Table Address
@OneToOne
@MapsId
private User user;
table employee
@OneToOne(cascade = CascadeType.ALL)
@JoinTable(name = "emp_workstation",
joinColumns =
{ @JoinColumn(name = "employee_id", referencedColumnName = "id") },
inverseJoinColumns =
{ @JoinColumn(name = "workstation_id", referencedColumnName = "id") })
private WorkStation workStation;
table workstation
@OneToOne(mappedBy = "workStation")
private Employee employee;
Table Cart
@OneToMany(mappedBy="cart")
private Set<Items> items;
Table Item
@ManyToOne
@JoinColumn(name="cart_id", nullable=false)
private Cart cart;
table student
@ManyToMany
@JoinTable(
name = "course_like",
joinColumns = @JoinColumn(name = "student_id"),
inverseJoinColumns = @JoinColumn(name = "course_id"))
Set<Course> likedCourses;
table course
@ManyToMany(mappedBy = "likedCourses")
Set<Student> likes;
@Embeddable
class CourseRatingKey implements Serializable {
@Column(name = "student_id")
Long studentId;
@Column(name = "course_id")
Long courseId;
// standard constructors, getters, and setters
// hashcode and equals implementation
}
@Entity
class CourseRating {
@EmbeddedId
CourseRatingKey id;
@ManyToOne
@MapsId("student_id")
@JoinColumn(name = "student_id")
Student student;
@ManyToOne
@MapsId("course_id")
@JoinColumn(name = "course_id")
Course course;
int rating;
// standard constructors, getters, and setters
}
table student
@OneToMany(mappedBy = "student")
Set<CourseRegistration> registrations;
table course
@OneToMany(mappedBy = "courses")
Set<CourseRegistration> registrations;
table course_registration
@ManyToOne
@JoinColumn(name = "student_id")
Student student;
@ManyToOne
@JoinColumn(name = "course_id")
Course course;
Hibernate large records set handling
https://dzone.com/articles/bulk-fetching-hibernate
JPA large records set handling
JPA / Spring Data - apply JPA specification
https://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/
JPA Criteria
https://jverhoelen.github.io/spring-data-queries-jpa-criteria-api/
https://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/
http://www.waitingforcode.com/spring-data-jpa/repositories-with-criteria-queries-in-spring-data/read
Compound Key
http://www.thejavageek.com/2014/05/01/jpa-embeddedid-example/
https://stackoverflow.com/questions/35708682/composite-primary-key-usage-in-rest-spring-boot
Join Tables
https://gigsterous.github.io/engineering/2016/09/25/spring-boot-2.html
A beginner’s guide to JPA and Hibernate Cascade Types
https://vladmihalcea.com/a-beginners-guide-to-jpa-and-hibernate-cascade-types/