Favorite.java
java
1package com.example.konkatsu.domain; 2 3import javax.persistence.Column; 4import javax.persistence.EmbeddedId; 5import javax.persistence.Entity; 6import javax.persistence.FetchType; 7import javax.persistence.JoinColumn; 8import javax.persistence.ManyToOne; 9import javax.persistence.Table; 10 11import lombok.AllArgsConstructor; 12import lombok.Data; 13import lombok.NoArgsConstructor; 14 15@Data 16@NoArgsConstructor 17@AllArgsConstructor 18@Entity 19@Table(name = "favorite") 20 21public class Favorite { 22 23 @EmbeddedId //複合キー 24 @Column(name = "user_id") 25 private Integer userId; 26 27 @Column(name = "favorite_user_id") 28 private Integer favoriteUserId; 29 30 //FavoriteとUserをjoin 31 //cascadeはDB操作を実行する方に設定 32 @ManyToOne(fetch = FetchType.LAZY) 33 @JoinColumn(nullable = true, name = "user_id") 34 private User user; 35 36} 37
FavoriteRepository.java
java
1package com.example.konkatsu.repository; 2 3import javax.transaction.Transactional; 4 5import org.springframework.data.jpa.repository.JpaRepository; 6import org.springframework.data.jpa.repository.Modifying; 7import org.springframework.data.jpa.repository.Query; 8import org.springframework.stereotype.Repository; 9 10import com.example.konkatsu.domain.Favorite; 11 12@Repository 13public interface FavoriteRepository extends JpaRepository<Favorite, Integer>{ 14 15 16 17 //お気に入りに追加 18 @Modifying //メソッドがUPDATE文であることを明示 19 @Transactional 20 @Query(value = "INSERT INTO FAVORITE (USER_ID,FAVORITE_USER_ID) VALUES (?1, ?2)", nativeQuery = true) 21 void createFavorite(Integer userId, Integer favoriteUserId); 22 23 24 //お気に入りを解除 25 @Query(value = "DELETE FROM FAVORITE WHERE USER_ID = ?1 AND FAVORITE_USER_ID = ?2", nativeQuery = true) 26 void deleteFavorite(Integer userId, Integer favoriteUserId); 27 28}
html
1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org"> 3<head> 4<meta charset="utf-8" /> 5<title>婚活サイト</title> 6<link rel="stylesheet" 7 href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" 8 th:href="@{/webjars/bootstrap/3.3.7/css/bootstrap.min.css}" /> 9<link rel="stylesheet" 10 href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" 11 th:href="@{/webjars/bootstrap/3.3.7/css/bootstrap-theme.min.css}" /> 12</head> 13 14<body th:with="user=${#authentication.principal.user}"> 15 <!-- authenticationで認証ユーザー情報にアクセスできる 、principalプロパティでUserDetailsオブジェクトにアクセスできるので、user.mailでログインユーザー名が分かる--> 16 17 <h2> 18 <span th:text="${user.mail}">メアド</span> さん、ログイン中 19 </h2> 20 21 22 <table class="table table-striped table-bordered table-condensed"> 23 <h2>登録ユーザー 一覧</h2> 24 <tr> 25 <th>写真</th> 26 <th>名前</th> 27 <th>性別</th> 28 <th>誕生日</th> 29 <th>身長(cm)</th> 30 <th>職業</th> 31 <th>年収(万円)</th> 32 <th>メールアドレス</th> 33 <th>自己紹介</th> 34 </tr> 35 36 <tr th:each="profile : ${profiles}"> 37 <!-- エンコードした画像の表示 --> 38 <td><img th:src="${'data:image/png;base64,' + profile.image}" 39 width="250" height="150" /></td> 40 <td th:text="${profile.name}">山田</td> 41 <td th:text="${profile.gender}">男</td> 42 <td th:text="${profile.birthday}">誕生日</td> 43 <td th:text="${profile.height}">170</td> 44 <td th:text="${profile.occupationName}">職業</td> 45 <td th:text="${profile.income}">年収</td> 46 <td th:text="${profile.mail}">メール</td> 47 <!-- 「profile」オブジェクトがもつ「user」の「mail」を表示 --> 48 <td th:text="${profile.text}">自己紹介</td> 49 <td> 50 <form th:action="@{/konkatsu/createFavorite}" method="post"> 51 <input type="hidden" name="userId" th:value="${user.userId}" /> <input 52 type="hidden" name="favoriteUserId" th:value="${profile.id}" /> 53 <button type="submit">☆</button> 54 (お気に入りにする) 55 </form> 56 </td> 57 <td> 58 <form th:action="@{/konkatsu/deleteFavorite}" method="post"> 59 <input type="hidden" name="userId" th:value="${user.userId}" /> <input 60 type="hidden" name="favoriteUserId" th:value="${profile.id}" /> 61 <button type="submit">×</button> 62 (お気に入りをやめる) 63 </form> 64 </td> 65 </tr> 66 </table> 67 68 <div th:fragment='paginationbar'> 69 <ul> 70 <li th:class="${page.first} ? 'disabled':''" style="display: inline"> 71 <span th:if="${page.first}">←先頭</span> <a th:if="${not page.first}" 72 th:href="@{${url}(searchName=${param.searchName},genderId=${param.genderId}, page=0)}">←先頭</a> 73 </li> 74 75 <!-- #numbers.sequenceは引数に指定した2つの数値の範囲で配列を生成 --> 76 <li th:each='i : ${#numbers.sequence(0, page.totalPages-1)}' 77 th:class="(${i}==${page.number})? 'active' : ''" 78 style="display: inline"><span th:if='${i}==${page.number}' 79 th:text='${i+1}'>1</span> <a th:if='${i}!=${page.number}' 80 th:href="@{${url} (searchName=${param.searchName}, genderId=${param.genderId}, page=${i})}"> 81 <span th:text='${i+1}'>1</span> 82 </a></li> 83 <li th:class="${page.last} ? 'disabled':''" style="display: inline"> 84 <span th:if="${page.last}">末尾➝</span> <a th:if="${not page.last}" 85 th:href="@{${url}(searchName=${param.searchName}, genderId=${param.genderId}, page=(${page.totalPages}-1))}">末尾➝</a> 86 </li> 87 </ul> 88 </div> 89 90 <form th:action="@{/konkatsu/myProfile}" method="get"> 91 <input type="hidden" name="id" th:value="${user.userId}" /> 92 <button type="submit" class="btn btn-lg btn-primary btn-block">Myページへ</button> 93 </form> 94 95</body> 96</html> 97 98
お気に入りを解除しようとすると
Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Tue Aug 01 14:17:54 JST 2017 There was an unexpected error (type=Internal Server Error, status=500). could not extract ResultSet; nested exception is org.hibernate.exception.GenericJDBCException: could not extract ResultSet
が画面上に
コンソール上に
いかなる結果も、クエリによって返されませんでした。
と出ます。
ちなみにDBを見ると
ちゃんと削除されています
どこがおかしいのでしょうか??
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。