⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 orderitemtest.java

📁 Java EE 5 许多新功能都包含经过修补的 EJB 架构
💻 JAVA
字号:
/*
 * Copyright 2006 Borys Burnayev
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package test.persistence;

import java.util.HashMap;
import java.util.Map;

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

import junit.framework.TestCase;

import com.rdacorp.petstore.domain.Item;
import com.rdacorp.petstore.domain.Order;
import com.rdacorp.petstore.domain.OrderItem;

/**
 * @author Borys Burnayev
 */
public class OrderItemTest extends TestCase {

   public void testCRUD() throws Exception {
      Map config =  new HashMap();
      EntityManagerFactory emf = Persistence.createEntityManagerFactory("manager1", config);
      EntityManager em = emf.createEntityManager();

      em.getTransaction().begin();
      OrderItem orderItem = new OrderItem(543, em.find(Order.class, 1), em.find(Item.class, 1));
      em.persist(orderItem);
      em.getTransaction().commit();

      // verify primary key
      Integer orderItemId = orderItem.getOrderItemId();
      assertNotNull(orderItemId);

      em.getTransaction().begin();
      // read the record
      orderItem = em.find(OrderItem.class, orderItemId);
      // verify the record
      assertEquals(543, orderItem.getQuantity());
      assertEquals(em.find(Order.class, 1).getAmount(), orderItem.getOrder().getAmount());
      assertEquals(em.find(Item.class, 1).getCode(), orderItem.getItem().getCode());
      // update existing record
      orderItem.setQuantity(987);
      em.getTransaction().commit();

      em.getTransaction().begin();
      // reread the record
      orderItem = em.find(OrderItem.class, orderItemId);
      // verify the updated record
      assertEquals(987, orderItem.getQuantity());
      // delete the record
      em.remove(orderItem);
      em.getTransaction().commit();

      // retrive the person and verify it is null
      orderItem = em.find(OrderItem.class, orderItemId);
      assertNull(orderItem);

      em.close();
      emf.close();
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -