forked from attacomsian/code-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
13e61d0
commit 7666afc
Showing
6 changed files
with
218 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
spring-data-jpa/jpa-queries/src/main/java/com/attacomsian/jpa/named/domains/Book.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package com.attacomsian.jpa.named.domains; | ||
|
||
import javax.persistence.*; | ||
import java.util.Objects; | ||
|
||
@Entity | ||
@NamedQueries({ | ||
@NamedQuery(name = "Book.findAllJPQL", | ||
query = "SELECT b FROM Book b ORDER BY b.title DESC"), | ||
@NamedQuery(name = "Book.findByTitleJPQL", | ||
query = "SELECT b FROM Book b WHERE b.title = ?1"), | ||
@NamedQuery(name = "Book.findByTitleAndPagesGreaterThanJPQL", | ||
query = "SELECT b FROM Book b WHERE b.title = :title AND b.pages > :pages") | ||
}) | ||
@NamedNativeQueries({ | ||
@NamedNativeQuery(name = "Book.findAllNative", | ||
query = "SELECT * FROM book b ORDER BY b.title DESC", | ||
resultClass = Book.class), | ||
@NamedNativeQuery(name = "Book.findByIsbnNative", | ||
query = "SELECT * FROM book b WHERE b.isbn = :isbn", | ||
resultClass = Book.class) | ||
}) | ||
public class Book { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.AUTO) | ||
private Long id; | ||
private String title; | ||
@Column(unique = true) | ||
private String isbn; | ||
private int pages; | ||
|
||
public Book() { | ||
} | ||
|
||
public Book(String title, String isbn, int pages) { | ||
this.title = title; | ||
this.isbn = isbn; | ||
this.pages = pages; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public void setTitle(String title) { | ||
this.title = title; | ||
} | ||
|
||
public String getIsbn() { | ||
return isbn; | ||
} | ||
|
||
public void setIsbn(String isbn) { | ||
this.isbn = isbn; | ||
} | ||
|
||
public int getPages() { | ||
return pages; | ||
} | ||
|
||
public void setPages(int pages) { | ||
this.pages = pages; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Book book = (Book) o; | ||
return id.equals(book.id); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(id); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Book{" + | ||
"id=" + id + | ||
", title='" + title + '\'' + | ||
", isbn='" + isbn + '\'' + | ||
", pages=" + pages + | ||
'}'; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...-jpa/jpa-queries/src/main/java/com/attacomsian/jpa/named/repositories/BookRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.attacomsian.jpa.named.repositories; | ||
|
||
import com.attacomsian.jpa.named.domains.Book; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.CrudRepository; | ||
import org.springframework.data.repository.query.Param; | ||
|
||
import java.util.List; | ||
|
||
public interface BookRepository extends CrudRepository<Book, Long> { | ||
|
||
// named queries declared in `jpa-named-queries.properties` file | ||
|
||
List<Book> findAllNamedFile(); | ||
|
||
List<Book> findByTitleNamedFile(String title); | ||
|
||
Book findByIsbnNamedFile(String isbn); | ||
|
||
@Query(nativeQuery = true) | ||
List<Book> findByTitleNativeNamedFile(@Param("title") String title); | ||
|
||
// named queries declared in `orm.xml` file | ||
|
||
List<Book> findAllXML(); | ||
|
||
List<Book> findByTitleContainingXML(String title); | ||
|
||
@Query(nativeQuery = true) | ||
List<Book> findByIsbnNativeXML(@Param("isbn") String isbn); | ||
|
||
// named queries declared with `@NamedQuery` | ||
|
||
List<Book> findAllJPQL(); | ||
|
||
List<Book> findByTitleJPQL(String title); | ||
|
||
List<Book> findByTitleAndPagesGreaterThanJPQL(@Param("title") String title, @Param("pages") int pages); | ||
|
||
// named queries declared with `@NamedNativeQuery` | ||
|
||
@Query(nativeQuery = true) | ||
List<Book> findAllNative(); | ||
|
||
@Query(nativeQuery = true) | ||
List<Book> findByIsbnNative(@Param("isbn") String isbn); | ||
} |
11 changes: 11 additions & 0 deletions
11
spring-data-jpa/jpa-queries/src/main/resources/META-INF/jpa-named-queries.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# find all books order by title descending | ||
Book.findAllNamedFile=SELECT b FROM Book b ORDER BY b.title DESC | ||
|
||
# find books by title | ||
Book.findByTitleNamedFile=SELECT b FROM Book b WHERE b.title = ?1 | ||
|
||
# find a book by isbn | ||
Book.findByIsbnNamedFile=SELECT b FROM Book b WHERE b.isbn = ?1 | ||
|
||
# native SQL query to find books by title | ||
Book.findByTitleNativeNamedFile=SELECT * FROM book b WHERE b.title = :title |
23 changes: 23 additions & 0 deletions
23
spring-data-jpa/jpa-queries/src/main/resources/META-INF/orm.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<entity-mappings version="2.0" xmlns="http://java.sun.com/xml/ns/persistence/orm" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm | ||
http://java.sun.com/xml/ns/persistence/orm_2_0.xsd "> | ||
|
||
<!--find all books order by pages descending--> | ||
<named-query name="Book.findAllXML"> | ||
<query>SELECT b FROM Book b ORDER BY b.pages DESC</query> | ||
</named-query> | ||
|
||
<!--find books by title--> | ||
<named-query name="Book.findByTitleContainingXML"> | ||
<query>SELECT b FROM Book b WHERE b.title LIKE ?1</query> | ||
</named-query> | ||
|
||
<!--native SQL query to find a book by isbn--> | ||
<named-native-query name="Book.findByIsbnNativeXML" | ||
result-class="com.attacomsian.jpa.named.domains.Book"> | ||
<query>SELECT * FROM book b WHERE b.isbn = :isbn</query> | ||
</named-native-query> | ||
|
||
</entity-mappings> |