使对象变脏并多次调用PersistenceManager.makePersistent,数据将不再存在

时间:2014-04-19 12:17:27

标签: java collections jdo datanucleus

我创建了一个单元测试来演示Datanucleus中的一个问题。 此测试的目的是通过向其Set添加元素使对象变脏,然后多次使其持久化。我已经对对象A进行了3次makePersistent调用。第3次调用实际上没有反映rdbms的变化,导致最后一个断言失败。

依赖关系:
org.datanucleus / DataNucleus将核/ 3.2.13
org.postgresql / PostgreSQL的/ 9.2-1004-jdbc4

对于出现问题的任何帮助都将不胜感激。

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class A
{
    @Persistent(column = "ID", valueStrategy = IdGeneratorStrategy.IDENTITY)
    @PrimaryKey
    private int id;

    @Persistent(table = "A__BS")
    @Element(column = "B")
    @Join(column = "A")
    private Set<B> bs;

    public int getId()
    {
        return id;
    }

    public void setId(int id)
    {
        this.id = id;
    }

    public Set<B> getBs()
    {
        return bs;
    }

    public void setBs(Set<B> bs)
    {
        this.bs = bs;
    }

    public void addB(B b)
    {
        if (bs == null)
        {
            bs = new HashSet<>();
        }
        bs.add(b);
    }
}

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class B
{
    @Persistent(column = "ID", valueStrategy = IdGeneratorStrategy.IDENTITY)
    @PrimaryKey
    private int id;

    public int getId()
    {
        return id;
    }

    public void setId(int id)
    {
        this.id = id;
    }
}

public class Test1
{
    private static PersistenceManagerFactory pmf;

    @BeforeClass
    public static void setUpClass()
    {
        PropertyConfigurator.configure("log4j.properties");

        Map<String, Object> props = new HashMap<>();

        props.put(Constants.PROPERTY_CONNECTION_DRIVER_NAME, "org.postgresql.Driver");
        props.put(Constants.PROPERTY_CONNECTION_URL, "jdbc:postgresql://localhost/test");
        props.put(Constants.PROPERTY_CONNECTION_USER_NAME, "postgres");
        props.put(Constants.PROPERTY_CONNECTION_PASSWORD, "password");
        props.put(Constants.PROPERTY_MAPPING, "postgres");
        props.put(Constants.PROPERTY_PERSISTENCE_MANAGER_FACTORY_CLASS,
                "org.datanucleus.api.jdo.JDOPersistenceManagerFactory");
        props.put(PropertyNames.PROPERTY_AUTOCREATE_SCHEMA, true);
        props.put(PropertyNames.PROPERTY_DETACH_ON_CLOSE, true);
        props.put(PropertyNames.PROPERTY_DETACH_ALL_ON_COMMIT, true);

        pmf = JDOHelper.getPersistenceManagerFactory(props);
    }

    @AfterClass
    public static void tearDownClass()
    {
        pmf.close();
    }

    @Test
    public void test1()
    {
        PersistenceManager pm = pmf.getPersistenceManager();

        B b1 = new B();
        B b2 = new B();
        B b3 = new B();

        b1 = pm.makePersistent(b1);
        b2 = pm.makePersistent(b2);
        b3 = pm.makePersistent(b3);

        A a = new A();
        a.addB(b1);
        a = pm.makePersistent(a);

        a.addB(b2);
        a = pm.makePersistent(a);

        a.addB(b3);
        a = pm.makePersistent(a);

        pm.close();
        pm = pmf.getPersistenceManager(); // Renew pm

        a = pm.getObjectById(A.class, a.getId());

        Assert.assertEquals(3, a.getBs().size()); // Fails. Size is 2
    }
}

0 个答案:

没有答案