【发布时间】:2014-11-24 14:52:04
【问题描述】:
编辑:我发现这是一个 Eclipse 错误链接:https://bugs.eclipse.org/bugs/show_bug.cgi?id=433075
原始问题:
在 JUnit 测试中,我发现了意外行为。如您所见,最后一个断言失败了,因为流似乎不包含任何数据,而映射不为空?!我发现,它与 jpa/eclipselink 有关。
public class MyTest {
private EntityManager em;
@Before
public void setUp() throws IOException {
String dbFileName = "testDb.db";
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(PersistenceUnitProperties.JDBC_URL, "jdbc:sqlite:"
+ dbFileName);
EntityManagerFactory factory = Persistence.createEntityManagerFactory("pul", properties);
this.em = factory.createEntityManager();
}
@Test
public void testStrange() {
LV lv = new LV();
// em.persist(lv); // this would work
// em.merge(lv); // this would work as well
lv = em.merge(lv); // this leads to the assertionError
lv.getAms().add(new AM());
assertEquals(1, lv.getAms().size()); // ok
assertEquals(1, lv.getAms().stream().count()); // java.lang.AssertionError: expected:<1> but was:<0>
}
}
LV:
@Entity
public class LV {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long _id;
@OneToMany(mappedBy = "lv", cascade = CascadeType.ALL)
private Collection<AM> ams = new HashSet<AM>();
public LV() {
}
public Collection<AM> getAms() {
return ams;
}
}
上午:
@Entity
public class AM {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long _id;
@ManyToOne
private LV lv;
public AM() {
}
}
persistence.xml
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="pul" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.sqlite.JDBC" />
<!-- url is set during runtime -->
<!-- <property name="javax.persistence.jdbc.url" value="jdbc:sqlite:sample.db"
/> -->
<property name="javax.persistence.jdbc.user" value="" />
<property name="javax.persistence.jdbc.password" value="" />
<!-- EclipseLink should create the database schema automatically -->
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode"
value="database" />
</properties>
</persistence-unit>
</persistence>
pom.xml 依赖项
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.8.6</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.6.0-M3</version>
</dependency>
【问题讨论】:
-
了解
lv.getAbstractModules()和getKurzbezeichnung()的类型可能会有所帮助。 -
当我使用这些方法名称设置虚拟类时,您的确切代码对我有用(在列表中显示一致数量的项目)。所以我只能建议您的
lv.getAbstractModules()方法不会随着时间的推移返回相同的列表。 -
您是否有机会使用自定义的
List实现,该实现可能有一个损坏的stream()方法? -
不,我使用 HashSet 将
AbstractModules 存储在 LV 中。 -
如果您使用的是 HashSet,您确定您已经正确地覆盖了
AbstractModule的equals和hashCode方法,还是将它们都保留为默认实现?
标签: java dictionary lambda eclipselink java-stream