问题标签 [hibernate-session]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
java - 根据数据库中的数据检查对象的内存版本
我有一个 Hibernate 项目,其中调用update()
需要将内存中修改后的对象与已保存到数据库中的数据进行比较。例如,我的业务逻辑指出,如果记录“生效”(生效日期是今天或更早),则更新无法更改生效日期。为了做到这一点,我有以下代码(有点长而且涉及):
经理
道
客户代码
在我在管理器中添加evict()
呼叫之前,我注意到管理器的行为方式出乎意料。为了更新记录,我首先必须通过调用来获取该记录findById()
,这会将记录放入会话缓存中。我会对该对象进行更改,然后调用updateRecord()
它将调用findById()
以获取(据称)持久数据。我意识到第二次调用findById()
不会查看数据库数据,而只是从缓存中提取对象。这将导致我oldEffectiveDate
始终与我新更改的日期相同,因为record
并且oldRecord
将是完全相同的对象。
为了解决这个问题,我添加了evict()
对MemberRecord
. 在我做出那个改变之后,MemberRecordDAO
当它调用时我会抛出一个异常,上面uniqueResult()
写着AssertionFailed: possible nonthreadsafe access to session
. 当我运行调试器时,我看到两者LogicManager
都MemberRecordDAO
使用相同的Session
,这是我认为正确的。
所以,我的问题:
- 我的想法/算法是否正确?是
evict()
正确的做法吗?有没有更好的办法?我对 Sessions、缓存或evict()
. 在处理线程问题之前,我想确保这个逻辑是正确的。 - 为什么
Session
从 DAO 访问不是线程安全的?
hibernate - 休眠删除项目
当我尝试使用 HQL 删除一个对象时,我使用了休眠session.CreateQuery().executeUpdate()
,它返回 1 个项目被删除。但是,在数据库中,没有任何内容被删除。之后executeUpdate
,我也做了冲洗。
谁能给我一些关于这里有什么问题的建议?
java - Hibernate session.load 不填充对象的 arraylist 属性
我在使用 hibernate 的 session.load 函数时遇到了问题。我尝试根据一个ID获取一个对象,它返回了一个好的对象,但实例中只设置了原始属性。我有一个属性是一个集合(映射到某个其他对象),但是在集合之后没有检索到它,并且集合的字段为空。
有谁知道为什么提取不能按预期工作?
谢谢
hibernate - Hibernate, session, lazyloading
I am developing a Java web application using Hibernate and JSF/primefaces. am sometimes getting errors like
1) an object with same identifier is already associated with session.
2) failed to load lazy initialization *,no session exist or session is already closed.
I know this is due to improper coding in my app. this is the way am doing aap:
When a user requests for a page(Let it be a list of Employees). the user will get the employee list page(empployeeList.xhtml) EmployeeListMBean is the managed bean for this page. in the managed bean in the constructor, am calling a method populateEmployees(). populateEmployee() will use the EmployeeDao method getAllEmployee() to getAllemployees.
Employee Class goes here:
here is my EmployeeDao:
Question 1) here, in the methods I am closing the session and then return back the result to managedbeans. so in the employee listing page the table list out name dob dateOfHire. and I have a buutton view more detail. on clicking this button, I want to display all projects of selected employee working on using the same managedbeans but, it is giving me error(2), failed to lazyload, no session or session already closed. if I keep the session opened in the getemployeeMethod of dao, I guess that may lead to a memory leak problem, or someother problem. is it so? also, I have tried lazy and eager loading. please give me a clear idea when/how to use these type of fetching. How can I solve this? can I go for filters or facelisteners for solving this?
Question 2) if am trying to edit a project of an employee, and update using session.saveorupadte(), merge(),flush(), am getting an error like this, "an object with same identifier is already associated with session" How can I solve this?
Question 3) I know that sessionfactory is resource consuming. so only single instance is enough for one app. but what about session? for a SINGLE USER of app, only one session is needed? please tell me good strategy for developing such an app.
Thanking you all :)
hibernate - 在单个事务中将数据持久保存在多个表中
在下面的代码片段中,我试图保留两个实体——Account 和 AccountDetails。我希望这是原子的。这意味着,Account 和 AccountDetails 实体都应该保存在一个事务中。我无法实现它。
请注意,AccountDetails 表是指具有外键 (account.id) 的 Account 表。
如果我尝试将它们保持在同一个事务中,我会遇到死锁。否则,我需要两个具有不同会话的不同事务。
问题:
- 我怎样才能避免死锁?为什么这里会发生死锁?毕竟,从逻辑上讲, AccountDetails不需要对Account进行锁定来持久化。
java - Hibernate 会话中持久对象的状态
关于 Hibernate,我有一个可能非常简单的问题:
当我有一个对象的持久实例,并且其中一些对象的属性在相同的内部进行了修改Session
时,这些更改对我来说也是可见的吗?
还是我必须要refresh
对象才能获得它的当前状态?
我想扩展我的问题:如果这些更改不在同一内部进行Session
怎么办?如果它们是由Session
同一个提供的,该SessionFactory
怎么办?
hibernate - 如何在 Hibernate 会话中管理未保存实体的映射?
在我的实体 ( myGroup : Group
) 上,我有一个集合属性 ( members : HashMap<Long, GroupMember>
),我希望允许用户向地图添加多个新的(未持久化GroupMember
的) myGroup.members
。
我为集合属性使用哈希图的原因是为了确保集合的使用是高性能的(即
GroupMember
在业务逻辑执行期间必须能够通过其 ID 快速多次获取 a)。我不想立即保留它们的原因是支持添加的还原。用户必须能够操作
myGroup.members
映射,添加和删除成员直到满意,然后再持久化(在 Hibernate 会话中保存实体并刷新)。
问题是GroupMember
必须为 new 分配一个 ID 为零,以便 Hibernate 检测到它们未保存。(*) 因为成员位于映射中,由 ID 键入,这意味着只能添加一个新成员保存或还原。添加第二个 ID 为 0 的新成员会简单地覆盖(引用)第一个成员。
我的问题是如何最好地调整我的代码解决方案以支持将多个未保存的实体添加到集合(myGroup.members
)中,同时保持使用哈希映射或其他映射/字典的性能优势?
注意:我已经尝试过一种 hack,其中 ID 来自GroupMember
实例中数据的唯一组合,然后,在保存之前,ID 设置为零,以便 Hibernate 检测到它需要被持久化,但它是不优雅,我还没有找到让它可靠工作的方法。
* 我不完全理解 Hibernate 在这方面的行为。
Group
以下实体的缩写代码:
java - 为什么在一些有限的调用后数据库调用休眠
这是问答式的。
在使用 SessionFactoryUtils.getSession 获取会话并执行我的查询时,它只能调用有限的计数,并且将在该限制计数之后等待调用。为什么会发生?
hibernate - Grails withNewSession 不刷新
在 grails 域中实现了beforeDelete
如下
但是客户商店的空值不会保存到数据库中。
如果我添加手动会话刷新
它有效,客户商店值在数据库中为空。
这是 Grails 错误还是我误解了文档?不是说withNewSession
自动刷机吗?
spring - 如何理解 PROPAGATION_REQUIRED
我仍然不明白以下内容:
两者xxx
和finder.find
都在 tx 控制下并且需要传播,但我发现只有第一个修改提交到数据库。当我删除该find
方法时,不会提交任何修改。为什么是这样?
好的,这是我的代码:
QuestionairService中的xxx方法:
这是我的配置