- @OrderBy - the collection is ordered upon retrieval using a child entity property
- @OrderColumn - the collection uses a dedicated order column in the collection link table
Unidirectional ordered lists
package org.example.demo.hibernate;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Persistence;
public class Test {
public static void main(String[] args) throws Exception {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("test");
EntityManager entityManager = factory.createEntityManager();
Person person = new Person();
person.phones.add(new Phone("landline", "028-234-9876"));
person.phones.add(new Phone("mobile", "072-122-9876"));
entityManager.getTransaction().begin();
entityManager.persist(person);
entityManager.flush();
person.phones.remove(0);
entityManager.getTransaction().commit();
entityManager.clear();
Person p = entityManager.find(Person.class, person.id);
// [org.example.demo.hibernate.Test$Phone@6dc1484]
System.out.println(p.phones);
entityManager.close();
factory.close();
}
@Entity(name = "Person")
public static class Person {
@Id
@GeneratedValue
private Long id;
@OneToMany(cascade = CascadeType.ALL)
@OrderBy("number")
private List<Phone> phones = new ArrayList<>();
public Person() {
}
public Person(Long id) {
this.id = id;
}
public List<Phone> getPhones() {
return phones;
}
}
@Entity(name = "Phone")
public static class Phone {
@Id
@GeneratedValue
private Long id;
private String type;
@Column(name = "`number`")
private String number;
public Phone() {
}
public Phone(String type, String number) {
this.type = type;
this.number = number;
}
public Long getId() {
return id;
}
public String getType() {
return type;
}
public String getNumber() {
return number;
}
}
}
生成 SQL
alter table Person_Phone drop constraint FKr38us2n8g5p9rj0b494sd3391
alter table Person_Phone drop constraint FK2ex4e4p7w1cj310kg2woisjl2
drop table if exists Person cascade
drop table if exists Person_Phone cascade
drop table if exists Phone cascade
drop sequence if exists hibernate_sequence
create sequence hibernate_sequence start 1 increment 1
create table Person (id int8 not null, primary key (id))
create table Person_Phone (Person_id int8 not null, phones_id int8 not null)
create table Phone (id int8 not null, "number" varchar(255), type varchar(255), primary key (id))
alter table Person_Phone add constraint UK_9uhc5itwc9h5gcng944pcaslf unique (phones_id)
alter table Person_Phone add constraint FKr38us2n8g5p9rj0b494sd3391 foreign key (phones_id) references Phone
alter table Person_Phone add constraint FK2ex4e4p7w1cj310kg2woisjl2 foreign key (Person_id) references Person
select nextval ('hibernate_sequence')
select nextval ('hibernate_sequence')
select nextval ('hibernate_sequence')
insert into Person (id) values (1)
insert into Phone ("number", type, id) values ('028-234-9876', 'landline', 2)
insert into Phone ("number", type, id) values ('072-122-9876', 'mobile', 3)
insert into Person_Phone (Person_id, phones_id) values (1, 2)
insert into Person_Phone (Person_id, phones_id) values (1, 3)
delete from Person_Phone where Person_id=1
insert into Person_Phone (Person_id, phones_id) values (1, 3)
select test_perso0_.id as id1_0_0_ from Person test_perso0_ where test_perso0_.id=1
select phones0_.Person_id as Person_i1_1_0_, phones0_.phones_id as phones_i2_1_0_, test_phone1_.id as id1_2_1_, test_phone1_."number" as number2_2_1_, test_phone1_.type as type3_2_1_ from Person_Phone phones0_ inner join Phone test_phone1_ on phones0_.phones_id=test_phone1_.id where phones0_.Person_id=1 order by test_phone1_."number"
The @OrderBy annotation can take multiple entity properties, and each property can take an ordering direction too (e.g. @OrderBy("name ASC, type DESC")).
If no property is specified (e.g. @OrderBy), the primary key of the child entity table is used for ordering.
Unidirectional @OrderColumn list
package org.example.demo.hibernate;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OrderColumn;
import javax.persistence.Persistence;
public class Test {
public static void main(String[] args) throws Exception {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("test");
EntityManager entityManager = factory.createEntityManager();
Person person = new Person();
person.phones.add(new Phone("landline", "028-234-9876"));
person.phones.add(new Phone("mobile", "072-122-9876"));
entityManager.getTransaction().begin();
entityManager.persist(person);
entityManager.getTransaction().commit();
entityManager.clear();
Person p = entityManager.find(Person.class, person.id);
// 028-234-9876
System.out.println(p.phones.get(0).number);
// 072-122-9876
System.out.println(p.phones.get(1).number);
entityManager.close();
factory.close();
}
@Entity(name = "Person")
public static class Person {
@Id
@GeneratedValue
private Long id;
@OneToMany(cascade = CascadeType.ALL)
@OrderColumn(name = "order_id")
private List<Phone> phones = new ArrayList<>();
public Person() {
}
public Person(Long id) {
this.id = id;
}
public List<Phone> getPhones() {
return phones;
}
}
@Entity(name = "Phone")
public static class Phone {
@Id
@GeneratedValue
private Long id;
private String type;
@Column(name = "`number`")
private String number;
public Phone() {
}
public Phone(String type, String number) {
this.type = type;
this.number = number;
}
public Long getId() {
return id;
}
public String getType() {
return type;
}
public String getNumber() {
return number;
}
}
}
生成 SQL
alter table Person_Phone drop constraint FKr38us2n8g5p9rj0b494sd3391
alter table Person_Phone drop constraint FK2ex4e4p7w1cj310kg2woisjl2
drop table if exists Person cascade
drop table if exists Person_Phone cascade
drop table if exists Phone cascade
drop sequence if exists hibernate_sequence
create sequence hibernate_sequence start 1 increment 1
create table Person (id int8 not null, primary key (id))
create table Person_Phone (Person_id int8 not null, phones_id int8 not null, order_id int4 not null, primary key (Person_id, order_id))
create table Phone (id int8 not null, "number" varchar(255), type varchar(255), primary key (id))
alter table Person_Phone add constraint UK_9uhc5itwc9h5gcng944pcaslf unique (phones_id)
alter table Person_Phone add constraint FKr38us2n8g5p9rj0b494sd3391 foreign key (phones_id) references Phone
alter table Person_Phone add constraint FK2ex4e4p7w1cj310kg2woisjl2 foreign key (Person_id) references Person
select nextval ('hibernate_sequence')
select nextval ('hibernate_sequence')
select nextval ('hibernate_sequence')
insert into Person (id) values (1)
insert into Phone ("number", type, id) values ('028-234-9876', 'landline', 2)
insert into Phone ("number", type, id) values ('072-122-9876', 'mobile', 3)
insert into Person_Phone (Person_id, order_id, phones_id) values (1, 0, 2)
insert into Person_Phone (Person_id, order_id, phones_id) values (1, 1, 3)
select test_perso0_.id as id1_0_0_ from Person test_perso0_ where test_perso0_.id=1
select phones0_.Person_id as Person_i1_1_0_, phones0_.phones_id as phones_i2_1_0_, phones0_.order_id as order_id3_0_, test_phone1_.id as id1_2_1_, test_phone1_."number" as number2_2_1_, test_phone1_.type as type3_2_1_ from Person_Phone phones0_ inner join Phone test_phone1_ on phones0_.phones_id=test_phone1_.id where phones0_.Person_id=1
注意,在 select 时并没有 order ,而是 Hibernate order the list in-memory after it's being fetched from the database
Bidirectional ordered lists
package org.example.demo.hibernate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Persistence;
public class Test {
public static void main(String[] args) throws Exception {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("test");
EntityManager entityManager = factory.createEntityManager();
Person person = new Person();
person.addPhone(new Phone("landline", "028-234-9876"));
person.addPhone(new Phone("mobile", "072-122-9876"));
entityManager.getTransaction().begin();
entityManager.persist(person);
entityManager.getTransaction().commit();
entityManager.clear();
Person p = entityManager.find(Person.class, person.id);
// 028-234-9876
System.out.println(p.phones.get(0).number);
// 072-122-9876
System.out.println(p.phones.get(1).number);
entityManager.close();
factory.close();
}
@Entity(name = "Person")
public static class Person {
@Id
@GeneratedValue
private Long id;
@OneToMany(mappedBy = "person", cascade = CascadeType.ALL)
@OrderBy("number")
private List<Phone> phones = new ArrayList<>();
public Person() {
}
public Person(Long id) {
this.id = id;
}
public List<Phone> getPhones() {
return phones;
}
public void addPhone(Phone phone) {
phones.add(phone);
phone.setPerson(this);
}
public void removePhone(Phone phone) {
phones.remove(phone);
phone.setPerson(null);
}
}
@Entity(name = "Phone")
public static class Phone {
@Id
@GeneratedValue
private Long id;
private String type;
@Column(name = "`number`")
private String number;
@ManyToOne
private Person person;
public Phone() {
}
public Phone(String type, String number) {
this.type = type;
this.number = number;
}
public Long getId() {
return id;
}
public String getType() {
return type;
}
public String getNumber() {
return number;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Phone phone = (Phone) o;
return Objects.equals(number, phone.number);
}
@Override
public int hashCode() {
return Objects.hash(number);
}
}
}
生成 SQL
alter table Phone drop constraint FKmw13yfsjypiiq0i1osdkaeqpg
drop table if exists Person cascade
drop table if exists Phone cascade
drop sequence if exists hibernate_sequence
create sequence hibernate_sequence start 1 increment 1
create table Person (id int8 not null, primary key (id))
create table Phone (id int8 not null, "number" varchar(255), type varchar(255), person_id int8, primary key (id))
alter table Phone add constraint FKmw13yfsjypiiq0i1osdkaeqpg foreign key (person_id) references Person
select nextval ('hibernate_sequence')
select nextval ('hibernate_sequence')
select nextval ('hibernate_sequence')
insert into Person (id) values (1)
insert into Phone ("number", person_id, type, id) values ('028-234-9876', 1, 'landline', 2)
insert into Phone ("number", person_id, type, id) values ('072-122-9876', 1, 'mobile', 3)
select test_perso0_.id as id1_0_0_ from Person test_perso0_ where test_perso0_.id=1
select phones0_.person_id as person_i4_1_0_, phones0_.id as id1_1_0_, phones0_.id as id1_1_1_, phones0_."number" as number2_1_1_, phones0_.person_id as person_i4_1_1_, phones0_.type as type3_1_1_ from Phone phones0_ where phones0_.person_id=1 order by phones0_."number"
Bidirectional @OrderColumn list
package org.example.demo.hibernate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderColumn;
import javax.persistence.Persistence;
public class Test {
public static void main(String[] args) throws Exception {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("test");
EntityManager entityManager = factory.createEntityManager();
Person person = new Person();
person.addPhone(new Phone("landline", "028-234-9876"));
person.addPhone(new Phone("mobile", "072-122-9876"));
entityManager.getTransaction().begin();
entityManager.persist(person);
entityManager.getTransaction().commit();
entityManager.clear();
Person p = entityManager.find(Person.class, person.id);
// 028-234-9876
System.out.println(p.phones.get(0).number);
// 072-122-9876
System.out.println(p.phones.get(1).number);
entityManager.close();
factory.close();
}
@Entity(name = "Person")
public static class Person {
@Id
@GeneratedValue
private Long id;
@OneToMany(mappedBy = "person", cascade = CascadeType.ALL)
@OrderColumn(name = "order_id")
private List<Phone> phones = new ArrayList<>();
public Person() {
}
public Person(Long id) {
this.id = id;
}
public List<Phone> getPhones() {
return phones;
}
public void addPhone(Phone phone) {
phones.add(phone);
phone.setPerson(this);
}
public void removePhone(Phone phone) {
phones.remove(phone);
phone.setPerson(null);
}
}
@Entity(name = "Phone")
public static class Phone {
@Id
@GeneratedValue
private Long id;
private String type;
@Column(name = "`number`")
private String number;
@ManyToOne
private Person person;
public Phone() {
}
public Phone(String type, String number) {
this.type = type;
this.number = number;
}
public Long getId() {
return id;
}
public String getType() {
return type;
}
public String getNumber() {
return number;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Phone phone = (Phone) o;
return Objects.equals(number, phone.number);
}
@Override
public int hashCode() {
return Objects.hash(number);
}
}
}
生成 SQL
alter table Phone drop constraint FKmw13yfsjypiiq0i1osdkaeqpg
drop table if exists Person cascade
drop table if exists Phone cascade
drop sequence if exists hibernate_sequence
create sequence hibernate_sequence start 1 increment 1
create table Person (id int8 not null, primary key (id))
create table Phone (id int8 not null, "number" varchar(255), type varchar(255), person_id int8, order_id int4, primary key (id))
alter table Phone add constraint FKmw13yfsjypiiq0i1osdkaeqpg foreign key (person_id) references Person
select nextval ('hibernate_sequence')
select nextval ('hibernate_sequence')
select nextval ('hibernate_sequence')
insert into Person (id) values (1)
insert into Phone ("number", person_id, type, id) values ('028-234-9876', 1, 'landline', 2)
insert into Phone ("number", person_id, type, id) values ('072-122-9876', 1, 'mobile', 3)
update Phone set order_id=0 where id=2
update Phone set order_id=1 where id=3
select test_perso0_.id as id1_0_0_ from Person test_perso0_ where test_perso0_.id=1
select phones0_.person_id as person_i4_1_0_, phones0_.id as id1_1_0_, phones0_.order_id as order_id5_0_, phones0_.id as id1_1_1_, phones0_."number" as number2_1_1_, phones0_.person_id as person_i4_1_1_, phones0_.type as type3_1_1_ from Phone phones0_ where phones0_.person_id=1