复制实体集合并坚持在Hibernate / JPA中(Duplicate a collection of entities and persist in Hibernate/JPA)
我想要复制数据库中的实体集合。 我通过以下方式检索收藏:
CategoryHistory chNew = new CategoryHistory(); CategoryHistory chLast = (CategoryHistory)em.createQuery("SELECT ch from CategoryHistory ch WHERE ch.date = MAX(date)").getSingleResult; List<Category> categories = chLast.getCategories(); chNew.addCategories(categories)// Should be a copy of the categories: OneToMany
现在我想复制'类别'的列表并将其与EntityManager一起保存。 我正在使用JPA / Hibernate。 UPDATE
知道如何分离我的实体后,我需要知道要分离的内容:当前代码:
CategoryHistory chLast = (CategoryHistory)em.createQuery("SELECT ch from CategoryHistory ch WHERE ch.date=(SELECT MAX(date) from CategoryHistory)").getSingleResult(); Set<Category> categories =chLast.getCategories(); //detach org.hibernate.Session session = ((org.hibernate.ejb.EntityManagerImpl) em.getDelegate()).getSession(); session.evict(chLast);//detaches also its child-entities? //set the realations chNew.setCategories(categories); for (Category category : categories) { category.setCategoryHistory(chNew); } //set now create date chNew.setDate(Calendar.getInstance().getTime()); //persist em.persist(chNew);
这会抛出一个
failed to lazily initialize a collection of role: entities.CategoryHistory.categories, no session or session was closed
异常。我认为他想要再次加载类别,因为我让它们分离。 我现在应该怎么做?
I want to duplicate a collection of entities in my database. I retreive the collection with:
CategoryHistory chNew = new CategoryHistory(); CategoryHistory chLast = (CategoryHistory)em.createQuery("SELECT ch from CategoryHistory ch WHERE ch.date = MAX(date)").getSingleResult; List<Category> categories = chLast.getCategories(); chNew.addCategories(categories)// Should be a copy of the categories: OneToMany
Now i want to duplicate a list of 'categories' and persist it with EntityManager. I'm using JPA/Hibernate. UPDATE
After knowing how to detach my entities, i need to know what to detach: current code:
CategoryHistory chLast = (CategoryHistory)em.createQuery("SELECT ch from CategoryHistory ch WHERE ch.date=(SELECT MAX(date) from CategoryHistory)").getSingleResult(); Set<Category> categories =chLast.getCategories(); //detach org.hibernate.Session session = ((org.hibernate.ejb.EntityManagerImpl) em.getDelegate()).getSession(); session.evict(chLast);//detaches also its child-entities? //set the realations chNew.setCategories(categories); for (Category category : categories) { category.setCategoryHistory(chNew); } //set now create date chNew.setDate(Calendar.getInstance().getTime()); //persist em.persist(chNew);
This throws a
failed to lazily initialize a collection of role: entities.CategoryHistory.categories, no session or session was closed
exception.I think he wants to lazy load the categories again, as i have them detached. What should i do now?
原文:https://stackoverflow.com/questions/1973877
最满意答案
$str = preg_replace('/(?<=>)\s+(?=<)/', '', $str);
不易破损,但使用更多资源:
<?php $html = '<tr> <td>A </td> <td>B </td> <td>C </td> </tr>'; $d = new DOMDocument(); $d->loadHTML($html); $x = new DOMXPath($d); foreach($x->query('//text()[normalize-space()=""]') as $textnode){ $textnode->deleteData(0,strlen($textnode->wholeText)); } echo $d->saveXML($d->documentElement->firstChild->firstChild);
$str = preg_replace('/(?<=>)\s+(?=<)/', '', $str);
Less prone to breakage, but uses some more resources:
<?php $html = '<tr> <td>A </td> <td>B </td> <td>C </td> </tr>'; $d = new DOMDocument(); $d->loadHTML($html); $x = new DOMXPath($d); foreach($x->query('//text()[normalize-space()=""]') as $textnode){ $textnode->deleteData(0,strlen($textnode->wholeText)); } echo $d->saveXML($d->documentElement->firstChild->firstChild);
相关问答
更多-
用一个空格替换两个或多个空格: s/ +/ /g 编辑:对于任何空白区域(不只是空格),如果您使用的是perl兼容的正则表达式库,并且出现次数为大括号语法,则可以使用\ s s/\s\s+/ /g 要么 s/\s{2,}/ /g 编辑#2:忘了/ g全局后缀,谢谢JL Substitute two or more spaces for one space: s/ +/ /g Edit: for any white space (not just spaces) you can use \s if ...
-
在JavaScript中修剪字符串?(Trim string in JavaScript?)[2022-09-16]
由于新的浏览器(IE9 +)已经实现了trim() ,所以只有在Prototype-Object上不可用的时候才应该执行trim() (覆盖它是一个巨大的性能命中)。 通常在扩展Native Objects时建议这样做! 请注意,添加的属性是枚举的,除非您使用ES5 Object.defineProperty ! 对于那些不支持trim()浏览器,您可以使用MDN中的这个polyfill: if (!String.prototype.trim) { (function() { // ... -
如果你的目标是从一开始就保留所有内容,但不包括第一个空格,那么试试这个: NSRange spaceRange = [myString rangeOfString:@" "]; if (spaceRange.location != NSNOtFound) { NSString *trimmedString = [myString substringToIndex:spaceRange.location]; // This gives you @"Wi-Fi" } 相反,如果要查找最后一个空 ...
-
正则表达式trim或preg_replace包含制表符和新行的空白区域(Regex trim or preg_replace white space including tabs and new lines)[2022-03-01]
我认为这个基于lookahead和lookbehind的正则表达式可以为你工作: $str = <<< EOF\t \n\n\n \t \n poo
EOF; $str = preg_replace_callback('#(?<=n \n \n \t )(.*?)(\s*<[^>]*>\s*)(.*?)(?= ...
-
尝试这个: $string = str_replace(array("\n", "\r"), '', $string); Try this: $string = str_replace(array("\n", "\r"), '', $string);
-
$str = preg_replace('/(?<=>)\s+(?=<)/', '', $str); 不易破损,但使用更多资源:
A B C '; $d = new DOMDocument(); $d->loadHTML($html); $x = new DOMXPath($d); foreach($x->query ... -
如何修剪和替换字符串(How to trim and replace a string)[2023-06-14]
这似乎是一种低效率的方法,但也许你应该看一下gregexpr和regmatches的方向而不是gsub : x <- " this is a string " pattern <- "^ +?\\b|\\b? +$" startstop <- gsub(" ", "~", regmatches(x, gregexpr(pattern, x))[[1]]) text <- paste(regmatches(x, gregexpr(pattern, x), invert=TRUE)[[1]], coll ... -
PHP trim()没有修剪空间(PHP trim() not trimming space)[2022-02-03]
请尝试以下方法: echo trim($deal->{self::DEAL_BUSINESS}, “\xC2\xA0\n”); 要么 $text = preg_replace('~\x{00a0}~siu','',$deal->{self::DEAL_BUSINESS}); echo $text; Try the following: echo trim($deal->{self::DEAL_BUSINESS}, "\xC2\xA0\n"); Or $text = preg_replace('~\x{ ... -
如果你调用source $source ,那么下面的$result就是你想要的数组: //split into parts separated by all non-word characters $result = preg_split('/\W+/',trim($source)); 现场演示 If you call your source $source, then $result below is the array you want: //split into parts separated by ...
-
PHP ingeligent修剪字符串(PHP ingeligent trim string)[2023-11-21]
$string = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas enim est, convallis non ullamcorper hendrerit, vehicula ac quam. In malesuada suscipit eleifend. Donec posuere ipsum at ipsum feugiat eu ornare nunc ultricies. Donec eleifend odio ...