今更Seasar2で開発

最近業務でSeasar2を使うことが多くなった。
個人的にはSpringMVCやPlay Frameworkに挑戦したいところですが・・・。

で、seasar2で開発する場合のORマッパーとしてS2jdbcを使うことになったので、
あまり使う事がなかったので、チュートリアルをやって勉強。

以外に分かり易かった。
まずはEntity

package sample.entity;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

@Entity
public class Point {
	/** idプロパティ */
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(nullable = false, unique = true)
	public Integer id;

	@Column(nullable = false, unique = false)
	public Integer point;

	@OneToOne(mappedBy = "point")
	public Users users;

	@OneToMany(mappedBy = "point")
	public List<PointHistory> pointHistoryList;
}

もう一つEntity.

package sample.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;

@Entity
public class Users {
	/** idプロパティ */
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(nullable = false, unique = true)
	public Integer id;

	/** nameプロパティ */
	@Column(length = 255, nullable = false, unique = false)
	public String name;

	@OneToOne(mappedBy = "users")
	public Point point;

}


で、テストコード

package sample.entity;

import java.util.List;

import javax.annotation.Generated;

import org.seasar.extension.jdbc.JdbcManager;
import org.seasar.extension.unit.S2TestCase;
import org.seasar.framework.beans.util.BeanMap;

/**
 * {@link Point}のテストクラスです。
 * 
 */
@Generated(value = { "S2JDBC-Gen 2.4.46", "org.seasar.extension.jdbc.gen.internal.model.EntityTestModelFactoryImpl" }, date = "2014/01/06 12:32:00")
public class PointTest extends S2TestCase {

	private JdbcManager jdbcManager;

	/**
	 * 事前処理をします。
	 * 
	 * @throws Exception
	 */
	@Override
	protected void setUp() throws Exception {
		super.setUp();
		include("s2jdbc.dicon");
	}

	public void testInsertTx() throws Exception {
		Users targetUser = jdbcManager.from(Users.class).where("id = ?", 1).getSingleResult();
		Point point = new Point();
		point.point = 100;
		point.users = targetUser;
		int count = jdbcManager.insert(point).execute();
		System.out.println("INSERT DATA COUNT:" + count);
		System.out.println(point.id);
		long totalCount = jdbcManager.from(Point.class).getCount();
		System.out.println("TOTAL COUNT:" + totalCount);
		List<Point> results = jdbcManager.from(Point.class).getResultList();
		for (Point e : results) {
			System.out.println("VALUE1:" + e.point);
			System.out.println("VALUE1:" + e.users);
		}
	}
}

テストコードのメソッド「testInsertTx() 」と末尾にTxと付けているとテストコード実行後にロールバックされるので、常にデータがテスト前の状態に戻ります。これはいい。

後は、各エンティティ間の関連がアノテーションで定義できるのですが、何となくRailsと同じニオイがします。だからか、頭に入り易い。

ま、まだまだ色々あるので、もっと調べて上手に使えるようにしたいですね。
では。