自分で作ったり提供したりするものは、まず自分自身で使ってみろろということです。自分じゃ使わないものなら人はいくらでも無責任にも無思考にもなれる。そういう投げやりな「サービス」やら「プロダクツ」なんて、だれだってイヤだ。自分が作り手と同時に利用者の立場になれば、ちゃんと使えるレベルのものを提供しようとします。

2012年1月1日日曜日

Hibernate EntityManager 複合キー

Accounts.java
package com.test;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Accounts {
    AccountsCompoundKey compoundKey;
    private int accountBalance;

    @Id
    public AccountsCompoundKey getCompoundKey() {
        return compoundKey;
    }

    public void setCompoundKey(AccountsCompoundKey compoundKey) {
        this.compoundKey = compoundKey;
    }

    public int getAccountBalance() {
        return accountBalance;
    }

    public void setAccountBalance(int accountBalance) {
        this.accountBalance = accountBalance;
    }
}

AccountsCompoundKey.java
package com.test;

import java.io.Serializable;

import javax.persistence.Embeddable;

@Embeddable
public class AccountsCompoundKey implements Serializable {
    private int userId;
    private int accountId;

    public AccountsCompoundKey(int userId, int accountId) {
        super();
        this.userId = userId;
        this.accountId = accountId;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public int getAccountId() {
        return accountId;
    }

    public void setAccountId(int accountId) {
        this.accountId = accountId;
    }
}

AccountsTest.java
package com.test;

import static org.junit.Assert.*;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class AccountsTest {

    static EntityManagerFactory entityManagerFactory;

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        entityManagerFactory = Persistence
                .createEntityManagerFactory("org.hibernate.tutorial.jpa");
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        entityManagerFactory.close();
    }

    @Before
    public void setUp() throws Exception {

    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void test() {
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        entityManager.getTransaction().begin();
        
        try {
            
            AccountsCompoundKey key1 = new AccountsCompoundKey(100, 10000);
            Accounts account1 = new Accounts();
            account1.setCompoundKey(key1);
            account1.setAccountBalance(8000);
            
            entityManager.persist(account1);
            
            AccountsCompoundKey key2 = new AccountsCompoundKey(200, 20000);
            Accounts account2 = new Accounts();
            account2.setCompoundKey(key2);
            account2.setAccountBalance(9000);
            
            entityManager.persist(account2);
            entityManager.getTransaction().commit();
        }
        catch (Exception e) {
            entityManager.getTransaction().rollback();
        }
    }
}

0 件のコメント:

コメントを投稿

ホームページ