- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
编辑:我发现这是 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() {
}
}
持久性.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>
最佳答案
正如 Boann 所做的那样,我尝试运行您的代码(为您尚未显示 (LV
, AbstractModule
) 代码的类填写简单实现并初始化 lv
),我得到了预期的结果:
2
before:short2
2
in map:short2, in map:short1
short2, short1
2
因此,我的假设是问题不在您在此处显示的代码中,而是在 LV 或 AbstractModule 的代码中,或者您可能没有导入您认为是的类。最后,我是否可以假设没有其他正在运行的线程可能会更改 ams
集合?
为了比较,这是我对您的类的虚拟实现。
抽象模块:
public class AbstractModule {
String kurzbezeichnung;
public AbstractModule(String desc) {
this.kurzbezeichnung = desc;
}
public String getKurzbezeichnung() {
return this.kurzbezeichnung;
}
}
LV:
public class LV {
HashSet<AbstractModule> ams;
public LV(HashSet<AbstractModule> abstractModules) {
this.ams = abstractModules;
}
public Collection<AbstractModule> getAbstractModules() {
return ams;
}
}
初始化:
HashSet<AbstractModule> initialAms = new HashSet<AbstractModule>();
AbstractModule short1 = new AbstractModule("short1");
AbstractModule short2 = new AbstractModule("short2");
initialAms.add(short2);
initialAms.add(short1);
LV lv = new LV(initialAms);
编辑:
在看到您更新的问题并意识到所有这些类实际上都是持久数据库实体之后,我认为问题可能是由于延迟加载引起的。我可以建议您将 ams 的定义更改为
@OneToMany(mappedBy = "lv", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Collection<AM> ams = new HashSet<AM>();
从下面的讨论中可以看出,EclipseLink 将 ams 集合实现为 org.eclipse.persistence.indirection.IndirectSet
,这是一个延迟加载的实现。但是那个类没有实现 stream()
方法。
正如 OP 所指出的,有一个针对 EclipseLink 的错误报告来修复此问题:https://bugs.eclipse.org/bugs/show_bug.cgi?id=433075
关于合并 myMap 后的 java 8 lambda myMap.stream().count() != myMap.size(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27107595/
我有几个长度不等的 vector ,我想对其进行cbind。我将 vector 放入列表中,并尝试结合使用do.call(cbind, ...): nm <- list(1:8, 3:8, 1:5)
合并(合并)两个 JSONObjects 的最佳方式是什么? JSONObject o1 = { "one": "1", "two": "2", "three": "3" }
我在一个表中有许多空间实体,其中有一个名为 Boundaries 的 geometry 字段。我想生成一个具有简化形状/几何图形的 GeoJson 文件。 这是我的第一次尝试: var entitie
谁能说出为什么这个选择返回 3.0 而不是 3.5: SELECT coalesce(1.0*(7/2),0) as foo 这个返回 3: SELECT coalesce(7/2,0) as foo
首先抱歉,也许这个问题已经提出,但我找不到任何可以帮助我的东西,可能是因为我对 XSLT 缺乏了解。 我有以下 XML: 0 OK
有时用户会使用 Windows 资源管理器复制文件并在他们应该执行 svn 存储库级别的复制或合并时提交它们。因此,SVN 没有正确跟踪这些变化。一旦我发现这一点,损坏显然已经完成,并且可能已经对相关
我想组合/堆叠 2 个不同列的值并获得唯一值。 如果范围相邻,则可以正常工作。例如: =UNIQUE(FILTERXML(""&SUBSTITUTE(TEXTJOIN(",",TRUE,TRANSPO
使用iTextSharp,如何将多个PDF合并为一个PDF,而又不丢失每个PDF中的“表单字段”及其属性? (我希望有一个使用来自数据库的流的示例,但文件系统也可以) 我发现this code可以正常
是否有一个合并函数可以优先考虑公共(public)变量中的非缺失值? 考虑以下示例。 首先,我们生成两个 data.frames,它们具有相同的 ID,但在特定变量上有互补的缺失值: set.seed
我们正在尝试实现 ALM Rangers 在最新的 Visual Studio TFS Branching and Merging Guide 中描述的“基本双分支计划”。 .从指导: The bas
我在不同目录(3个不同名称)中有很多(3个只是一个例子)文本文件,如下所示: 目录:A,文件名:run.txt 格式:txt制表符分隔 ; file one 10 0.2 0.5 0.
我有一张包含学生等级关系的表: Student Grade StartDate EndDate 1 1 09/01/2009 NULL 2
我在学习 https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/working-with-associatio
我觉得我有世界上最简单的 SVN 用例: 我有一个文件,Test.java在 trunk SVN的。 我分行trunk至 dev-branch . 我搬家Test.java进入 com/mycompa
我有两个数据框,其中一些列名称相同,而另一些列名称不同。数据框看起来像这样: df1 ID hello world hockey soccer 1 1 NA NA
Elasticsearch 中是否缺少以扁平化形式(多个子/子aggs)返回结果的方法? 例如,当前我正在尝试获取所有产品类型及其状态(在线/离线)。 这就是我最终得到的: aggs [ { key:
如何合并如下所示的 map : Map1 = Map(1 -> Class1(1), 2 -> Class1(2)) Map2 = Map(2 -> Class2(1), 3 -> Class2(2)
我试图通过从netezza服务器导入数据来合并两个数据集。 以下是数据集,其数字为,ID为,字母为,名称为: 下表都是使用命令从netezza导入的: sqoop import --connect n
我有两个数组 $array1 = array('first', 'second', 'third', 'fourth'); $array2 = array('first', 'third', 'fou
我正在 SQL Server 中运行合并。在我的更新中,我只想在值发生更改时更新该行。有一个版本行在每次更新时都会递增。下面是一个例子: MERGE Employee as tgt USING (SE
我是一名优秀的程序员,十分优秀!