Spring 06


・Thymeleaf
エスケープ処理機能が内蔵しています。

(Controller)
@RequestMapping("/")
public ModelAndView index(ModelAndView mav) {
    mav.setViewName("index");
    mav.addObject("msg","message 1<hr/>message 2<br/>message 3");
    return mav;
}

 

(index)
<body>
    <h1 th:text="#{content.title}">Helo page</h1>
    <p th:text="${msg}">message.</p>
</body>


使いたくない場合は、utextに変わります。   
<body>
    <h1 th:text="#{content.title}">Helo page</h1>
    <p th:utext="${msg}">message.</p>
</body>


・GenerationType
問題点:AUTOで生成することができません。
解決方法:IDENTITYに変更しました。

@GeneratedValue(strategy = GenerationType.AUTO)
データベースごとに異なる方法を選択し,主キー値を生成します。
@GeneratedValue(strategy = GenerationType.IDENTITY)
テーブルのidentity列を利用して,主キー値を生成します。

(ブラウザのエラー)
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Tue May 28 14:03:31 JST 2019
There was an unexpected error (type=Internal Server Error, status=500).
error performing isolated work; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: error performing isolated work

(Consoleのエラー)
Hibernate: select mydata0_.id as id1_0_, mydata0_.age as age2_0_, mydata0_.mail as mail3_0_, mydata0_.memo as memo4_0_, mydata0_.name as name5_0_ from mydata mydata0_
Hibernate: select next_val as id_val from hibernate_sequence for update
2019-05-28 14:03:31.119 ERROR 1364 --- [nio-8080-exec-3] o.hibernate.id.enhanced.TableStructure   : could not read a hi value

java.sql.SQLSyntaxErrorException: Table 'test_01.hibernate_sequence' doesn't exist


@PostConstruct
データの初期化処理
保存したデータは、アプリケーションを終了し、再度起動すると消えてしまっています。

問題点:初期化が成功しましたが、終了の時、データが残っています。
解決方法:IDを指定して、データを初期化処理します。
@PostConstruct
    public void init(){
        MyData d1 = new MyData();
        d1.setId(1);
        d1.setName("tuyano");
        d1.setAge(123);
        d1.setMail("syoda@tuyano.com");
        d1.setMemo("this is my data!");
        repository.saveAndFlush(d1);
    }

この解決方法はよくないと思います。
DatabaseにIDは1のデータがいないなら、新しいデータを作成しまいました。
そして、データを削除するのはいけません。削除したら、上記の状況が発生します。
Columnにunique keyを追加する場合は、重複のデータを作成すると、バグになってしまいます。

・Optional
nullかもしれないオブジェクトをラップするためのクラス。