教えてください
Profile
java
1//profileテーブルとマッピングさせる 2 3package com.example.konkatsu.domain; 4 5import java.sql.Date; 6 7import javax.persistence.Column; 8import javax.persistence.Entity; 9import javax.persistence.FetchType; 10import javax.persistence.Id; 11import javax.persistence.JoinColumn; 12import javax.persistence.ManyToOne; 13import javax.persistence.Table; 14 15import lombok.AllArgsConstructor; 16import lombok.Data; 17import lombok.NoArgsConstructor; 18 19@Data 20@NoArgsConstructor //デフォルトコンストラクタも必要な場合 21@AllArgsConstructor //すべてのフィールドを引数に持つコンストラクタを自動生成 22@Entity //エンティティ(一単位として扱われるデータのまとまり)であることを示す。指定したクラス名がテーブルとマッピングされる 23@Table(name = "profile") //接続するテーブルを指定 24public class Profile { 25 @Column(name = "user_id") 26 @Id //主キー 27 //@GeneratedValue(strategy = GenerationType.IDENTITY) //自動採番される 28 private Integer id; 29 private String name; 30 private Integer genderId; 31 private Date birthday; 32 private Integer height; 33 private Integer occupationId; 34 private Integer income; 35 private String text; 36 private byte[] image; 37 @ManyToOne(fetch = FetchType.LAZY) //@ManyToOneで多対1の関係にする 38 @JoinColumn(nullable = true, name = "user_id", insertable=false, updatable=false) //外部キーのカラム名の指定 39 private User user; 40 41}
ProfileForm
java
1//このクラスにプロフィール登録画面の<form>から送るパラメータをマッピングさせる 2 3 4package com.example.konkatsu.web; 5 6import java.sql.Date; 7 8import javax.persistence.Column; 9import javax.persistence.Id; 10import javax.validation.constraints.NotNull; 11 12import org.springframework.web.multipart.MultipartFile; 13 14import lombok.Data; 15 16@Data 17public class ProfileForm { 18 19 20 @Column(name = "user_id") 21 @Id //主キー 22 //@GeneratedValue(strategy = GenerationType.IDENTITY) //自動採番される 23 private Integer id; 24 25 26 //Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 27 //Integer userId = ((User)principal).getUserId(); 28 @NotNull 29 private String name; 30 @NotNull 31 Integer genderId; 32 @NotNull 33 private Date birthday; 34 @NotNull 35 private Integer height; 36 @NotNull 37 private Integer occupationId; 38 @NotNull 39 private Integer income; 40 @NotNull 41 private String text; 42 @NotNull 43 private MultipartFile image; 44 //ファイルアップロードをするにはMultipartFile 45 //private byte[] image; 46 47 48} 49 50 51
controller
java
1//th:action="@{/createProfile}" postからの処理 2 @PostMapping(path = "createProfile") //URLが konkatsu/createProfile となる 3 public String create(@Validated ProfileForm form, BindingResult bindingResult, Model model, 4 @AuthenticationPrincipal LoginUserDetails userDetails) throws IOException{ //@AuthenticationPrincipalをつけることでログイン中の[LoginDetails]オブジェクトを取得できる 5 6 7 System.out.println(form.getId()); 8 System.out.println(form.getName()); 9 System.out.println(form.getGenderId()); 10 System.out.println(form.getBirthday()); 11 System.out.println(form.getHeight()); 12 System.out.println(form.getOccupationId()); 13 System.out.println(form.getIncome()); 14 System.out.println(form.getText()); 15 System.out.println(form.getImage()); 16 17 18 19 20 if (bindingResult.hasErrors()){ //入力チェックの結果を確認し、エラーがある場合は一覧画面表示に戻る 21 System.out.println("エラー"); 22 return list(model); 23 } 24 25 26???????? 27 28MultipartFile uploaded = form.getImage(); 29 byte[] binaryData = uploaded.getBytes(); 30 31 System.out.println(form.getBinaryData()); 32 33 34??????? 35 36 37 Profile profile = new Profile(); 38 BeanUtils.copyProperties(form, profile);//ProfileFormをProfileにコピーする 39 profileService.create(profile, userDetails.getUser()); //ProfileをDBに追加する (ログインユーザー情報も) 40 //LoginUserDetailsオブジェクトに格納されているUser情報を取り出してProfileServiceに渡す 41 //ログイン中の[LoginUserDetails]オブジェクトに格納されているUser情報を取り出して[ProfileService]に渡す 42 return "/konkatsu/profileConfirm"; 43 44 }
フォームが画面からそれぞれのデータを送ると
コンソールに
39
佐藤太郎
1
1990-02-03
170
1
500
よろしく
org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile@23fff05
というように表示されるのですが
DBには
imageがnullとなって追加されません
byte[]に変換する?
のでしょうか?
controllerの
???
???
の間に書けばよいのでしょうか??
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/07/07 10:17
2017/07/11 05:21
2017/07/12 06:11