[SpringBoot2.4.3] エンティティの連携

複数のテーブルが関連して動かしたい場合は、「アソシエーション」という機能によりエンティティ同士を連携して処理する

@OneToOne,@OneToMany, @ManyToOne, @ManyToMany

package com.example.demo;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.NotEmpty;

@Entity
@Table(name = "msgdata")
public class MsgData {
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column
	@NotNull
	private long id;
	
	@Column
	private String title;
	
	@Column(nullable = false)
	@NotEmpty
	private String message;
	
	@ManyToOne
	private MyData mydata;
	
	public MsgData() {
		super();
		mydata = new MyData();
	}
	
	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 getMessage() {
		return message;
	}
	
	public void setMessage(String message) {
		this.message = message;
	}
	
	public MyData getMyData() {
		return mydata;
	}
	
	public void setMydata(MyData mydata) {
		this.mydata = mydata;
	}
}

MsgDataRepository.java

package com.example.demo.repositories;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.example.demo.MsgData;

@Repository
public interface MsgDataRepository extends JpaRepository<MsgData, Long>{

}

MsgDataDao.java

package com.example.demo;

import java.io.Serializable;
import java.util.List;

public interface MsgDataDao<T> {
	
	public List<MsgData> getAll();
	public MsgData findById(long id);
}

MsgDataDaoImpl.java

package com.example.demo;

import java.util.List;
import javax.persistence.EntityManager;

import org.springframework.stereotype.Repository;

@SuppressWarnings("rawtypes")
@Repository
public class MsgDataDaoImpl implements MsgDataDao<MsgDataDao>{
	
	private EntityManager entityManager;
	
	public MsgDataDaoImpl() {
		super();
	}
	public MsgDataDaoImpl(EntityManager manager) {
		entityManager = manager;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<MsgData> getAll(){
		return entityManager
				.createQuery("from MsgData")
				.getResultList();
	}
	
	@Override
	public MsgData findById(long id) {
		return (MsgData)entityManager
				.createQuery("from MsgData where id = "
						+ id).getSingleResult();
	}

}

showMsgData.html

	<h1 th:text="${title}">MyMsg page</h1>
	<p th:text="${msg}"></p>
	<table>
		<form method="post" action="/msg" th:object="${formModel}">
		<input type="hidden" name="id" th:value="*{id}">
		<tr>
			<td><label for="title">タイトル</label></td>
			<td><input type="text" name="title" th:value="*{title}"></td>
		</tr>
		<tr>
			<td><label for="message">メッセージ</label></td>
			<td><textarea name="message" th:text="*{message}"></textarea></td>
		</tr>
		<tr>
			<td><label for="mydata">MYDATA_ID</label></td>
			<td><input type="text" name="mydata"></td>
		</tr>
		<tr>
			<td></td>
			<td><input type="submit"></td>
		</tr>
		</form>
	</table>
	<hr>
	<table>
		<tr><th>ID</th><th>名前</th><th>タイトル</th></tr>
		<tr th:each="obj : ${datalist}">
			<td th:text="${obj.id}"></td>
			<td th:text="${obj.mydata.name}"></td>
			<td th:text="${obj.title}"></td>
		</tr>
	</table>

MsgDataController.java

package com.example.demo;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.example.demo.repositories.MsgDataRepository;

@Controller
public class MsgDataController {

		@Autowired
		MsgDataRepository repository;
		
		@PersistenceContext
		EntityManager entityManager;
		
		MsgDataDaoImpl dao;
		
		@RequestMapping(value = "/msg", method=RequestMethod.GET)
		public ModelAndView msg(ModelAndView mav) {
			mav.setViewName("showMsgData");
			mav.addObject("title", "Sample");
			mav.addObject("msg","MsgDataのsampleです");
			MsgData msgdata = new MsgData();
			mav.addObject("formModel", msgdata);
			List<MsgData> list = (List<MsgData>)dao.getAll();
			mav.addObject("datalist", list);
			return mav;
		}
		
		@RequestMapping(value="/msg", method=RequestMethod.POST)
		public ModelAndView msgform(
				@Valid @ModelAttribute MsgData msgdata,
				Errors result,
				ModelAndView mav) {
			if(result.hasErrors()) {
				mav.setViewName("showMsgData");
				mav.addObject("title", "Sample [Error]");
				mav.addObject("msg", "値を再チェックして下さい");
				return mav;
			} else {
				repository.saveAndFlush(msgdata);
				return new ModelAndView("redirect:/msg");
			}
		}
		
		@PostConstruct
		public void init() {
			System.out.println("ok");
			dao = new MsgDataDaoImpl(entityManager);
		}
		
}

んん?あれ?

[SpringBoot2.4.3] Repository

Repository

@Repository
public interface MyDataRepository extends JpaRepository<MyData, Long> {
	
	@Query("SELECT d FROM MyData d ORDER BY d.name")
	List<MyData> findAllOrderByName();
// 省略

Controller

	@RequestMapping(value="/", method=RequestMethod.GET)
	public ModelAndView index(
			ModelAndView mav) {
		mav.setViewName("index");
		mav.addObject("msg","this is sample content.");
		Iterable<MyData> list = repository.findAllOrderByName();
		mav.addObject("datalist", list);
		return mav;
	}

よく出来てるなー
エンティティ自体にクエリを持たせるか、リポジトリに用意するか、どちらでもできる。

[SpringBoot2.4.3] JPQLを活用

SQLクエリーに似た簡易言語
本格的な検索処理はJPQLを理解する必要がある
1.EntityManagerの用意
2.Queryの作成
3.結果(List)の取得

html

	<table>
	<form action="/find" method="post">
		<tr>
			<td>FIND:</td>
			<td><input type="text" name="fstr" size="20" th:value="${value}"></td></td>
		</tr>
		<tr>
			<td></td>
			<td><input type="submit"></td>
		</tr>
	</form>
	</table>

controller

	@RequestMapping(value="/find", method=RequestMethod.GET)
	public ModelAndView find(ModelAndView mav) {
		mav.setViewName("find");
		mav.addObject("title", "Find Page");
		mav.addObject("msg","MyData sample");
		mav.addObject("value", "");
		Iterable<MyData> list = dao.getAll();
		mav.addObject("datalist", list);
		return mav;
	}
	@RequestMapping(value="/find", method=RequestMethod.POST)
	public ModelAndView search(HttpServletRequest request,
			ModelAndView mav) {
		mav.setViewName("find");
		String param = request.getParameter("fstr");
		if(param =="") {
			mav = new ModelAndView("redirect:/find");
		} else {
			mav.addObject("title","Find result");
			mav.addObject("msg","「" + param + "」の検索結果");
			mav.addObject("value", param);
			List<MyData> list = dao.find(param);
			mav.addObject("datalist", list);
		}
		return mav;
	}

MyDataDao

public interface MyDataDao <T> extends Serializable {
	public List<T> getAll();
	public T findById(long id);
	public List<T> findByName(String name);
	public List<T> find(String fstr);
}
	@Override
	public List<MyData> find(String fstr){
		List<MyData> list = null;
		String qstr = "from MyData where id = :fstr";
		Query query = entityManager.createQuery(qstr).setParameter("fstr", Long.parseLong(fstr));
		list = query.getResultList();
		return list;
	}

[SpringBoot2.4.3] DAOに検索メソッドを追加

MyDataDao.java

import java.io.Serializable;
import java.util.List;

public interface MyDataDao <T> extends Serializable {
	public List<T> getAll();
	public T findById(long id);
	public List<T> findByName(String name);
}

エンティティの追加
MyDataDaoImpl.java

	@Override
	public MyData findById(long id) {
		return (MyData)entityManager.createQuery("from MyData where id = " + id).getSingleResult();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<MyData> findByName(String name){
		return (List<MyData>)entityManager.createQuery("from MyData where name = " + name).getResultList();
	}

完璧に理解するにはもう少し時間がかかりそうやな

[SpringBoot2.4.3] DAO

データベースアクセスする際のオブジェクト
Data Access Object

Dao interface

package com.example.demo;

import java.io.Serializable;
import java.util.List;

public interface MyDataDao <T> extends Serializable {
	public List<T> getAll();
}

MyDataDaoImpl.java

package com.example.demo;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

@Repository
public class MyDataDaoImpl implements MyDataDao<MyData> {
	private static final long serialVersionUID = 1L;
	
	private EntityManager entityManager;
	
	public MyDataDaoImpl() {
		super();
	}
	public MyDataDaoImpl(EntityManager manager) {
		this();
		entityManager = manager;
	}
	
	@Override
	public List<MyData> getAll(){
		Query query = entityManager.createQuery("from MyData");
		@SuppressWarnings("unchecked")
		List<MyData> list = query.getResultList();
		entityManager.close();
		return list;
	}

}

Controller

@Controller
public class HelloController {

	@Autowired
	MyDataRepository repository;
	
	@PersistenceContext
	EntityManager entityManager;
	
	MyDataDaoImpl dao;
	
	@PostConstruct
	public void init() {
		dao = new MyDataDaoImpl(entityManager);
		MyData d1 = new MyData();
		d1.setName("yamada");
		d1.setAge(20);
		d1.setMail("yamada@gmail.com");
		d1.setMemo("090999999");
		repository.saveAndFlush(d1);
		MyData d2 = new MyData();
		d2.setName("hanako");
		d2.setAge(18);
		d2.setMail("hanako@gmail.com");
		d2.setMemo("080888888");
		repository.saveAndFlush(d2);
		MyData d3 = new MyData();
		d3.setName("sachiko");
		d3.setAge(35);
		d3.setMail("sachiko@gmail.com");
		d3.setMemo("070777777");
		repository.saveAndFlush(d3);
	}	
	
	@RequestMapping(value="/", method=RequestMethod.GET)
	public ModelAndView index(
			@ModelAttribute("formModel") MyData mydata,
			ModelAndView mav) {
		mav.setViewName("index");
		mav.addObject("msg","this is sample content.");
		Iterable<MyData> list = dao.getAll();
		mav.addObject("datalist", list);
		return mav;
	}
// 省略

HTML

	<h1>Helo page</h1>
	<p th:text="${msg}"></p>
	<table>
		<form method="post" action="/" th:object="${formModel}">
		<tr>
			<th>ID</th><th>名前</th><th>メール</th><th>年齢</th><th>メモ(tel)</th>
		</tr>
		<tr th:each="obj: ${datalist}">
			<td th:text="${obj.id}"></td>
			<td th:text="${obj.name}"></td>
			<td th:text="${obj.mail}"></td>
			<td th:text="${obj.age}"></td>
			<td th:text="${obj.memo}"></td>
		</tr>
		</form>
	</table>

なんやとおおおおおおおおおおおおお

[SpringBoot2.4.3] PhoneValidatorクラスの作成

PhoneValidator.java

package com.example.demo;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class PhoneValidator implements ConstraintValidator<Phone, String>{
	
	@Override
	public void initialize(Phone phone) {
		
	}
	
	@Override
	public boolean isValid(String input, ConstraintValidatorContext cxt) {
		if(input == null) {
			return false;
		}
		return input.matches("[0-9()-]*");
	}
}

### アノテーションクラス
validation classのためのアノテーションは色々用意しなければならない。

package com.example.demo;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import javax.validation.Constraint;
import javax.validation.Payload;
import javax.validation.ReportAsSingleViolation;

@Documented
@Constraint(validatedBy = PhoneValidator.class)
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@ReportAsSingleViolation
public @interface Phone {
	
	String message() default "please input a phone number.";
	
	Class<?>[] groups() default {};
	
	Class<? extends Payload>[] payload() default{};

}

### Phoneバリデータの使用
MyData.java

	@Column(nullable = true)
	@Phone
	private String memo;

html

		<tr>
			<td><label for="memo">メモ</label></td>
			<td><textarea name="memo" th:text="*{memo}" cols="20" rows="5"  th:errorclass="err"></textarea>
			<div th:if="${#fields.hasErrors('memo')}" th:errors="*{memo}" th:errorclass="err">
			</div></td>
		</tr>

おおお、javaだとなんか異様に感動するな

[SpringBoot2.4.3] javax.validationのアノテーション

### javax.validationパッケージ
@Null, @NotNull
@Min, @Max
@DecimalMin, @DecimalMax
@Digits
@Future @Past
@Size
@Pattern

### Hibernate Validatorアノテーション
@NotEmpty
@Length
@Range
@Email
@CreditCardNumber
@EAN

エンティティ

	@Column(length = 50, nullable = false)
	@NotNull(message="空白は不可")
	private String name;
	
	@Column(length = 200, nullable = true)
	@Email(message="メールアドレスのみ")
	private String mail;
	
	@Column(nullable = true)
	@Min(value=0,message="ゼロ以上")
	@Max(value=200,message="200以下")
	private Integer age;

ValidationMessages.properties

org.hibernate.validator.constraints.NotBlank.message = \u7A7A\u767D\u306F\u4E0D\u53EF\u3067\u3059\u3002
org.hibernate.validator.constraints.NotEmpty.message = \u7A7A\u767D\u306F\u4E0D\u53EF\u3067\u3059\u3002
javax.validation.constraints.Max.message = {value}\u3088\u308A\u5C0F\u3055\u304F\u3057\u3066\u4E0B\u3055\u3044\u3002
javax.validation.constraints.Min.message = {value}\u3088\u308A\u5927\u304D\u304F\u3057\u3066\u4E0B\u3055\u3044\u3002
org.hibernate.validator.constraints.Email.message=\u30E1\u30FC\u30EB\u30A2\u30C9\u30EC\u30B9\u3067\u306F\u3042\u308A\u307E\u305B\u3093\u3002

[SpringBoot2.4.3] バリデーション

validationを使うために、pom.xmlで定義する

pom.xml

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-validation</artifactId>
		</dependency>
		
		<dependency>
			<groupId>javax.validation</groupId>
			<artifactId>validation-api</artifactId>
			<version>2.0.1.Final</version>
		</dependency>

MyData.java

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.Min;
import javax.validation.constraints.Max;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;

@Entity
@Table(name="mydata")
public class MyData{
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column
	@NotNull
	private long id;
	
	@Column(length = 50, nullable = false)
	@NotEmpty
	private String name;
	
	@Column(length = 200, nullable = true)
	@Email
	private String mail;
	
	@Column(nullable = true)
	@Min(0)
	@Max(200)
	private Integer age;
//省略

Controller

import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;

@RequestMapping(value="/", method=RequestMethod.GET)
	public ModelAndView index(
			@ModelAttribute("formModel") MyData mydata,
			ModelAndView mav) {
		mav.setViewName("index");
		mav.addObject("msg","this is sample content.");
		mav.addObject("formModel",mydata);
		Iterable<MyData> list = repository.findAll();
		mav.addObject("datalist", list);
		return mav;
	}
	
	@RequestMapping(value="/", method= RequestMethod.POST)
	@Transactional(readOnly=false)
	public ModelAndView form(
			@ModelAttribute("formModel") 
			@Validated MyData mydata,
			BindingResult result,
			ModelAndView mov) {
		ModelAndView res = null;
		if(!result.hasErrors()) {
			repository.saveAndFlush(mydata);
			res = new ModelAndView("redirect:/");
		} else {
			mov.setViewName("index");
			mov.addObject("msg","sorry, error is occured...");
			Iterable<MyData> list = repository.findAll();
			mov.addObject("datalist",list);
			res = mov;
		}
		return res;
	}

html

		<ul>
			<li th:each="error : ${#fields.detailedErrors()}" class="err" th:text="${error.message}">
		</ul>

うおおおおおお、凄え

html

	<table>
		<form method="post" action="/" th:object="${formModel}">
		<tr>
			<td><label for="name">名前</label></td>
			<td><input type="text" name="name" th:value="*{name}" th:errorclass="err">
			<div th:if="${#fields.hasErrors('name')}" th:errors="*{name}" th:errorclass="err">
			</div>
			</td>
		</tr>
		<tr>
			<td><label for="age">年齢</label></td>
			<td><input type="text" name="age" th:value="*{age}"  th:errorclass="err">
			<div th:if="${#fields.hasErrors('age')}" th:errors="*{age}" th:errorclass="err">
			</div></td>
		</tr>
		<tr>
			<td><label for="mail">メール</label></td>
			<td><input type="text" name="mail" th:value="*{mail}"  th:errorclass="err">
			<div th:if="${#fields.hasErrors('mail')}" th:errors="*{mail}" th:errorclass="err">
			</div></td>
		</tr>
		<tr>
			<td><label for="memo">メモ</label></td>
			<td><textarea name="memo" th:text="*{memo}" cols="20" rows="5"></textarea></td>
		</tr>
		<tr>
			<td></td>
			<td><input type="submit"/></td>
		</tr>
		</form>
	</table>

こうもできる

[SpringBoot2.4.3] JPAで自動生成可能なメソッド

findById(引数)
And
Or
Between
LessThan
GreaterThan
IsNull
IsNotNull, NotNull
Like
NotLike
OrdeBy … AscやDescをつける
Not
In
NotIn

Repository

package com.example.demo.repositories;

import com.example.demo.MyData;

import java.util.List;
import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface MyDataRepository extends JpaRepository<MyData, Long> {
	
	public Optional<MyData> findById(Long name);
	public List<MyData> findByNameLike(String name);
	public List<MyData> findByIdIsNotNullOrderByIdDesc();
	public List<MyData> findByAgeGreaterThan(Integer age);
	public List<MyData> findByAgeBetween(Integer age1, Integer age2);
}

単純検索は自動生成で複雑な検索のみDAOを定義
ほう、SpringBoot JPAの仕組みがわかってきた。

[SpringBoot2.4.3] CRUDのdelete

html

		<form method="post" action="/delete" th:object="${formModel}">
		<input type="hidden" name="id" th:value="*{id}">
		<tr>
			<td><p th:text="|名前: *{name}|"></p></td>
		</tr>
		<tr>
			<td><p th:text="|年齢: *{age}|"></p></td>
		</tr>
		<tr>
			<td><p th:text="*{mail}"></p></td>
		</tr>
		<tr>
			<td><p th:text="*{memo}"></p></td>
		</tr>
		<tr>
			<td><input type="submit" value="delete"/></td>
		</tr>
		</form>

controller

	@RequestMapping(value="/delete/{id}", method=RequestMethod.GET)
	public ModelAndView delete(@PathVariable int id,
			ModelAndView mav) {
		mav.setViewName("delete");
		mav.addObject("title", "delete mydata.");
		Optional<MyData> data = repository.findById((long)id);
		mav.addObject("formModel",data.get());
		return mav;
	}
	
	@RequestMapping(value = "delete", method=RequestMethod.POST)
	@Transactional(readOnly=false)
	public ModelAndView remove(@RequestParam long id,
			ModelAndView mav) {
		repository.deleteById(id);
		return new ModelAndView("redirect:/");
	}

ほう、