为什么我的代码没有抛出EntityExistsException

时间:2014-09-01 19:42:30

标签: java mysql java-ee exception jpa

我目前正在学习Java EE 7,并试图找出何时抛出EntityExistsException。我目前有一个非常简单的Message类,它具有基本属性。据我所知,当数据库中已经存在具有相同主键的实体时,应该抛出EntityExistsException。我不太确定如果所述实体是否分离是否重要,所以做了一个快速测试,看看它何时会发生。但是,由于某种原因,两个测试用例都没有通过,而没有向我显示错误。

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import org.iys.jpa.mysql.example.Message;
import org.junit.After;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;

public class MessageIT {

    private static EntityManagerFactory emf ; 
    private EntityManager em;
    private EntityTransaction tx;

    public MessageIT() {
    }

    @Before
    public void setUp() {
       emf= Persistence.createEntityManagerFactory("JPAMYSQLExampleU");
        em = emf.createEntityManager();
        tx = em.getTransaction();
        Message m1= new Message("Hello");       
        tx.begin();
        em.persist(m1);        
        tx.commit();
        System.out.println("Setting up the test");
    }

    @After
    public void tearDown() {
        if (em != null) {
            em.close();
        }
    }

    //either one of the test should fail

    @Test
    public void shouldFail1(){
        Message msg = em.find(Message.class, 1L);
        Message copied= msg;    
        copied.setMessage("changed");
        assertTrue(em.contains(msg));
        em.clear();
        assertFalse(em.contains(msg));
        assertFalse(em.contains(copied));  //both entities are currently detached
        tx.begin();
       em.persist(copied);

        tx.commit();

    }
    @Test
    public void shouldFail2(){
        Message msg = em.find(Message.class, 1L);
        assertTrue(em.contains(msg));
        tx.begin();
        em.persist(msg);
        tx.commit();
    }
}

如果我误解了发生错误的条件,您将如何更改代码以便抛出上述错误。

1 个答案:

答案 0 :(得分:1)

您可能正在使用@GeneratedValue作为您的ID(如果您可以在问题中提供您的实体实施,则会很好)。在这种情况下,具有持久性的提供者可能只是在持久化实体之前生成新的id。 (这就是为什么ifFail1不会失败的原因。)

如果shouldFail2规范声明:

  

如果X是预先存在的托管实体,则persist会忽略它   操作

由于您在该测试中管理了msg,因此只会忽略持久性。

您最好切换到提供的ID,而不是生成以测试EntityExistsException案例。