Customer.java
CustomerTest.java
テーブル
customer
customerdetail
Customer.java
package com.test; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.SecondaryTable; import javax.persistence.Table; import javax.persistence.TableGenerator; @Entity @Table(name = "Customer") @SecondaryTable(name = "CustomerDetail") public class Customer { private long customerId; private String customerName; private String customerAddress; private int creditScore; private int rewardPoints; @Id @TableGenerator(name = "customerid", table = "emppktbl", pkColumnName = "empkey", pkColumnValue = "customervalue", allocationSize = 1) @GeneratedValue(strategy = GenerationType.TABLE, generator = "customerid") @Column(name = "CustomerId") public long getCustomerId() { return customerId; } public void setCustomerId(long customerId) { this.customerId = customerId; } public String getCustomerName() { return customerName; } public void setCustomerName(String customerName) { this.customerName = customerName; } @Column(table = "CustomerDetail") public String getCustomerAddress() { return customerAddress; } public void setCustomerAddress(String customerAddress) { this.customerAddress = customerAddress; } @Column(table = "CustomerDetail") public int getCreditScore() { return creditScore; } public void setCreditScore(int creditScore) { this.creditScore = creditScore; } @Column(table = "CustomerDetail") public int getRewardPoints() { return rewardPoints; } public void setRewardPoints(int rewardPoints) { this.rewardPoints = rewardPoints; } }
CustomerTest.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 CustomerTest { 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 { Customer customer1 = new Customer(); customer1.setCustomerName("customer1"); customer1.setCustomerAddress("newyork"); customer1.setCreditScore(12345); customer1.setRewardPoints(100); entityManager.persist(customer1); Customer customer2 = new Customer(); customer2.setCustomerName("customer2"); customer2.setCustomerAddress("tokyo"); customer2.setCreditScore(67890); customer2.setRewardPoints(200); entityManager.persist(customer2); entityManager.getTransaction().commit(); } catch (Exception e) { entityManager.getTransaction().rollback(); } } }
Customer.sql
create table Customer ( CustomerId int8 not null, customerName varchar(255), primary key (CustomerId) )
CustomerDetail.sql
create table CustomerDetail ( creditScore int4, customerAddress varchar(255), rewardPoints int4, CustomerId int8 not null, primary key (CustomerId) )
0 件のコメント:
コメントを投稿