英文:
I don't know how to use JPQL [AND] and [OR]
问题
Book.java
@Table(name = "books")
@NamedQueries({
@NamedQuery(name = "searchNameAndAuthor", query = "SELECT b FROM Book AS b WHERE b.book_name = :book_name and b.author = :author"),
@NamedQuery(name = "searchNameAndAuthorCount", query = "SELECT count(b) FROM Book AS b WHERE b.book_name = :book_name and b.author = :author")
})
@Entity
public class Book {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "isbn_code", length = 17)
private Integer isbn_code;
@Column(name = "book_name", length = 30, nullable = false)
private String book_name;
@Column(name = "author", length = 20)
private String author;
@Column(name = "publisher", length = 20)
private String publisher;
@Column(name = "publish_date", length = 8)
private Date publish_date;
@Column(name = "stock", length = 4)
private Integer stock;
search.jsp
<c:import url="/WEB-INF/views/layout/app.jsp">
<c:param name="content">
<c:if test="${flush != null}">
<div id="flush_success">
<c:out value="${flush}"></c:out>
</div>
</c:if>
<h2>図書館ようこそ</h2>
<form method="POST" action="<c:url value='/books/search/result' />">
<label for="book_name">調べたいタイトル</label><br />
<input type="text" name="book_name" value="${book_name}" /> <br />
<br />
<label for="author">著者</label><br />
<input type="text" name="author" value="${author}" /> <br />
<br />
<button type="submit">検索</button>
</form>
</c:param>
</c:import>
请注意,上面的翻译已经去除了代码中的HTML和XML标签,只保留了文本内容。如果您需要更多的帮助或其他翻译,请随时提问。
英文:
I made application what it will be able to search book of name and author.I use JPQL to realize it.When I fill two textbox(name of book and author),it search right.But I fill one textbox,it search not found.
How to fix?
Book.java
@Table(name = "books")
@NamedQueries({
@NamedQuery(name = "searchNameAndAuthor", query = "SELECT b FROM Book AS b WHERE b.book_name =:book_name and b.author=:author"),
@NamedQuery(name = "searchNameAndAuthorCount", query = "SELECT count(b) FROM Book AS b WHERE b.book_name =:book_name and b.author=:author")
})
@Entity
public class Book {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "isbn_code", length = 17)
private Integer isbn_code;
@Column(name = "book_name", length = 30, nullable = false)
private String book_name;
@Column(name = "author", length = 20)
private String author;
@Column(name = "publisher", length = 20)
private String publisher;
@Column(name = "publish_date", length = 8)
private Date publish_date;
@Column(name = "stock", length = 4)
private Integer stock;
search.jsp
<c:import url="/WEB-INF/views/layout/app.jsp">
<c:param name="content">
<c:if test="${flush != null}">
<div id="flush_success">
<c:out value="${flush}"></c:out>
</div>
</c:if>
<h2>図書館ようこそ</h2>
<form method="POST" action="<c:url value='/books/search/result' />">
<label for="book_name">調べたいタイトル</label><br /> <input type="text"
name="book_name" value="${book_name}" /> <br />
<br />
<label for="author">著者</label><br /> <input type="text"
name="author" value="${author}" /> <br />
<br />
<button type="submit">検索</button>
</form>
</c:param>
</c:import>
答案1
得分: 1
需要处理空值情况。一种方法是检查参数是否为null,使用OR条件。
SELECT b FROM Book AS b WHERE (:book_name IS NULL OR b.book_name = :book_name)
and (:author IS NULL OR b.author = :author)
英文:
You need to handle the null case also. A way can be checking is the parameter is null as OR condition
SELECT b FROM Book AS b WHERE (:book_name IS NULL OR b.book_name =:book_name)
and (:author IS NULL OR b.author=:author)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论