我在SO上找到了关于这个问题的几个问题和答案,但它们似乎都涵盖了问题的一个主要原因:在事务之外或在另一个事务中获取集合.但在我的情况下,我在获取父对象和集合时在同一事务中获取.
@Service
@Transactional
public class IntegrationServiceImpl implements IntegrationService {
@Override
public Integration getIntegrationByIdFetchBackendParameters(Long integrationId) {
Integration integration = integrationDao.get(integrationId);
//not all integrations have to have backend.
if (integration.getBackend() != null) {
Hibernate.initialize(integration.getBackend().getBackendParameters());
}
return integration;
}
...
Run Code Online (Sandbox Code Playgroud)
但是当谈到Hibernate.initialize这个分支在org.hibernate.collection.internal.AbstractPersistentCollection中执行
if ( session == null ) {
throw new HibernateException( "collection is not associated with any session" );
}
Run Code Online (Sandbox Code Playgroud)
我不能明白为什么session
是null
.有人会解释这个并提出解决方案吗?
编辑1 - 完整堆栈跟踪
org.hibernate.HibernateException: collection is not associated with any session
at org.hibernate.collection.internal.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:676)
at org.hibernate.Hibernate.initialize(Hibernate.java:77)
at com.dhl.finFw.service.IntegrationServiceImpl.getIntegrationByIdFetchBackendParameters(IntegrationServiceImpl.java:169)
at …
Run Code Online (Sandbox Code Playgroud) 我有一个带消息的属性文件,我想处理一些复数的特殊情况.现在我使用:
xxx.yyy.plural=test{0,choice,2#y}
Run Code Online (Sandbox Code Playgroud)
但它也将12格式化为"暴躁".如何指定2为完全匹配?
我正在用H2数据库编写集成测试.我的数据库(生成)初始化包含此脚本(因为生成的连接表没有此列):
ALTER TABLE INT_USR ADD IU_INSDTTM TIMESTAMP DEFAULT NOW();
Run Code Online (Sandbox Code Playgroud)
这是我创建记录的方式:
Integration integrationOne = createIntegration(firstId, "FIRST");
Integration integrationTwo = createIntegration(secondId, "SECOND");
flushAndClear();
userService.logRecentIntegration(integrationOne.getId(), user.getId());
flushAndClear();
userService.logRecentIntegration(integrationTwo.getId(), user.getId()); //1
Run Code Online (Sandbox Code Playgroud)
方法logRecentIntegrations(..,..)只调用DAO,dao执行此操作:
Query query = entityManager.createNativeQuery(
"INSERT INTO INT_USR (USR_ID, INT_ID) VALUES (?, ?)");
query.setParameter(1, userId)
.setParameter(2, integrationId);
query.executeUpdate();
Run Code Online (Sandbox Code Playgroud)
后来在我的测试中:
Query query = entityManager.createNativeQuery(
"SELECT * FROM INT_USR ORDER BY IU_INSDTTM");
List resultList = query.getResultList();
Run Code Online (Sandbox Code Playgroud)
当我在resultList中调试此测试时,有两个记录(正确)但它们具有相同的时间戳.即使我在标记为// 1的行上插入断点并等待一段时间 - 因此插入之间的时间间隔也很重要.(Thread.sleep - 相同的结果)
我试图将SQL脚本修改为
ALTER TABLE INT_USR ADD IU_INSDTTM TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
Run Code Online (Sandbox Code Playgroud)
但结果相同.为什么两个结果都有相同的时间戳?
我和CustomerUnit之间有很多关系:
public class Customer extends AbstractEntity {
@JoinTable(name = "CUS_BUS_UNITS",
joinColumns = {
@JoinColumn(name = "CUS_ID", referencedColumnName = "CUS_ID")},
inverseJoinColumns = {
@JoinColumn(name = "BUS_ID", referencedColumnName = "BUS_ID")})
@ManyToMany
private Collection<BusinessUnit> businessUnits;
}
public class BusinessUnit extends AbstractEntity {
@ManyToMany(mappedBy = "businessUnits")
private Collection<Customer> customers;
}
Run Code Online (Sandbox Code Playgroud)
当我调用entityManager.merge(customer)时; 在客户(已经在DB中,没有改变)我在日志中看到这两个sql命令:
Hibernate:更新CUSTOMERS设置CUS_DESCR =?,CUS_NAME =?,CUS_ENABLED =?哪里有CUS_ID =?Hibernate:从CUS_BUS_UNITS中删除CUS_ID =?
为什么hibernate试图从连接表中删除记录?我只需要在连接表中更新客户记录和可能的记录 - 取决于我是否在客户上添加或删除了业务单位.不应更新,删除或插入业务单位.
编辑: 我的equals/hashCode是(在AbstractEntity中定义):
public int hashCode() {
if (getId() != null) {
return getId().hashCode();
}
return super.hashCode();
}
public boolean equals(Object obj) {
if …
Run Code Online (Sandbox Code Playgroud) 这是我的实体:
public class Account extends AbstractEntity<Long> {
@Id
@SequenceGenerator(name = "accountSequence", sequenceName = "SQ_ACCOUNTS", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "accountSequence")
@Column(name = "ACC_ID", nullable = false)
private Long id;
...
}
public class Integration extends AbstractEntity<Long> {
@Id
@SequenceGenerator(name = "integrationSequence", sequenceName="SQ_INTEGRATIONS", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "integrationSequence")
@Column(name = "INT_ID", nullable = false)
private Long id;
...
public void addIntegration(Integration integration) {
IntegrationAccount association = new IntegrationAccount();
// This does not help
//association.setIntAccountsPK(new …
Run Code Online (Sandbox Code Playgroud) 我的目标是从java中的字符串中提取名称和数字.示例:输入 - >输出
1234 - >数字:[1234],姓名:[]
1234,34,234 - >数字:[1234,34,234],名称:[]
12,foo,123 - >数字:[12,123],姓名:[foo]
foo3,1234,4bar,12,12foo34 - >数字:[1234,12],姓名:[foo3,4bar,12foo34]
foo,bar - > - > numbers:[],姓名:[foo,bar]
我想出了[^,]+(,?!,+)*
匹配字符串的所有部分,但我不知道如何只匹配数字或名称(名称可以包含数字 - 如例子).谢谢
我在SO上阅读了关于这个主题的所有答案(不仅仅是),但这些都没有解决我的问题.我有数据表:
<p:dataTable
value="#{reportBean.reportData.rows}"
var="row"
styleClass="listBlock dataTableOverflowAuto fullScreenTable"
rendered="#{!reportBean.reportData.grouped}">
<f:facet name="header">
#{msg['report.table.header']}
</f:facet>
<p:column>
#{row.integrationName}
</p:column>
<p:column>
#{row.integrationId}
</p:column>
<p:column>
#{row.service}
</p:column>
<p:columns value="#{reportBean.reportData.columns}"
var="column">
<f:facet name="header">
#{column}
</f:facet>
<h:outputText value="#{row.getData(column)}" escape="false" />
</p:columns>
Run Code Online (Sandbox Code Playgroud)
reportBean上的getReportData()非常简单(无计算):
public ReportDataInterface getReportData() {
return reportData;
}
Run Code Online (Sandbox Code Playgroud)
返回的数据是:
public class NoneGroupedReportData implements ReportDataInterface {
private int counter = 0;
Logger logger = LoggerFactory.getLogger(getClass());
private List<String> columns = new LinkedList<String>();
public void addRow(Row row) {
addColumns(row);
}
public List<String> getColumns() {
counter++;
logger.debug("getColumns called {} times", counter); …
Run Code Online (Sandbox Code Playgroud) 我有一个与此线程中描述的类似问题:Show/hide JSF2 form using selectBooleanCheckbox。但该线程中提出的解决方案对我不起作用。我想要一个可以通过选中复选框来扩展的表单。
我的设置如下:
<p:selectBooleanCheckbox id="lookupCheck" value="#{addressDetailBean.addQLookup}">
<p:ajax event="change" update="qlookupcolumn1" process="@this"/>
</p:selectBooleanCheckbox>
<p:outputPanel id="qlookupcolumn1" rendered="#{addressDetailBean.addQLookup}">
<h2>#{msg['qloookup.new.title']}</h2>
<ui:include src="/WEB-INF/includes/integration/qlookupDetailColumns.xhtml">
<ui:param name="bean" value="#{addressDetailBean}" />
</ui:include>
</p:outputPanel>
Run Code Online (Sandbox Code Playgroud)
但是当我检查复选框表单时,未呈现。Bean 被调用并且属性被正确设置,我可以在响应中看到表单,但它没有呈现在页面上。
可能出了什么问题?
java ×7
hibernate ×3
jpa ×3
jsf ×2
many-to-many ×2
primefaces ×2
choice ×1
h2 ×1
jointable ×1
messages ×1
orm ×1
performance ×1
properties ×1
regex ×1
spring ×1