我需要从数据库中导出大量数据.这是代表我的数据的类:
public class Product{
...
@OneToMany
@JoinColumn(name = "product_id")
@Cascade({SAVE_UPDATE, DELETE_ORPHAN})
List<ProductHtmlSource> htmlSources = new ArrayList<ProductHtmlSource>();
Run Code Online (Sandbox Code Playgroud)
...}
ProductHtmlSource - 包含我实际需要导出的大字符串.
由于导出数据的大小比JVM内存大,我正在按块读取数据.像这样:
final int batchSize = 1000;
for (int i = 0; i < 50; i++) {
ScrollableResults iterator = getProductIterator(batchSize * i, batchSize * (i + 1));
while (iterator.getScrollableResults().next()) {
Product product = (Product) iterator.getScrollableResults().get(0);
List<String> htmls = product.getHtmlSources();
<some processing>
}
Run Code Online (Sandbox Code Playgroud)
}
代码getProductIterator:
public ScrollableResults getProductIterator(int offset, int limit) {
Session session = getSession(true);
session.setCacheMode(CacheMode.IGNORE);
ScrollableResults iterator …Run Code Online (Sandbox Code Playgroud) 如何在dbunit数据集中插入换行符?像这样:
<user id="1"
story="first line
second line
third line"/>
Run Code Online (Sandbox Code Playgroud)
如果我以这种方式执行db 中的字段故事,则只会出现由空格分隔的情况,但我需要换行。
我尝试使用以下代码插入或更新db记录:
Category category = new Category();
category.setName('catName');
category.setId(1L);
categoryDao.saveOrUpdate(category);
Run Code Online (Sandbox Code Playgroud)
当数据库中已存在id = 1的类别时,一切正常.但如果没有id = 1的记录,我得到以下异常:
org.hibernate.StaleStateException:
Batch update returned unexpected row count from update [0]; actual row count: 0;
expected: 1:
Run Code Online (Sandbox Code Playgroud)
为清晰起见,这里是我的Category类setter,getters和constructors:
@Entity
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@ManyToOne
private Category parent;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "parent")
private List<Category> categories = new ArrayList<Category>();
}
Run Code Online (Sandbox Code Playgroud)
在控制台中我看到了这个hibernate查询:
update
Category
set
name=?,
parent_id=?
where
id=?
Run Code Online (Sandbox Code Playgroud)
所以看起来像hibernates tryis更新记录而不是插入新的.我在这做错了什么?
是否可以执行在java应用程序中动态加载的groovy代码.例如,有一个数据库表,其中包含一小段groovy代码,如:
def test(${val_to_insert_from_java}){
if (${val_to_insert_from_java} > 10){
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
哪里${val_to_insert_from_java}是它要去的Java代码,如执行过程中插入一些实际值的占位符:
String groovyFuncSource = getFromDb();
groovyFuncSource.replace(${val_to_insert_from_java}, 9);
Object result = <evaluate somehow groovyFuncSource>;
Run Code Online (Sandbox Code Playgroud)
有没有办法评估这样的Groovy代码?或者你可能会告诉我一些其他方法如何实现这一点.
如果我们谈论域对象,为什么setter在接口上是坏的?
澄清:
我有一个存储在db中的域对象.它有几个领域,设置成本很高.即
class JurasicPark {
private long area;
...other fields ...
getters and setters
....
private Collection<Dinosaur> dinosaurs;
private Collection<ExoticTree> flora;
public Collection<Dinosaur> getDinosaurus(){
...
}
public Collection<ExoticTree> getFlora(){
...
}
}
Run Code Online (Sandbox Code Playgroud)
字段dinosaurs并且flora初始化和设置成本非常高.但在许多情况下,我不需要每次都设置这个字段.
问题是,如果我返回到JurasicPark类的用户实例,dinosaurs或者flora没有初始化它,那么填充导致NPE或我自己投掷的一些预期.我不想让api用户考虑这个并记住哪些字段可能没有设置.
因此,为了解决这个问题,我考虑创建两个接口IJurasicPark,IFullJurasicPark第一个将所有访问方法声明为简单字段,前者将声明访问方法flora和dinosaurs字段.
interface IFullJurasicPark extends IJurasicPark
class IJurasicPark implements IFullJurasicPark
Run Code Online (Sandbox Code Playgroud)
在这种方法中IJurasicPark接口将包含getter和setter,所以我实际上问这个设计是不是很糟糕?
我也不想在使用LazyInit异常的hibernate风格中实现它.
我有复杂的对象,收集字段需要存储到Hadoop.我不想遍历整个对象树并明确存储每个字段.所以我只考虑复杂字段的序列化并将其存储为一个大块.而不是在阅读对象时绝望.那么最好的方法是什么?我虽然为此使用了某种类型的血清,但我希望Hadoop有办法处理这种情况.
要存储的示例对象的类:
class ComplexClass {
<simple fields>
List<AnotherComplexClassWithCollectionFields> collection;
}
Run Code Online (Sandbox Code Playgroud) 我有2个同步方法的类:
class Service {
public synchronized void calc1();
public synchronized void calc2();
}
Run Code Online (Sandbox Code Playgroud)
两者都需要相当长的时间来执行 问题是这些方法的执行会相互阻塞.即两种方法可以在不同的线程中并行执行吗?
我只是好奇,允许接口包含静态方法的实现会不会更方便?这些方法可能包含常用的短(通过此接口实现者)逻辑.
我有以下字符串:
data = ["myKey": "myValue"]
Run Code Online (Sandbox Code Playgroud)
并希望将其评估为地图:
def map = evaluate(data)
Run Code Online (Sandbox Code Playgroud)
看起来我做错了但是得到了
groovy.lang.MissingMethodException: No signature of method: DUMMY.evaluate() is
applicable for argument types: (java.lang.String) values: [["myKey": "myValue"]]
Run Code Online (Sandbox Code Playgroud)
所以我的问题是如何进行这样的评估?