---
# System prepended metadata

title: Specification 實務應用
tags: [specification, spring, jpa]

---

# Specification 實務應用

[TOC]

###### tags: `spring` `jpa` `specification`

---

## `greaterThan()`<SUP>大於</SUP>

```java=
public static Specification<EntityClass> weightGreaterThan(float weight) {
	return (root, criteriaQuery, criteriaBuilder) -> {
		return criteriaBuilder.greaterThan(
			root.get(EntityClass_.weight),
			weight
		);
	};
}
```

## `greaterThanOrEqualTo()`<SUP>大於等於</SUP>

```java=
public static Specification<EntityClass> weightGreaterThanOrEqualTo(float weight) {
	return (root, criteriaQuery, criteriaBuilder) -> {
		return criteriaBuilder.greaterThanOrEqualTo(
			root.get(EntityClass_.weight),
			weight
		);
	};
}
```

## `lessThan()`<SUP>小於</SUP>

```java=
public static Specification<EntityClass> heightLessThan(double height) {
	return (root, criteriaQuery, criteriaBuilder) -> {
		return criteriaBuilder.lessThan(
			root.get(EntityClass_.height),
			height
		);
	};
}
```

## `lessThanOrEqualTo()`<SUP>小於等於</SUP>

```java=
public static Specification<EntityClass> heightLessThanOrEqualTo(double height) {
	return (root, criteriaQuery, criteriaBuilder) -> {
		return criteriaBuilder.lessThanOrEqualTo(
			root.get(EntityClass_.height),
			height
		);
	};
}
```

## `equal`<SUP>等於</SUP>

```java=
public static Specification<EntityClass> amountEqual(int amount) {
	return (root, criteriaQuery, criteriaBuilder) -> {
		return criteriaBuilder.equal(
			root.get(EntityClass_.amount),
			amount
		);
	};
}
```

`equal()`也可應用在其它資料型態；如…

### 字符串

```java=
public static Specification<EntityClass> nicknameEqual(String string) {
	return (root, criteriaQuery, criteriaBuilder) -> {
		return criteriaBuilder.equal(
			root.get(EntityClass_.nickname),
			string
		);
	};
}
```

### 布林

```java=
public static Specification<EntityClass> verified() {
	return (root, criteriaQuery, criteriaBuilder) -> {
		return criteriaBuilder.equal(
			root.get(EntityClass_.verified),
			true
		);
	};
}
```

### 列舉

```java=
public static Specification<EntityClass> categoryEqual(Category category) {
	return (root, criteriaQuery, criteriaBuilder) -> {
		return criteriaBuilder.equal(
			root.get(EntityClass_.category),
			category
		);
	};
}
```

### 其它 entity class

```java=
public static Specification<EntityClass> classificationEqual(Classification classification) {
	return (root, criteriaQuery, criteriaBuilder) -> {
		return criteriaBuilder.equal(
			root.get(EntityClass_.classification),
			classification
		);
	};
}
```

## `not()`

```java=
public static Specification<EntityClass> classificationNot(Classification classification) {
	return (root, criteriaQuery, criteriaBuilder) -> {
		return criteriaBuilder.not(
			classificationEqual(classification)
		);
	};
}
```

## `isNull()`

```java=
public static Specification<EntityClass> classificationNull() {
	return (root, criteriaQuery, criteriaBuilder) -> {
		return criteriaBuilder.isNull(
			root.get(EntityClass_.classification)
		);
	};
}
```

## `isNotNull()`

```java=
public static Specification<EntityClass> classificationNull() {
	return (root, criteriaQuery, criteriaBuilder) -> {
		return criteriaBuilder.isNotNull(
			root.get(EntityClass_.classification)
		);
	};
}
```

## `LIKE`

## 多個條件

```java=
public static Specification<Entity> nickname(String nickname) {
	return (root, criteriaQuery, criteriaBuilder) -> {
		criteriaQuery.orderBy(
			criteriaBuilder.desc(
				root.get(Lover_.active)
			)
		);//以活跃降幂排序
		return criteriaBuilder.and(
			criteriaBuilder.equal(
				root.get(Entity_.nickname),
				string
			),//未封号
			criteriaBuilder.equal(
				root.get(Lover_.gender),
				gender
			),//性别
			criteriaBuilder.isTrue(
				root.get(Lover_.relief)
			)//通过安心认证
		);
	};
}
```

參考資料：

* [Spring Data JPA - Combining multiple Specifications](https://www.logicbig.com/tutorials/spring-framework/spring-data/combined-specifications.html)
* [Spring data jpa Specification 子查詢實現](https://www.twblogs.net/a/5d03a246bd9eee47d34bb920)
