HHH-12349 - User Guide documentation for @Filter is too verbose
This commit is contained in:
parent
dfe0bcbbca
commit
88fda0598c
|
@ -1768,19 +1768,40 @@ include::{sourcedir}/basic/WhereJoinTableTest.java[tags=mapping-where-join-table
|
|||
[[mapping-column-filter]]
|
||||
==== `@Filter`
|
||||
|
||||
The `@Filter` annotation is another way to filter out entities or collections using custom SQL criteria, for both entities and collections.
|
||||
The `@Filter` annotation is another way to filter out entities or collections using custom SQL criteria.
|
||||
Unlike the `@Where` annotation, `@Filter` allows you to parameterize the filter clause at runtime.
|
||||
|
||||
[[mapping-filter-example]]
|
||||
.`@Filter` mapping usage
|
||||
Now, considering we have the following `Account` entity:
|
||||
|
||||
[[mapping-filter-account-example]]
|
||||
.`@Filter` mapping entity-level usage
|
||||
====
|
||||
[source, JAVA, indent=0]
|
||||
----
|
||||
include::{sourcedir}/basic/FilterTest.java[tags=mapping-filter-example]
|
||||
include::{sourcedir}/basic/FilterTest.java[tags=mapping-filter-Account-example]
|
||||
----
|
||||
====
|
||||
|
||||
If the database contains the following entities:
|
||||
[NOTE]
|
||||
====
|
||||
Notice that the `active` property is mapped to the `active_status` column.
|
||||
|
||||
This mapping was done to show you that the `@Filter` condition uses a SQL condition, and not a JPQL filtering criteria.
|
||||
====
|
||||
|
||||
As already explained, we can also apply the `@Filter` annotation for collections as illustrated by the `Client` entity:
|
||||
|
||||
[[mapping-filter-client-example]]
|
||||
.`@Filter` mapping collection-level usage
|
||||
====
|
||||
[source, JAVA, indent=0]
|
||||
----
|
||||
include::{sourcedir}/basic/FilterTest.java[tags=mapping-filter-Client-example]
|
||||
----
|
||||
====
|
||||
|
||||
If we persist a `Client` with three associated `Account` entities,
|
||||
Hibernate will execute the following SQL statements:
|
||||
|
||||
[[mapping-filter-persistence-example]]
|
||||
.Persisting and fetching entities with a `@Filter` mapping
|
||||
|
@ -1797,6 +1818,21 @@ include::{extrasdir}/basic/mapping-filter-persistence-example.sql[]
|
|||
====
|
||||
|
||||
By default, without explicitly enabling the filter, Hibernate is going to fetch all `Account` entities.
|
||||
|
||||
[[mapping-no-filter-entity-query-example]]
|
||||
.Query entities mapped without activating the `@Filter`
|
||||
====
|
||||
[source, JAVA, indent=0]
|
||||
----
|
||||
include::{sourcedir}/basic/FilterTest.java[tags=mapping-no-filter-entity-query-example]
|
||||
----
|
||||
|
||||
[source, SQL, indent=0]
|
||||
----
|
||||
include::{extrasdir}/basic/mapping-no-filter-entity-query-example.sql[]
|
||||
----
|
||||
====
|
||||
|
||||
If the filter is enabled and the filter parameter value is provided,
|
||||
then Hibernate is going to apply the filtering criteria to the associated `Account` entities.
|
||||
|
||||
|
@ -1817,6 +1853,7 @@ include::{extrasdir}/basic/mapping-filter-entity-query-example.sql[]
|
|||
[IMPORTANT]
|
||||
====
|
||||
Filters apply to entity queries, but not to direct fetching.
|
||||
|
||||
Therefore, in the following example, the filter is not taken into consideration when fetching an entity from the Persistence Context.
|
||||
|
||||
[[mapping-filter-entity-example]]
|
||||
|
@ -1835,7 +1872,22 @@ As you can see from the example above, contrary to an entity query, the filter d
|
|||
====
|
||||
|
||||
Just like with entity queries, collections can be filtered as well, but only if the filter is explicitly enabled on the currently running Hibernate `Session`.
|
||||
This way, when fetching the `accounts` collections, Hibernate is going to apply the `@Filter` clause filtering criteria to the associated collection entries.
|
||||
|
||||
[[mapping-no-filter-collection-query-example]]
|
||||
.Traversing collections without activating the `@Filter`
|
||||
====
|
||||
[source, JAVA, indent=0]
|
||||
----
|
||||
include::{sourcedir}/basic/FilterTest.java[tags=mapping-no-filter-collection-query-example]
|
||||
----
|
||||
|
||||
[source, SQL, indent=0]
|
||||
----
|
||||
include::{extrasdir}/basic/mapping-no-filter-collection-query-example.sql[]
|
||||
----
|
||||
====
|
||||
|
||||
When activating the `@Filter` and fetching the `accounts` collections, Hibernate is going to apply the filter condition to the associated collection entries.
|
||||
|
||||
[[mapping-filter-collection-query-example]]
|
||||
.Traversing collections mapped with `@Filter`
|
||||
|
@ -1861,7 +1913,7 @@ The main advantage of `@Filter` over the `@Where` clause is that the filtering c
|
|||
It's not possible to combine the `@Filter` and `@Cache` collection annotations.
|
||||
This limitation is due to ensuring consistency and because the filtering information is not stored in the second-level cache.
|
||||
|
||||
If caching was allowed for a currently filtered collection, then the second-level cache would store only a subset of the whole collection.
|
||||
If caching were allowed for a currently filtered collection, then the second-level cache would store only a subset of the whole collection.
|
||||
Afterward, every other Session will get the filtered collection from the cache, even if the Session-level filters have not been explicitly activated.
|
||||
|
||||
For this reason, the second-level collection cache is limited to storing whole collections, and not subsets.
|
||||
|
@ -1873,7 +1925,7 @@ For this reason, the second-level collection cache is limited to storing whole c
|
|||
When using the `@Filter` annotation with collections, the filtering is done against the child entries (entities or embeddables).
|
||||
However, if you have a link table between the parent entity and the child table, then you need to use the `@FilterJoinTable` to filter child entries according to some column contained in the join table.
|
||||
|
||||
The `@FilterJoinTable` annotation can be, therefore, applied to a unidirectional `@OneToMany` collection as illustrate din the following mapping:
|
||||
The `@FilterJoinTable` annotation can be, therefore, applied to a unidirectional `@OneToMany` collection as illustrated in the following mapping:
|
||||
|
||||
[[mapping-filter-join-table-example]]
|
||||
.`@FilterJoinTable` mapping usage
|
||||
|
@ -1884,7 +1936,11 @@ include::{sourcedir}/basic/FilterJoinTableTest.java[tags=mapping-filter-join-tab
|
|||
----
|
||||
====
|
||||
|
||||
If the database contains the following entities:
|
||||
The `firstAccounts` filter will allow us to get only the `Account` entities that have the `order_id`
|
||||
(which tells the position of every entry inside the `accounts` collection)
|
||||
less than a given number (e.g. `maxOrderId`).
|
||||
|
||||
Let's assume our database contains the following entities:
|
||||
|
||||
[[mapping-filter-join-table-persistence-example]]
|
||||
.Persisting and fetching entities with a `@FilterJoinTable` mapping
|
||||
|
@ -1900,8 +1956,24 @@ include::{extrasdir}/basic/mapping-filter-join-table-persistence-example.sql[]
|
|||
----
|
||||
====
|
||||
|
||||
The collections can be filtered if the associated filter is enabled on the currently running Hibernate `Session`.
|
||||
This way, when fetching the `accounts` collections, Hibernate is going to apply the `@FilterJoinTable` clause filtering criteria to the associated collection entries.
|
||||
The collections can be filtered only if the associated filter is enabled on the currently running Hibernate `Session`.
|
||||
|
||||
[[mapping-no-filter-join-table-collection-query-example]]
|
||||
.Traversing collections mapped with `@FilterJoinTable` without enabling the filter
|
||||
====
|
||||
[source, JAVA, indent=0]
|
||||
----
|
||||
include::{sourcedir}/basic/FilterJoinTableTest.java[tags=mapping-no-filter-join-table-collection-query-example]
|
||||
----
|
||||
|
||||
[source, SQL, indent=0]
|
||||
----
|
||||
include::{extrasdir}/basic/mapping-no-filter-join-table-collection-query-example.sql[]
|
||||
----
|
||||
====
|
||||
|
||||
If we enable the filter and set the `maxOrderId` to `1`, when fetching the `accounts` collections, Hibernate is going to apply the `@FilterJoinTable` clause filtering criteria, and we will get just
|
||||
`2` `Account` entities, with the `order_id` values of `0` and `1`.
|
||||
|
||||
[[mapping-filter-join-table-collection-query-example]]
|
||||
.Traversing collections mapped with `@FilterJoinTable`
|
||||
|
|
|
@ -8,7 +8,7 @@ WHERE
|
|||
|
||||
SELECT
|
||||
a.id as id1_0_,
|
||||
a.active as active2_0_,
|
||||
a.active_status as active2_0_,
|
||||
a.amount as amount3_0_,
|
||||
a.client_id as client_i6_0_,
|
||||
a.rate as rate4_0_,
|
||||
|
@ -16,27 +16,5 @@ SELECT
|
|||
FROM
|
||||
Account a
|
||||
WHERE
|
||||
a.client_id = 1
|
||||
|
||||
-- Activate filter [activeAccount]
|
||||
|
||||
SELECT
|
||||
c.id as id1_1_0_,
|
||||
c.name as name2_1_0_
|
||||
FROM
|
||||
Client c
|
||||
WHERE
|
||||
c.id = 1
|
||||
|
||||
SELECT
|
||||
a.id as id1_0_,
|
||||
a.active as active2_0_,
|
||||
a.amount as amount3_0_,
|
||||
a.client_id as client_i6_0_,
|
||||
a.rate as rate4_0_,
|
||||
a.account_type as account_5_0_
|
||||
FROM
|
||||
Account a
|
||||
WHERE
|
||||
accounts0_.active = true
|
||||
accounts0_.active_status = true
|
||||
and a.client_id = 1
|
|
@ -1,6 +1,6 @@
|
|||
SELECT
|
||||
a.id as id1_0_0_,
|
||||
a.active as active2_0_0_,
|
||||
a.active_status as active2_0_0_,
|
||||
a.amount as amount3_0_0_,
|
||||
a.client_id as client_i6_0_0_,
|
||||
a.rate as rate4_0_0_,
|
||||
|
@ -9,8 +9,5 @@ SELECT
|
|||
c.name as name2_1_1_
|
||||
FROM
|
||||
Account a
|
||||
LEFT OUTER JOIN
|
||||
Client c
|
||||
ON a.client_id=c.id
|
||||
WHERE
|
||||
a.id = 2
|
|
@ -1,18 +1,6 @@
|
|||
SELECT
|
||||
a.id as id1_0_,
|
||||
a.active as active2_0_,
|
||||
a.amount as amount3_0_,
|
||||
a.client_id as client_i6_0_,
|
||||
a.rate as rate4_0_,
|
||||
a.account_type as account_5_0_
|
||||
FROM
|
||||
Account a
|
||||
|
||||
-- Activate filter [activeAccount]
|
||||
|
||||
SELECT
|
||||
a.id as id1_0_,
|
||||
a.active as active2_0_,
|
||||
a.active_status as active2_0_,
|
||||
a.amount as amount3_0_,
|
||||
a.client_id as client_i6_0_,
|
||||
a.rate as rate4_0_,
|
||||
|
@ -20,4 +8,4 @@ SELECT
|
|||
FROM
|
||||
Account a
|
||||
WHERE
|
||||
a.active = true
|
||||
a.active_status = true
|
|
@ -3,28 +3,6 @@ SELECT
|
|||
ca.accounts_id as accounts2_2_0_,
|
||||
ca.order_id as order_id3_0_,
|
||||
a.id as id1_0_1_,
|
||||
a.active as active2_0_1_,
|
||||
a.amount as amount3_0_1_,
|
||||
a.rate as rate4_0_1_,
|
||||
a.account_type as account_5_0_1_
|
||||
FROM
|
||||
Client_Account ca
|
||||
INNER JOIN
|
||||
Account a
|
||||
ON ca.accounts_id=a.id
|
||||
WHERE
|
||||
ca.Client_id = ?
|
||||
|
||||
-- binding parameter [1] as [BIGINT] - [1]
|
||||
|
||||
-- Activate filter [firstAccounts]
|
||||
|
||||
SELECT
|
||||
ca.Client_id as Client_i1_2_0_,
|
||||
ca.accounts_id as accounts2_2_0_,
|
||||
ca.order_id as order_id3_0_,
|
||||
a.id as id1_0_1_,
|
||||
a.active as active2_0_1_,
|
||||
a.amount as amount3_0_1_,
|
||||
a.rate as rate4_0_1_,
|
||||
a.account_type as account_5_0_1_
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
INSERT INTO Client (name, id)
|
||||
VALUES ('John Doe', 1)
|
||||
|
||||
INSERT INTO Account (active, amount, client_id, rate, account_type, id)
|
||||
VALUES (true, 5000.0, 1, 0.0125, 'CREDIT', 1)
|
||||
INSERT INTO Account (amount, client_id, rate, account_type, id)
|
||||
VALUES (5000.0, 1, 0.0125, 'CREDIT', 1)
|
||||
|
||||
INSERT INTO Account (active, amount, client_id, rate, account_type, id)
|
||||
VALUES (false, 0.0, 1, 0.0105, 'DEBIT', 2)
|
||||
INSERT INTO Account (amount, client_id, rate, account_type, id)
|
||||
VALUES (0.0, 1, 0.0105, 'DEBIT', 2)
|
||||
|
||||
INSERT INTO Account (active, amount, client_id, rate, account_type, id)
|
||||
VALUES (true, 250.0, 1, 0.0105, 'DEBIT', 3)
|
||||
INSERT INTO Account (amount, client_id, rate, account_type, id)
|
||||
VALUES (250.0, 1, 0.0105, 'DEBIT', 3)
|
||||
|
||||
INSERT INTO Client_Account (Client_id, order_id, accounts_id)
|
||||
VALUES (1, 0, 1)
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
INSERT INTO Client (name, id)
|
||||
VALUES ('John Doe', 1)
|
||||
|
||||
INSERT INTO Account (active, amount, client_id, rate, account_type, id)
|
||||
INSERT INTO Account (active_status, amount, client_id, rate, account_type, id)
|
||||
VALUES (true, 5000.0, 1, 0.0125, 'CREDIT', 1)
|
||||
|
||||
INSERT INTO Account (active, amount, client_id, rate, account_type, id)
|
||||
INSERT INTO Account (active_status, amount, client_id, rate, account_type, id)
|
||||
VALUES (false, 0.0, 1, 0.0105, 'DEBIT', 2)
|
||||
|
||||
INSERT INTO Account (active, amount, client_id, rate, account_type, id)
|
||||
INSERT INTO Account (active_status, amount, client_id, rate, account_type, id)
|
||||
VALUES (true, 250.0, 1, 0.0105, 'DEBIT', 3)
|
|
@ -0,0 +1,19 @@
|
|||
SELECT
|
||||
c.id as id1_1_0_,
|
||||
c.name as name2_1_0_
|
||||
FROM
|
||||
Client c
|
||||
WHERE
|
||||
c.id = 1
|
||||
|
||||
SELECT
|
||||
a.id as id1_0_,
|
||||
a.active_status as active2_0_,
|
||||
a.amount as amount3_0_,
|
||||
a.client_id as client_i6_0_,
|
||||
a.rate as rate4_0_,
|
||||
a.account_type as account_5_0_
|
||||
FROM
|
||||
Account a
|
||||
WHERE
|
||||
a.client_id = 1
|
|
@ -0,0 +1,9 @@
|
|||
SELECT
|
||||
a.id as id1_0_,
|
||||
a.active_status as active2_0_,
|
||||
a.amount as amount3_0_,
|
||||
a.client_id as client_i6_0_,
|
||||
a.rate as rate4_0_,
|
||||
a.account_type as account_5_0_
|
||||
FROM
|
||||
Account a
|
|
@ -0,0 +1,17 @@
|
|||
SELECT
|
||||
ca.Client_id as Client_i1_2_0_,
|
||||
ca.accounts_id as accounts2_2_0_,
|
||||
ca.order_id as order_id3_0_,
|
||||
a.id as id1_0_1_,
|
||||
a.amount as amount3_0_1_,
|
||||
a.rate as rate4_0_1_,
|
||||
a.account_type as account_5_0_1_
|
||||
FROM
|
||||
Client_Account ca
|
||||
INNER JOIN
|
||||
Account a
|
||||
ON ca.accounts_id=a.id
|
||||
WHERE
|
||||
ca.Client_id = ?
|
||||
|
||||
-- binding parameter [1] as [BIGINT] - [1]
|
|
@ -8,6 +8,7 @@ package org.hibernate.userguide.mapping.basic;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.EnumType;
|
||||
|
@ -45,52 +46,52 @@ public class FilterJoinTableTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void testLifecycle() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
//tag::mapping-filter-join-table-persistence-example[]
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Client client = new Client()
|
||||
.setId( 1L )
|
||||
.setName( "John Doe" );
|
||||
|
||||
client.addAccount(
|
||||
new Account()
|
||||
.setId( 1L )
|
||||
.setType( AccountType.CREDIT )
|
||||
.setAmount( 5000d )
|
||||
.setRate( 1.25 / 100 )
|
||||
);
|
||||
|
||||
client.addAccount(
|
||||
new Account()
|
||||
.setId( 2L )
|
||||
.setType( AccountType.DEBIT )
|
||||
.setAmount( 0d )
|
||||
.setRate( 1.05 / 100 )
|
||||
);
|
||||
|
||||
client.addAccount(
|
||||
new Account()
|
||||
.setType( AccountType.DEBIT )
|
||||
.setId( 3L )
|
||||
.setAmount( 250d )
|
||||
.setRate( 1.05 / 100 )
|
||||
);
|
||||
|
||||
Client client = new Client();
|
||||
client.setId( 1L );
|
||||
client.setName( "John Doe" );
|
||||
entityManager.persist( client );
|
||||
|
||||
Account account1 = new Account( );
|
||||
account1.setId( 1L );
|
||||
account1.setType( AccountType.CREDIT );
|
||||
account1.setAmount( 5000d );
|
||||
account1.setRate( 1.25 / 100 );
|
||||
account1.setActive( true );
|
||||
client.getAccounts().add( account1 );
|
||||
entityManager.persist( account1 );
|
||||
|
||||
Account account2 = new Account( );
|
||||
account2.setId( 2L );
|
||||
account2.setType( AccountType.DEBIT );
|
||||
account2.setAmount( 0d );
|
||||
account2.setRate( 1.05 / 100 );
|
||||
account2.setActive( false );
|
||||
client.getAccounts().add( account2 );
|
||||
entityManager.persist( account2 );
|
||||
|
||||
Account account3 = new Account( );
|
||||
account3.setType( AccountType.DEBIT );
|
||||
account3.setId( 3L );
|
||||
account3.setAmount( 250d );
|
||||
account3.setRate( 1.05 / 100 );
|
||||
account3.setActive( true );
|
||||
client.getAccounts().add( account3 );
|
||||
entityManager.persist( account3 );
|
||||
} );
|
||||
//end::mapping-filter-join-table-persistence-example[]
|
||||
} );
|
||||
|
||||
//tag::mapping-filter-join-table-collection-query-example[]
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
//tag::mapping-no-filter-join-table-collection-query-example[]
|
||||
Client client = entityManager.find( Client.class, 1L );
|
||||
|
||||
assertEquals( 3, client.getAccounts().size());
|
||||
//end::mapping-no-filter-join-table-collection-query-example[]
|
||||
} );
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
log.infof( "Activate filter [%s]", "firstAccounts");
|
||||
|
||||
//tag::mapping-filter-join-table-collection-query-example[]
|
||||
Client client = entityManager.find( Client.class, 1L );
|
||||
|
||||
entityManager
|
||||
|
@ -99,19 +100,28 @@ public class FilterJoinTableTest extends BaseEntityManagerFunctionalTestCase {
|
|||
.setParameter( "maxOrderId", 1);
|
||||
|
||||
assertEquals( 2, client.getAccounts().size());
|
||||
} );
|
||||
//end::mapping-filter-join-table-collection-query-example[]
|
||||
} );
|
||||
}
|
||||
|
||||
//tag::mapping-filter-join-table-example[]
|
||||
public enum AccountType {
|
||||
DEBIT,
|
||||
CREDIT
|
||||
}
|
||||
|
||||
//tag::mapping-filter-join-table-example[]
|
||||
@Entity(name = "Client")
|
||||
@FilterDef(name="firstAccounts", parameters=@ParamDef( name="maxOrderId", type="int" ) )
|
||||
@Filter(name="firstAccounts", condition="order_id <= :maxOrderId")
|
||||
@FilterDef(
|
||||
name="firstAccounts",
|
||||
parameters=@ParamDef(
|
||||
name="maxOrderId",
|
||||
type="int"
|
||||
)
|
||||
)
|
||||
@Filter(
|
||||
name="firstAccounts",
|
||||
condition="order_id <= :maxOrderId"
|
||||
)
|
||||
public static class Client {
|
||||
|
||||
@Id
|
||||
|
@ -119,34 +129,42 @@ public class FilterJoinTableTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
private String name;
|
||||
|
||||
@OneToMany
|
||||
@OneToMany(cascade = CascadeType.ALL)
|
||||
@OrderColumn(name = "order_id")
|
||||
@FilterJoinTable(name="firstAccounts", condition="order_id <= :maxOrderId")
|
||||
@FilterJoinTable(
|
||||
name="firstAccounts",
|
||||
condition="order_id <= :maxOrderId"
|
||||
)
|
||||
private List<Account> accounts = new ArrayList<>( );
|
||||
|
||||
//Getters and setters omitted for brevity
|
||||
|
||||
//end::mapping-filter-join-table-example[]
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
public Client setId(Long id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
public Client setName(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public List<Account> getAccounts() {
|
||||
return accounts;
|
||||
}
|
||||
//tag::mapping-filter-join-table-example[]
|
||||
|
||||
public void addAccount(Account account) {
|
||||
this.accounts.add( account );
|
||||
}
|
||||
}
|
||||
|
||||
@Entity(name = "Account")
|
||||
|
@ -163,49 +181,42 @@ public class FilterJoinTableTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
private Double rate;
|
||||
|
||||
private boolean active;
|
||||
|
||||
//Getters and setters omitted for brevity
|
||||
|
||||
//end::mapping-filter-join-table-example[]
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
public Account setId(Long id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public AccountType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(AccountType type) {
|
||||
public Account setType(AccountType type) {
|
||||
this.type = type;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Double getAmount() {
|
||||
return amount;
|
||||
}
|
||||
|
||||
public void setAmount(Double amount) {
|
||||
public Account setAmount(Double amount) {
|
||||
this.amount = amount;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Double getRate() {
|
||||
return rate;
|
||||
}
|
||||
|
||||
public void setRate(Double rate) {
|
||||
public Account setRate(Double rate) {
|
||||
this.rate = rate;
|
||||
}
|
||||
|
||||
public boolean isActive() {
|
||||
return active;
|
||||
}
|
||||
|
||||
public void setActive(boolean active) {
|
||||
this.active = active;
|
||||
return this;
|
||||
}
|
||||
|
||||
//tag::mapping-filter-join-table-example[]
|
||||
|
|
|
@ -8,10 +8,12 @@ package org.hibernate.userguide.mapping.basic;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.EnumType;
|
||||
import javax.persistence.Enumerated;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.NoResultException;
|
||||
|
@ -49,45 +51,43 @@ public class FilterTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void testLifecycle() {
|
||||
//tag::mapping-filter-persistence-example[]
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
|
||||
Client client = new Client();
|
||||
client.setId( 1L );
|
||||
client.setName( "John Doe" );
|
||||
//tag::mapping-filter-persistence-example[]
|
||||
Client client = new Client()
|
||||
.setId( 1L )
|
||||
.setName( "John Doe" );
|
||||
|
||||
client.addAccount(
|
||||
new Account()
|
||||
.setId( 1L )
|
||||
.setType( AccountType.CREDIT )
|
||||
.setAmount( 5000d )
|
||||
.setRate( 1.25 / 100 )
|
||||
.setActive( true )
|
||||
);
|
||||
|
||||
client.addAccount(
|
||||
new Account()
|
||||
.setId( 2L )
|
||||
.setType( AccountType.DEBIT )
|
||||
.setAmount( 0d )
|
||||
.setRate( 1.05 / 100 )
|
||||
.setActive( false )
|
||||
);
|
||||
|
||||
client.addAccount(
|
||||
new Account()
|
||||
.setType( AccountType.DEBIT )
|
||||
.setId( 3L )
|
||||
.setAmount( 250d )
|
||||
.setRate( 1.05 / 100 )
|
||||
.setActive( true )
|
||||
);
|
||||
|
||||
entityManager.persist( client );
|
||||
|
||||
Account account1 = new Account( );
|
||||
account1.setId( 1L );
|
||||
account1.setType( AccountType.CREDIT );
|
||||
account1.setAmount( 5000d );
|
||||
account1.setRate( 1.25 / 100 );
|
||||
account1.setActive( true );
|
||||
account1.setClient( client );
|
||||
client.getAccounts().add( account1 );
|
||||
entityManager.persist( account1 );
|
||||
|
||||
Account account2 = new Account( );
|
||||
account2.setId( 2L );
|
||||
account2.setType( AccountType.DEBIT );
|
||||
account2.setAmount( 0d );
|
||||
account2.setRate( 1.05 / 100 );
|
||||
account2.setActive( false );
|
||||
account2.setClient( client );
|
||||
client.getAccounts().add( account2 );
|
||||
entityManager.persist( account2 );
|
||||
|
||||
Account account3 = new Account( );
|
||||
account3.setType( AccountType.DEBIT );
|
||||
account3.setId( 3L );
|
||||
account3.setAmount( 250d );
|
||||
account3.setRate( 1.05 / 100 );
|
||||
account3.setActive( true );
|
||||
account3.setClient( client );
|
||||
client.getAccounts().add( account3 );
|
||||
entityManager.persist( account3 );
|
||||
} );
|
||||
//end::mapping-filter-persistence-example[]
|
||||
} );
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
log.infof( "Activate filter [%s]", "activeAccount");
|
||||
|
@ -124,35 +124,36 @@ public class FilterTest extends BaseEntityManagerFunctionalTestCase {
|
|||
.getSingleResult();
|
||||
}
|
||||
catch (NoResultException expected) {
|
||||
expected.fillInStackTrace();
|
||||
}
|
||||
} );
|
||||
|
||||
//tag::mapping-filter-entity-example[]
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
log.infof( "Activate filter [%s]", "activeAccount");
|
||||
|
||||
//tag::mapping-filter-entity-example[]
|
||||
entityManager
|
||||
.unwrap( Session.class )
|
||||
.enableFilter( "activeAccount" )
|
||||
.setParameter( "active", true);
|
||||
|
||||
Account account = entityManager.find( Account.class, 2L );
|
||||
assertFalse( account.isActive() );
|
||||
} );
|
||||
//end::mapping-filter-entity-example[]
|
||||
|
||||
// tag::mapping-filter-entity-query-example[]
|
||||
assertFalse( account.isActive() );
|
||||
//end::mapping-filter-entity-example[]
|
||||
} );
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
//tag::mapping-no-filter-entity-query-example[]
|
||||
List<Account> accounts = entityManager.createQuery(
|
||||
"select a from Account a", Account.class)
|
||||
.getResultList();
|
||||
|
||||
assertEquals( 3, accounts.size());
|
||||
//end::mapping-no-filter-entity-query-example[]
|
||||
} );
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
log.infof( "Activate filter [%s]", "activeAccount");
|
||||
|
||||
//tag::mapping-filter-entity-query-example[]
|
||||
entityManager
|
||||
.unwrap( Session.class )
|
||||
.enableFilter( "activeAccount" )
|
||||
|
@ -161,36 +162,41 @@ public class FilterTest extends BaseEntityManagerFunctionalTestCase {
|
|||
List<Account> accounts = entityManager.createQuery(
|
||||
"select a from Account a", Account.class)
|
||||
.getResultList();
|
||||
|
||||
assertEquals( 2, accounts.size());
|
||||
} );
|
||||
//end::mapping-filter-entity-query-example[]
|
||||
} );
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
//tag::mapping-no-filter-collection-query-example[]
|
||||
Client client = entityManager.find( Client.class, 1L );
|
||||
|
||||
assertEquals( 3, client.getAccounts().size() );
|
||||
//end::mapping-no-filter-collection-query-example[]
|
||||
} );
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
log.infof( "Activate filter [%s]", "activeAccount");
|
||||
|
||||
//tag::mapping-filter-collection-query-example[]
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Client client = entityManager.find( Client.class, 1L );
|
||||
assertEquals( 3, client.getAccounts().size() );
|
||||
} );
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
log.infof( "Activate filter [%s]", "activeAccount");
|
||||
|
||||
entityManager
|
||||
.unwrap( Session.class )
|
||||
.enableFilter( "activeAccount" )
|
||||
.setParameter( "active", true);
|
||||
|
||||
Client client = entityManager.find( Client.class, 1L );
|
||||
|
||||
assertEquals( 2, client.getAccounts().size() );
|
||||
} );
|
||||
//end::mapping-filter-collection-query-example[]
|
||||
} );
|
||||
}
|
||||
|
||||
//tag::mapping-filter-example[]
|
||||
public enum AccountType {
|
||||
DEBIT,
|
||||
CREDIT
|
||||
}
|
||||
|
||||
//tag::mapping-filter-Client-example[]
|
||||
@Entity(name = "Client")
|
||||
public static class Client {
|
||||
|
||||
|
@ -199,44 +205,67 @@ public class FilterTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
private String name;
|
||||
|
||||
@OneToMany(mappedBy = "client")
|
||||
@Filter(name="activeAccount", condition="active = :active")
|
||||
@OneToMany(
|
||||
mappedBy = "client",
|
||||
cascade = CascadeType.ALL
|
||||
)
|
||||
@Filter(
|
||||
name="activeAccount",
|
||||
condition="active_status = :active"
|
||||
)
|
||||
private List<Account> accounts = new ArrayList<>( );
|
||||
|
||||
//Getters and setters omitted for brevity
|
||||
|
||||
//end::mapping-filter-example[]
|
||||
//end::mapping-filter-Client-example[]
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
public Client setId(Long id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
public Client setName(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public List<Account> getAccounts() {
|
||||
return accounts;
|
||||
}
|
||||
//tag::mapping-filter-example[]
|
||||
}
|
||||
//tag::mapping-filter-Client-example[]
|
||||
|
||||
public void addAccount(Account account) {
|
||||
account.setClient( this );
|
||||
this.accounts.add( account );
|
||||
}
|
||||
}
|
||||
//end::mapping-filter-Client-example[]
|
||||
|
||||
//tag::mapping-filter-Account-example[]
|
||||
@Entity(name = "Account")
|
||||
@FilterDef(name="activeAccount", parameters=@ParamDef( name="active", type="boolean" ) )
|
||||
@Filter(name="activeAccount", condition="active = :active")
|
||||
@FilterDef(
|
||||
name="activeAccount",
|
||||
parameters = @ParamDef(
|
||||
name="active",
|
||||
type="boolean"
|
||||
)
|
||||
)
|
||||
@Filter(
|
||||
name="activeAccount",
|
||||
condition="active_status = :active"
|
||||
)
|
||||
public static class Account {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
@ManyToOne
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
private Client client;
|
||||
|
||||
@Column(name = "account_type")
|
||||
|
@ -247,60 +276,66 @@ public class FilterTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
private Double rate;
|
||||
|
||||
@Column(name = "active_status")
|
||||
private boolean active;
|
||||
|
||||
//Getters and setters omitted for brevity
|
||||
|
||||
//end::mapping-filter-example[]
|
||||
//end::mapping-filter-Account-example[]
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
public Account setId(Long id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Client getClient() {
|
||||
return client;
|
||||
}
|
||||
|
||||
public void setClient(Client client) {
|
||||
public Account setClient(Client client) {
|
||||
this.client = client;
|
||||
return this;
|
||||
}
|
||||
|
||||
public AccountType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(AccountType type) {
|
||||
public Account setType(AccountType type) {
|
||||
this.type = type;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Double getAmount() {
|
||||
return amount;
|
||||
}
|
||||
|
||||
public void setAmount(Double amount) {
|
||||
public Account setAmount(Double amount) {
|
||||
this.amount = amount;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Double getRate() {
|
||||
return rate;
|
||||
}
|
||||
|
||||
public void setRate(Double rate) {
|
||||
public Account setRate(Double rate) {
|
||||
this.rate = rate;
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean isActive() {
|
||||
return active;
|
||||
}
|
||||
|
||||
public void setActive(boolean active) {
|
||||
public Account setActive(boolean active) {
|
||||
this.active = active;
|
||||
return this;
|
||||
}
|
||||
|
||||
//tag::mapping-filter-example[]
|
||||
//tag::mapping-filter-Account-example[]
|
||||
}
|
||||
//end::mapping-filter-example[]
|
||||
//end::mapping-filter-Account-example[]
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue