我正在使用 jpa 2.0 并且我有以下实体:
@Entity
public class Folder{
@ElementCollection
@CollectionTable(name="folder_files")
private Set<String> files;
// .....
}
Run Code Online (Sandbox Code Playgroud)
给定文件名,我想删除 files == theGivenFileName 的所有条目。在 sql 中它会是这样的:
Delete from folder_files where files = XXX
Run Code Online (Sandbox Code Playgroud)
有没有办法使用标准 API 执行此查询?如果没有,有没有办法使用 jpql 执行此查询?
更新:
我认为我的问题不够清楚:因为 jpql 使用实体(而不是表),所以我不能只执行上面写的 sql 加上因为我正在使用@ElementCollection我不知道如何解决这个 variablr 甚至处理它。我想从所有实体中删除该集合中包含给定值的所有条目(在我的情况下,是文件集)。是否可以使用 jpql 或(甚至更好)标准 API?
我正在使用maven 3.0.4 , JRE 1.7.0_09. 当我使用mvn clean install所有测试通过并且一切看起来都很好 - 这是我的surefire插件配置:
<plugin>
<version>2.12.4</version>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!-- -XX:-UseSplitVerifier is for java 7 -->
<argLine>-XX:-UseSplitVerifier</argLine>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)
现在,当我的mvn cobertura:cobertura一些测试有这样的错误:
Expecting a stackmap frame at branch target ....
还有一些错误让我明白它没有使用JRE7运行(例如Encountered " "|" "| "" at line...)
这是我的cobertura插件配置:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<formats>
<format>html</format>
<format>xml</format>
</formats>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)
报告是:
<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<formats>
<format>html</format>
<format>xml</format>
</formats>
</configuration>
</plugin>
</plugins>
</reporting>
Run Code Online (Sandbox Code Playgroud)
我看到很多线程谈论这个问题,解决方案是添加这一行, …
我正在生产中使用Postgres数据库,并在H2中使用它进行测试。我想为现有表创建一个新序列-因此在Liquibase中我这样写:
<changeSet id="Add sequence for BOOKS" author="library">
<createSequence sequenceName="BOOKS_SEQ" incrementBy="500" startValue="1"/>
</changeSet>
Run Code Online (Sandbox Code Playgroud)
我的实体看起来像这样:
@Entity
@Table(name = "BOOKS")
@SequenceGenerator(name = "BOOKS_SEQ", allocationSize = 500)
public class Book {
@Id
@GeneratedValue(generator = "BOOKS_SEQ", strategy = GenerationType.TABLE)
private Long id;
@ManyToOne
@JoinColumn(name = "AUTHOR_ID")
private Author author;
...
}
Run Code Online (Sandbox Code Playgroud)
现在,由于我已经在该表中有了实体(带有我使用的上一个序列的ID),因此需要相应地设置该序列的当前值。为此,我写道:
<changeSet id="Alter sequence for BOOKS" author="library">
<sql dbms="postgresql">select setval('BOOKS_SEQ', (select nextval('OLD_SEQUENCE_UID')))</sql>
</changeSet>
<changeSet id="Add default value to BOOKS.ID" author="library">
<addDefaultValue tableName="BOOKS" columnName="ID" defaultValueSequenceNext="BOOKS_SEQ"/>
</changeSet>
Run Code Online (Sandbox Code Playgroud)
在Postgres(生产环境)中,它似乎可以正常工作-但在H2中,我收到一条错误消息“名为[BOOKS_SEQ]的序列设置不正确。其增量与预分配的大小不匹配”。
根据这个 -我需要设置START_VALUE(或电流值)的东西大于500 -但我无法弄清楚如何能够在下半年完成。
所以我的问题是:如何在H2中设置序列的当前值?
我正在Derived使用mockito在TDD中编写一个新类(),我有以下情况:
班级基地:
public abstract class Base<T>{
//.......
protected final T baseCreate(T entity){
// implementation
}
}
Run Code Online (Sandbox Code Playgroud)
Class Derived(这是我使用TDD编写的类):
public class Derived extends Base<MyObject> {
//.......
public MyObject create(MyObject entity){
baseCreate(entity); //This is what I want the implementation to be
}
}
Run Code Online (Sandbox Code Playgroud)
当我开始编写测试时,会强制我调用该baseCreate方法 - 我无法理解如何做到这一点.有没有办法,使用mockito,来验证该方法在类create(...)中Derived调用baseCreate(...)方法Base?
谢谢.
我有这个代码:
private ConcurrentMap<String, Integer> myMap = new ConcurrentHashMap<>();
@Scheduled(fixedDelay = 600_000)
public void foo(){
myMap.values().stream().
filter(predicate()).
forEach(this::remove);
}
public void insert(String str, Integer value){
myMap.put(str, value);
}
Run Code Online (Sandbox Code Playgroud)
如果在遍历此地图时会发生什么 - 有人会在其中添加新值或从中删除现有值?
java ×5
cobertura ×1
collections ×1
concurrency ×1
criteria-api ×1
eclipselink ×1
h2 ×1
java-7 ×1
java-8 ×1
java-stream ×1
jenkins ×1
jpa ×1
jpa-2.0 ×1
liquibase ×1
maven ×1
mockito ×1
mysql ×1
tdd ×1
testing ×1