@org.hibernate.annotations.Where 可以标记在 Entity 或者 collections property 上。
@Where mapping usage
package org.example.demo.hibernate;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Persistence;
import org.hibernate.annotations.Where;
public class Test {
public static void main(String[] args) {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("test");
EntityManager em = factory.createEntityManager();
Client client = new Client();
Account account1 = new Account();
account1.type = AccountType.CREDIT;
account1.active = true;
account1.client = client;
client.creditAccounts.add(account1);
Account account2 = new Account();
account2.type = AccountType.DEBIT;
account2.active = false;
account2.client = client;
client.debitAccounts.add(account2);
Account account3 = new Account();
account3.type = AccountType.DEBIT;
account3.active = true;
account3.client = client;
client.debitAccounts.add(account3);
em.getTransaction().begin();
em.persist(client);
em.persist(account1);
em.persist(account2);
em.persist(account3);
em.getTransaction().commit();
em.clear();
Client c = em.find(Client.class, client.id);
// [org.example.demo.hibernate.Test$Account@6f815e7f]
System.out.println(c.creditAccounts);
// [org.example.demo.hibernate.Test$Account@263558c9]
System.out.println(c.debitAccounts);
List<Account> accounts = em.createQuery("from Account", Account.class).getResultList();
// 2
System.out.println(accounts.size());
em.close();
factory.close();
}
public enum AccountType {
DEBIT, CREDIT
}
@Entity(name = "Client")
public static class Client {
@Id
@GeneratedValue
private Long id;
@Where(clause = "account_type = 'DEBIT'")
@OneToMany(mappedBy = "client")
private List<Account> debitAccounts = new ArrayList<>();
@Where(clause = "account_type = 'CREDIT'")
@OneToMany(mappedBy = "client")
private List<Account> creditAccounts = new ArrayList<>();
}
@Entity(name = "Account")
@Where(clause = "active = true")
public static class Account {
@Id
@GeneratedValue
private Long id;
@ManyToOne
private Client client;
@Column(name = "account_type")
@Enumerated(EnumType.STRING)
private AccountType type;
boolean active;
}
}
生成的 SQL
alter table Account drop constraint FKpei4spdwejr1mbu98yngbojd8
drop table if exists Account cascade
drop table if exists Client cascade
drop sequence if exists hibernate_sequence
create sequence hibernate_sequence start 1 increment 1
create table Account (id int8 not null, active boolean not null, account_type varchar(255), client_id int8, primary key (id))
create table Client (id int8 not null, primary key (id))
alter table Account add constraint FKpei4spdwejr1mbu98yngbojd8 foreign key (client_id) references Client
select nextval ('hibernate_sequence')
select nextval ('hibernate_sequence')
select nextval ('hibernate_sequence')
select nextval ('hibernate_sequence')
insert into Client (id) values (1)
insert into Account (active, client_id, account_type, id) values (true, 1, 'CREDIT', 2)
insert into Account (active, client_id, account_type, id) values (false, 1, 'DEBIT', 3)
insert into Account (active, client_id, account_type, id) values (true, 1, 'DEBIT', 4)
select test_clien0_.id as id1_1_0_ from Client test_clien0_ where test_clien0_.id=1
select creditacco0_.client_id as client_i4_0_0_, creditacco0_.id as id1_0_0_, creditacco0_.id as id1_0_1_, creditacco0_.active as active2_0_1_, creditacco0_.client_id as client_i4_0_1_, creditacco0_.account_type as account_3_0_1_ from Account creditacco0_ where ( creditacco0_.active = true and creditacco0_.account_type = 'CREDIT') and creditacco0_.client_id=1
select debitaccou0_.client_id as client_i4_0_0_, debitaccou0_.id as id1_0_0_, debitaccou0_.id as id1_0_1_, debitaccou0_.active as active2_0_1_, debitaccou0_.client_id as client_i4_0_1_, debitaccou0_.account_type as account_3_0_1_ from Account debitaccou0_ where ( debitaccou0_.active = true and debitaccou0_.account_type = 'DEBIT') and debitaccou0_.client_id=1
select test_accou0_.id as id1_0_, test_accou0_.active as active2_0_, test_accou0_.client_id as client_i4_0_, test_accou0_.account_type as account_3_0_ from Account test_accou0_ where ( test_accou0_.active = true)