CLIからデプロイしてみる
TweetPosted on Tuesday Jan 21, 2014 at 04:18PM in Technology
環境
- WildFly8.0.0CR1
- Eclipse Kelper SR1
デプロイするWARファイルの準備
WARファイルを作る
対象のプロジェクトを選んで右クリック→Export→WAR file
Web projectとDestnationを埋めてFinish
中身を見てみる
kyle-no-MacBook:~ kyle$ jar tvf ~/JavaEE7Prac.war
Picked up _JAVA_OPTIONS: -Dfile.encoding=UTF-8
105 Mon Jan 20 13:28:12 JST 2014 META-INF/MANIFEST.MF
0 Sat Jan 18 21:56:20 JST 2014 META-INF/
0 Thu Jan 16 15:04:08 JST 2014 META-INF/maven/
0 Thu Jan 16 15:04:08 JST 2014 META-INF/maven/com.example/
0 Thu Jan 16 15:04:08 JST 2014 META-INF/maven/com.example/JavaEE7Prac/
232 Mon Jan 20 18:07:00 JST 2014 META-INF/maven/com.example/JavaEE7Prac/pom.properties
897 Mon Jan 20 18:07:00 JST 2014 META-INF/maven/com.example/JavaEE7Prac/pom.xml
0 Sat Jan 18 21:53:56 JST 2014 WEB-INF/
0 Sat Jan 18 21:56:12 JST 2014 WEB-INF/classes/
0 Sat Jan 18 21:56:20 JST 2014 WEB-INF/classes/META-INF/
0 Sat Jan 18 21:47:46 JST 2014 WEB-INF/classes/META-INF/batch-jobs/
183 Sat Jan 18 21:50:40 JST 2014 WEB-INF/classes/META-INF/batch-jobs/job001.xml
0 Thu Jan 16 15:09:30 JST 2014 WEB-INF/classes/com/
0 Mon Jan 20 17:00:08 JST 2014 WEB-INF/classes/com/example/
678 Mon Jan 20 13:28:16 JST 2014 WEB-INF/classes/com/example/TestBatchlet.class
1413 Mon Jan 20 13:28:16 JST 2014 WEB-INF/classes/com/example/TestServlet.class
252 Sat Jan 18 21:53:56 JST 2014 WEB-INF/beans.xml
0 Thu Jan 16 15:51:56 JST 2014 WEB-INF/lib/
385091 Mon Jan 20 13:28:12 JST 2014 WEB-INF/lib/commons-lang3-3.2.1.jar
540 Mon Jan 20 16:58:00 JST 2014 WEB-INF/web.xml
kyle-no-MacBook:~ kyle$
大丈夫そうですね。jarファイルとかもちゃんと入ってる。
デプロイしてみる
CLIからデプロイ
$ sudo -i -u wildfly /usr/local/wildfly/bin/jboss-cli.sh [disconnected /] connect [standalone@localhost:9990 /] deploy /tmp/JavaEE7Prac.war [standalone@localhost:9990 /]
アクセスしてみる
大丈夫そうですね
デプロイされたアプリ一覧を表示
[standalone@localhost:9990 /] ls deployment JavaEE7Prac.war postgresql-9.3-1100.jdbc41.jar [standalone@localhost:9990 /]
CLIからアンデプロイ
[standalone@localhost:9990 /] undeploy JavaEE7Prac.war [standalone@localhost:9990 /]
[standalone@localhost:9990 /] ls deployment postgresql-9.3-1100.jdbc41.jar [standalone@localhost:9990 /]
消えましたね。アクセスしても消えています
$JBOSS_HOME/standalone/log/server.logを見てみる
2014-01-21 15:45:59,294 INFO [org.jboss.as.repository] (management-handler-thread - 8) JBAS014900: Content added at location /usr/local/wildfly/standalone/data/content/81/8f5b7155a35cc08acfd55f279cd7bab40e5972/content 2014-01-21 15:45:59,305 INFO [org.jboss.as.server.deployment] (MSC service thread 1-4) JBAS015876: Starting deployment of "JavaEE7Prac.war" (runtime-name: "JavaEE7Prac.war") 2014-01-21 15:46:00,445 INFO [org.jboss.weld.deployer] (MSC service thread 1-3) JBAS016002: Processing weld deployment JavaEE7Prac.war 2014-01-21 15:46:01,027 INFO [org.hibernate.validator.internal.util.Version] (MSC service thread 1-3) HV000001: Hibernate Validator 5.0.2.Final 2014-01-21 15:46:01,941 INFO [org.jboss.weld.deployer] (MSC service thread 1-1) JBAS016005: Starting Services for CDI deployment: JavaEE7Prac.war 2014-01-21 15:46:02,535 INFO [org.jboss.weld.Version] (MSC service thread 1-1) WELD-000900: 2.1.1 (Final) 2014-01-21 15:46:02,593 INFO [org.jboss.weld.deployer] (MSC service thread 1-1) JBAS016008: Starting weld service for deployment JavaEE7Prac.war 2014-01-21 15:46:05,880 INFO [javax.enterprise.resource.webcontainer.jsf.config] (MSC service thread 1-4) Initializing Mojarra 2.2.4-jbossorg-1 20131017-1524 for context '/JavaEE7Prac' 2014-01-21 15:46:07,412 INFO [org.wildfly.extension.undertow] (MSC service thread 1-4) JBAS017534: Register web context: /JavaEE7Prac 2014-01-21 15:46:07,590 INFO [org.jboss.as.server] (management-handler-thread - 8) JBAS018559: Deployed "JavaEE7Prac.war" (runtime-name : "JavaEE7Prac.war") 2014-01-21 15:46:38,643 INFO [stdout] (batch-batch - 1) Hello JSR352 2014-01-21 15:47:14,605 INFO [stdout] (batch-batch - 3) Hello JSR352 2014-01-21 15:47:27,235 INFO [stdout] (batch-batch - 5) Hello JSR352 2014-01-21 15:47:27,389 INFO [stdout] (batch-batch - 7) Hello JSR352 2014-01-21 15:51:30,933 INFO [org.wildfly.extension.undertow] (MSC service thread 1-4) JBAS017535: Unregister web context: /JavaEE7Prac 2014-01-21 15:51:30,956 INFO [org.jboss.weld.deployer] (MSC service thread 1-3) JBAS016009: Stopping weld service for deployment JavaEE7Prac.war 2014-01-21 15:51:30,992 INFO [org.jboss.as.server.deployment] (MSC service thread 1-2) JBAS015877: Stopped deployment JavaEE7Prac.war (runtime-name: JavaEE7Prac.war) in 65ms 2014-01-21 15:51:31,088 INFO [org.jboss.as.repository] (management-handler-thread - 15) JBAS014901: Content removed from location /usr/local/wildfly/standalone/data/content/81/8f5b7155a35cc08acfd55f279cd7bab40e5972/content 2014-01-21 15:51:31,089 INFO [org.jboss.as.server] (management-handler-thread - 15) JBAS018558: Undeployed "JavaEE7Prac.war" (runtime-name: "JavaEE7Prac.war")
特に問題なさげですね。
上書きデプロイをワンライナーで
sudo -i -u wildfly /usr/local/wildfly/bin/jboss-cli.sh --connect "deploy /tmp/JavaEE7Prac.war --force JavaEE7Prac.war"
参考文献
Tags: wildfly
ダンプしてリストアしてみる
TweetPosted on Tuesday Jan 21, 2014 at 02:32PM in Technology
何をするの?
手元の端末で作ったDBを別のマシン上のPostgreSQLにリストアしてみます
環境
- ダンプ環境
- PostgreSQL 9.2.4 @ OS X 10.9.1
- リストア環境
- PostgreSQL 9.2.6 @ CentOS 6.5
テスト用のDBを作る
作成用SQL
一応日本語が化けるのとか心配なので日本語も入れてみます
create database testdb001 owner kyle encoding 'UTF8' TEMPLATE template0; \c testdb001 create table hogetable (id bigint, hogecol varchar(255)); insert into hogetable values (1, 'hoge111'); insert into hogetable values (2, 'ほげ222');
流す
何故かpsqlのプロンプトに日本語をコピペすると文字化けしてしまうのでヒアドキュメント経由で食わせます
kyle-no-MacBook:prepare kyle$ psql <<EOF > create database testdb001 owner kyle encoding 'UTF8' TEMPLATE template0; > \c testdb001 > create table hogetable (id bigint, hogecol varchar(255)); > insert into hogetable values (1, 'hoge111'); > insert into hogetable values (2, 'ほげ222'); > EOF CREATE DATABASE You are now connected to database "testdb001" as user "kyle". CREATE TABLE INSERT 0 1 INSERT 0 1 kyle-no-MacBook:prepare kyle$
ちゃんと入ったか確認してみましょう
kyle-no-MacBook:prepare kyle$ psql testdb001 psql (9.2.4) Type "help" for help. testdb001=# select * from hogetable ; id | hogecol ----+--------- 1 | hoge111 2 | ほげ222 (2 rows) testdb001=#
大丈夫そうですね。
ダンプ
もっとも柔軟だそうなのでcustom形式にします。このコマンドで
pg_dump -F c testdb001 > testdb001.dump
実行してみる
kyle-no-MacBook:~ kyle$ pg_dump -F c testdb001 > testdb001.dump kyle-no-MacBook:~ kyle$ ls -l testdb001.dump -rw-r--r--+ 1 kyle staff 2106 1 21 13:37 testdb001.dump kyle-no-MacBook:~ kyle$
こいつをscpか何かでリストア環境に持っていきます
リストア
このコマンドでいってみる
pg_restore -v -C -d postgres -j 2 testdb001.dump
- -v
- 進捗状況を詳細表示
- -C
- リストアの前にデータベースを作成する
- -d postgres
- 接続時のDBにpostgresを使う。-Cを同時に指定している場合,データのリストア先ではない。リストア先のDB名はダンプファイルに含まれるものが使われる.-Cを同時に指定していない場合は,データのリストア先になる.
- -j 2
- 2パラで同時実行。この場合入力はパイプではだめらしい
スーパーユーザで実行するならこう
sudo -u postgres sh -c "pg_restore -v -C -d postgres -j 2 testdb001.dump"
ダンプファイルをpostgresユーザが読めるところに置いて実行
$ cp testdb001.dump /tmp $ cd /tmp $ sudo -u postgres sh -c "pg_restore -v -C -d postgres -j 2 testdb001.dump" pg_restore: リストアのためにデータベースに接続しています pg_restore: 2183 ENCODING ENCODING を処理しています pg_restore: 2184 STDSTRINGS STDSTRINGS を処理しています pg_restore: 2185 DATABASE testdb001 を処理しています pg_restore: DATABASE testdb001を作成しています pg_restore: 新しいデータベース"testdb001"に接続しています pg_restore: データベース"testdb001"にユーザ"postgres"で接続しています pg_restore: 5 SCHEMA public を処理しています pg_restore: SCHEMA publicを作成しています pg_restore: 2186 COMMENT SCHEMA public を処理しています pg_restore: COMMENT SCHEMA publicを作成しています pg_restore: 2187 ACL public を処理しています pg_restore: 169 EXTENSION plpgsql を処理しています pg_restore: EXTENSION plpgsqlを作成しています pg_restore: 2188 COMMENT EXTENSION plpgsql を処理しています pg_restore: COMMENT EXTENSION plpgsqlを作成しています pg_restore: 168 TABLE hogetable を処理しています pg_restore: TABLE hogetableを作成しています pg_restore: メインの並列ループに入ります pg_restore: 項目 2180 TABLE DATA hogetable に着手します pg_restore: テーブル"hogetable"のデータをリストアしています pg_restore: 項目 2180 TABLE DATA hogetable を完了しました pg_restore: メインの並列ループを終了します pg_restore: DATABASE testdb001用の所有者と権限を設定しています pg_restore: SCHEMA public用の所有者と権限を設定しています pg_restore: COMMENT SCHEMA public用の所有者と権限を設定しています pg_restore: ACL public用の所有者と権限を設定しています pg_restore: EXTENSION plpgsql用の所有者と権限を設定しています pg_restore: COMMENT EXTENSION plpgsql用の所有者と権限を設定しています pg_restore: TABLE hogetable用の所有者と権限を設定しています pg_restore: TABLE DATA hogetable用の所有者と権限を設定しています $ echo $? 0 $
確認してみましょう
$ psql testdb001
psql (9.2.6)
"help" でヘルプを表示します.
testdb001=> \d
リレーションの一覧
スキーマ | 名前 | 型 | 所有者
----------+-----------+----------+--------
public | hogetable | テーブル | kyle
(1 行)
testdb001=> select * from hogetable ;
id | hogecol
----+---------
1 | hoge111
2 | ほげ222
(2 行)
testdb001=>
権限とかスキーマが若干不安ですがデータは移っているようですね。
参考文献
Tags: postgres
PUTメソッドとmod_jkについて調べる
TweetPosted on Tuesday Jan 21, 2014 at 09:34AM in Technology
環境
- さくらVPS 1G
- CentOS 6.5
- Apache 2.2.15
- mod_jk設定済み
$ /usr/sbin/httpd -v Server version: Apache/2.2.15 (Unix) Server built: Aug 13 2013 17:29:28 $
GETを送ってみる
全部APサーバに飛ばす設定にしているのでAPサーバのウェルカム画面的なものが返る。
$ curl -v http://localhost:80/ -X GET * About to connect() to localhost port 80 (#0) * Trying 127.0.0.1... connected * Connected to localhost (127.0.0.1) port 80 (#0) > GET / HTTP/1.1 > User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2 > Host: localhost > Accept: */* > < HTTP/1.1 200 OK < Date: Tue, 21 Jan 2014 00:28:59 GMT < Server: Apache/2.2.15 (CentOS) < Last-Modified: Sun, 22 Dec 2013 06:12:49 GMT < Content-Length: 2417 < Connection: close < Content-Type: text/html; charset=UTF-8 < <!-- ~ JBoss, Home of Professional Open Source. ~ Copyright (c) 2011, Red Hat, Inc., and individual contributors ~ as indicated by the @author tags. See the copyright.txt file in the ...
PUTを送ってみる
デフォルトで拒否されるっぽい
$ curl -v http://localhost:80/ -X PUT * About to connect() to localhost port 80 (#0) * Trying 127.0.0.1... connected * Connected to localhost (127.0.0.1) port 80 (#0) > PUT / HTTP/1.1 > User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2 > Host: localhost > Accept: */* > < HTTP/1.1 405 Method Not Allowed < Date: Mon, 20 Jan 2014 23:55:11 GMT < Server: Apache/2.2.15 (CentOS) < Content-Length: 83 < Connection: close < Content-Type: text/html; charset=UTF-8 < * Closing connection #0 <html><head><title>Error</title></head><body>405 - Method Not Allowed</body></html>
インストールしてからmod_jkでAPサーバに全リクエスト飛ばす設定をした他は、何も設定変更していないのだけど拒否されている。PUTも飛ぶのかと思っていたけど違うようだ。
他のメソッドも試してみる
PUT, DELETE, OPTIONSは405
$ curl -LI http://localhost:80/ -X GET -o /dev/null -w '%{http_code}\n' -s
200
$ curl -LI http://localhost:80/ -X POST -o /dev/null -w '%{http_code}\n' -s
200
$ curl -LI http://localhost:80/ -X PUT -o /dev/null -w '%{http_code}\n' -s
405
$ curl -LI http://localhost:80/ -X DELETE -o /dev/null -w '%{http_code}\n' -s
405
$ curl -LI http://localhost:80/ -X OPTIONS -o /dev/null -w '%{http_code}\n' -s
405
$
知らなかった。そういえば少し前に脆弱性が報告されていたような。そのせいでApache側でデフォルトオフになったのだろうか。mod_jkの設定がどうのというよりそもそもApacheで受け付けない設定になっている感じ。
許可するには?
そもそも最近のApacheでは全面的にオフになっていて、[7]によると何やらモジュールの有効化などが必要らしい。何か大変そうなので今回はここまでにしておく。
参考文献
- Issue with HTTP methods (DELETE,PUT) not being accepted (returning 405 Method not allowed) (Apache Users)
- Re: Issue with HTTP methods (DELETE,PUT) not being accepted (returning 405 Method not allowed) (Apache Users)
- Tomcat + Apache mod_jk PUT gives 405 (Tomcat forum at JavaRanch)
- [Tomcat-users] mod_jk PUT request-method returns 405 Error - Grokbase
- Agile Testing: Configuring Apache 2 and Tomcat 5.5 with mod_jk
- cURLでHTTPステータスコードだけを取得する - Qiita [キータ]
- PUTやDELETEを使うために - PukiWiki
Tags: apache
JPQLをorm.xmlに書いてみる
TweetPosted on Tuesday Jan 21, 2014 at 08:04AM in Technology
何をするの?
JPQLをorm.xmlに書いて動かしてみます。
環境
- WildFly8.0.0CR1
- Eclipse Kepler SR1
- PostgreSQL 9.2.4
- Oracle JDK7u51
準備
これの続きです。
orm.xmlに書いたクエリを動かしてみる
orm.xmlを作る
src/main/java/META-INFの下に作ります。右クリックメニューからNew→JPA ORM Mapping File
場所を確かめてFinish
出来たら開きましょう
クエリを書く
named-queryを書いてみます。エンティティに直書きしてたのとほとんど同じです。違うのはidで逆順ソートかけてるとこだけです
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence/orm http://xmlns.jcp.org/xml/ns/persistence/orm_2_1.xsd">
<named-query name="findHogeEntityAllDesc">
<query>
SELECT
e
FROM
HogeEntity e
ORDER BY
e.id DESC
</query>
</named-query>
</entity-mappings>
この書き方だと < とか > をエスケープしないといけないのが面倒くさいですが、こういう感じで書くとそのまま書けます。
Daoにメソッドを追加
findAllDesc()を追加します
package com.example.dao;
import java.util.Date;
import java.util.List;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;
import com.example.entity.HogeEntity;
@Named
public class HogeEntityDao {
@PersistenceContext
private EntityManager em;
@Transactional
public void save() {
HogeEntity e = new HogeEntity();
e.setHogeDate(new Date());
em.persist(e);
}
public List<HogeEntity> findAll() {
return em.createNamedQuery("findHogeEntityAll", HogeEntity.class).getResultList();
}
public List<HogeEntity> findAllDesc() {
return em.createNamedQuery("findHogeEntityAllDesc", HogeEntity.class).getResultList();
}
}
サーブレットから呼び出すメソッドを変更
package com.example.servlet;
import java.io.IOException;
import java.io.Writer;
import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.example.dao.HogeEntityDao;
import com.example.entity.HogeEntity;
@WebServlet("/HogeServlet")
public class HogeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Inject
HogeEntityDao dao;
public HogeServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
dao.save();
try (Writer w = response.getWriter()) {
// for (HogeEntity e : dao.findAll()) {
for (HogeEntity e : dao.findAllDesc()) {
w.write("id=" + e.getId() + ", hogeDate=" + e.getHogeDate() + "\n");
}
}
}
}
動かしてみる
逆順になりました。改行とかも自由なので、やっぱりクエリはエンティティのアノテーション内に書くよりorm.xmlに書いた方が良い気がします。
Tags: jpa
動かしてみる
TweetPosted on Tuesday Jan 21, 2014 at 07:57AM in Technology
何をするの?
開発環境を設定して、JPAを叩くCDI管理Beanと、それを叩くサーブレットを作って動かしてみます。
環境
- WildFly8.0.0CR1
- Eclipse Kepler SR1
- PostgreSQL 9.2.4
- Oracle JDK7u51
準備
プロジェクトを作る
- まずこの要領で空っぽのプロジェクトjpahelloを作ります
-
Project Facetsを開き、CDIとJPAにチェックを入れてOKを押します
-
何故かbeans.xmlが出来ないので作ります。web-resourcesを右クリックしてNew→File beans.xmlを選択
-
フォルダを「jpahello/src/main/webapp/WEB-INF」にしてFinish
-
persistence.xmlとbeans.xmlができた事を確認します。
データベース作成とデータソース定義
このへんを参考にすると良いかも
persistence.xmlの設定をする
Persistence provider
先ほど作ったpersistence.xmlをダブルクリックして開いてみましょう
Persistence providerを設定します。WildFlyにはHibernateが入っているので
org.hibernate.ejb.HibernatePersistenceになります。
Connection
次はデータソースの設定です。画面下部のConnectionタブを押して画面を切り替えます
JTA data sourceにAPサーバに登録してあるデータソースのJNDI名を入力します
Schema Generation
次はDDL自動生成の設定です。画面下部のSchema Generationタブを押して画面を切り替えます
Database actionをDrop and createにしておきます。こうするとデプロイの度にDROPとCREATEが走ります。ちょっといじりたいだけなのでこれで。
Sourceを確認
画面下部のSourceタブを押して画面を切り替えるとpersistence.xmlが確認出来ます。こんな感じになってるのを確認したら保存しましょう。
<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> <persistence-unit name="jpahello"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <jta-data-source>java:jboss/jdbc/TestDS</jta-data-source> <properties> <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/> </properties> </persistence-unit> </persistence>
エンティティを作る
-
src/main/javaで右クリック→New→JPA Entityを選択
-
Java packageとClass nameを埋めてFinish
-
内容はこんな感じで
package com.example.entity;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
@NamedQueries({ @NamedQuery(name = "findHogeEntityAll", query = "SELECT e FROM HogeEntity e") })
public class HogeEntity implements Serializable {
private static final long serialVersionUID = 1L;
public HogeEntity() {
super();
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(nullable = false)
private Long id;
@Column
@Temporal(TemporalType.TIMESTAMP)
private Date hogeDate;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Date getHogeDate() {
return hogeDate;
}
public void setHogeDate(Date hogeDate) {
this.hogeDate = hogeDate;
}
}
Dao的なクラスを作る
package com.example.dao;
import java.util.Date;
import java.util.List;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;
import com.example.entity.HogeEntity;
@Named
public class HogeEntityDao {
@PersistenceContext
private EntityManager em;
@Transactional
public void save() {
HogeEntity e = new HogeEntity();
e.setHogeDate(new Date());
em.persist(e);
}
public List<HogeEntity> findAll() {
return em.createNamedQuery("findHogeEntityAll", HogeEntity.class).getResultList();
}
}
サーブレットを作る
package com.example.servlet;
import java.io.IOException;
import java.io.Writer;
import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.example.dao.HogeEntityDao;
import com.example.entity.HogeEntity;
@WebServlet("/HogeServlet")
public class HogeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Inject
HogeEntityDao dao;
public HogeServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
dao.save();
try (Writer w = response.getWriter()) {
for (HogeEntity e : dao.findAll()) {
w.write("id=" + e.getId() + ", hogeDate=" + e.getHogeDate() + "\n");
}
}
}
}
作った資源を確認
5つ全部あるか確認しましょう。
デプロイしてみる
コンソールにこんなのが流れるはず
17:05:24,199 INFO [org.jboss.as.server.deployment] (MSC service thread 1-14) JBAS015876: Starting deployment of "jpahello.war" (runtime-name: "jpahello.war") 17:05:24,219 INFO [org.jboss.as.jpa] (MSC service thread 1-3) JBAS011401: Read persistence.xml for jpahello 17:05:24,227 INFO [org.jboss.as.jpa] (ServerService Thread Pool -- 264) JBAS011409: Starting Persistence Unit (phase 1 of 2) Service 'jpahello.war#jpahello' 17:05:24,227 INFO [org.hibernate.jpa.internal.util.LogHelper] (ServerService Thread Pool -- 264) HHH000204: Processing PersistenceUnitInfo [ name: jpahello ...] 17:05:24,236 INFO [org.jboss.weld.deployer] (MSC service thread 1-15) JBAS016002: Processing weld deployment jpahello.war 17:05:24,247 INFO [org.jboss.weld.deployer] (MSC service thread 1-9) JBAS016005: Starting Services for CDI deployment: jpahello.war 17:05:24,249 INFO [org.jboss.weld.deployer] (MSC service thread 1-16) JBAS016008: Starting weld service for deployment jpahello.war 17:05:24,252 INFO [org.jboss.as.jpa] (ServerService Thread Pool -- 264) JBAS011409: Starting Persistence Unit (phase 2 of 2) Service 'jpahello.war#jpahello' 17:05:24,425 INFO [org.hibernate.dialect.Dialect] (ServerService Thread Pool -- 264) HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL9Dialect 17:05:24,426 INFO [org.hibernate.engine.jdbc.internal.LobCreatorBuilder] (ServerService Thread Pool -- 264) HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException 17:05:24,428 INFO [org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory] (ServerService Thread Pool -- 264) HHH000397: Using ASTQueryTranslatorFactory 17:05:24,437 INFO [org.hibernate.dialect.Dialect] (ServerService Thread Pool -- 264) HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL9Dialect 17:05:24,443 WARN [org.hibernate.jpa.internal.schemagen.GenerationTargetToDatabase] (ServerService Thread Pool -- 264) Unable to execute JPA schema generation drop command [drop table HogeEntity cascade] 17:05:24,443 WARN [org.hibernate.jpa.internal.schemagen.GenerationTargetToDatabase] (ServerService Thread Pool -- 264) Unable to execute JPA schema generation drop command [drop sequence hibernate_sequence] 17:05:24,663 INFO [org.wildfly.extension.undertow] (MSC service thread 1-8) JBAS017534: Register web context: /jpahello 17:05:24,671 INFO [org.jboss.as.server] (DeploymentScanner-threads - 1) JBAS018559: Deployed "jpahello.war" (runtime-name : "jpahello.war")
DBはどうなっているかというと
testdb=# \d
List of relations
Schema | Name | Type | Owner
--------+--------------------+----------+----------
public | hibernate_sequence | sequence | postgres
public | hogeentity | table | postgres
(2 rows)
testdb=# \d hogeentity
Table "public.hogeentity"
Column | Type | Modifiers
----------+-----------------------------+-----------
id | bigint | not null
hogedate | timestamp without time zone |
Indexes:
"hogeentity_pkey" PRIMARY KEY, btree (id)
testdb=# \d hibernate_sequence
Sequence "public.hibernate_sequence"
Column | Type | Value
---------------+---------+---------------------
sequence_name | name | hibernate_sequence
last_value | bigint | 1
start_value | bigint | 1
increment_by | bigint | 1
max_value | bigint | 9223372036854775807
min_value | bigint | 1
cache_value | bigint | 1
log_cnt | bigint | 0
is_cycled | boolean | f
is_called | boolean | f
testdb=#
自動で作ってくれると楽ですね
アクセスしてみる
リロードするたびに増えていきます。
テーブルも見てみましょう
testdb=# select * from hogeentity ; id | hogedate ----+------------------------- 1 | 2014-01-20 17:09:57.694 2 | 2014-01-20 17:09:58.091 3 | 2014-01-20 17:09:59.28 4 | 2014-01-20 17:09:59.475 5 | 2014-01-20 17:09:59.629 6 | 2014-01-20 17:09:59.812 7 | 2014-01-20 17:09:59.966 8 | 2014-01-20 17:10:03.878 (8 rows) testdb=#
ちゃんと動いているようですね
参考文献
Tags: jpa
