首页 \ 问答 \ 如何从Hibernate中的OneToMany Collection中仅选择一些属性?(How to Select only some properties from a OneToMany Collection in Hibernate?)

如何从Hibernate中的OneToMany Collection中仅选择一些属性?(How to Select only some properties from a OneToMany Collection in Hibernate?)

TL; DR

如何获得B集合中只有少数属性的单个A对象?

说明

我正在研究一个遗留项目,在这个项目中,人们有明智的想法将每个关系映射为EAGER。

现在,我们遇到了性能问题。

例如,我有A类,它有一个B对象列表。 该列表已在hibernate中映射为一个包,并且懒得加载(到目前为止,这么好)。 问题是,B急切地加载了整个怪异的字母表:

A.hbm.xml

<hibernate-mapping>
    <class="A" table="a" lazy="false">
        // properties
        <bag name="listOfBs" inverse="true">
            <key column="a_id" [...]>
            <one-to-many class="B" />
        </bag>
</hibernate-mapping>

B.hbm.xml

<hibernate-mapping>
    <class="B" table="b" lazy="false">
        <many-to-one name="a" class="A" column="a_id" />
        // lots of other many-to-one mappings

        // properties

        // lots of one-to-many properties
</hibernate-mapping>

所以我只需要B的4个属性,但它会获取每个相关对象!

为了解决这个问题而不破坏一切,我试图使用hql查询只选择集合的几个列:

"select a, b.field1, b.field2, b.field3, b.field4
 from A a inner join B b where a.id = :id"

但我需要一个唯一的结果,因此以下查询会导致异常。


TL;DR

How can I get a single A object with only a few properties in a B collection?

Explanation

I am working on a legacy project where people had the brillant idea to map every relationship as EAGER.

Now, we are having performance problems.

For instance, I have Class A, that has a list of B objects. That list has been mapped as a bag in hibernate, and loads lazily (so far, so good). The problem is, B eagerly loads the whole freaking alphabet:

A.hbm.xml

<hibernate-mapping>
    <class="A" table="a" lazy="false">
        // properties
        <bag name="listOfBs" inverse="true">
            <key column="a_id" [...]>
            <one-to-many class="B" />
        </bag>
</hibernate-mapping>

B.hbm.xml

<hibernate-mapping>
    <class="B" table="b" lazy="false">
        <many-to-one name="a" class="A" column="a_id" />
        // lots of other many-to-one mappings

        // properties

        // lots of one-to-many properties
</hibernate-mapping>

So I need only 4 properties of B, but it fetches every related object!

In order to solve this without breaking everything, I am trying to use a hql query to select only a few column of the collection:

"select a, b.field1, b.field2, b.field3, b.field4
 from A a inner join B b where a.id = :id"

But I need an Unique Result, so the following query causes an exception.


原文:https://stackoverflow.com/questions/18774101
更新时间:2022-04-15 06:04

最满意答案

您需要注意的是,SQLite文档警告您不要从多个线程访问/写入数据库。 只要您从单个线程访问数据库,您就可以了。 如果该线程是程序的主线程或其他线程并不重要。

请记住,iPhone上编译的SQLite版本的线程模式设置为“多线程”,根据文档 ,“禁用数据库连接和准备好的语句对象的静音。应用程序负责序列化对数据库连接的访问并且准备好了语句,但是启用了其他互斥锁,这样只要没有两个线程同时尝试使用相同的数据库连接,SQLite就可以安全地在多线程环境中使用。“ 因此,如果您决定将此事务放在另一个线程上,请注意您尝试对数据库执行的其他操作。

话虽这么说,我首先按照Yonel的建议切换到“BEGIN”和“COMMIT”。 如果这没有帮助,请将事务移动到另一个线程。 根据我所听到的情况,使用“blob”可能会非常缓慢。


The thing that you need to be aware of is that the SQLite documentation warns you away from accessing/writing to the database from multiple threads. As long as you access the database from a single thread, you'll be fine. It doesn't matter if that thread is your program's main thread or some other thread.

Keep in mind that compiled version of SQLite on the iPhone has its threading mode set to "multi-thread" which, according to the documentation, "disables mutexing on database connection and prepared statement objects. The application is responsible for serializing access to database connections and prepared statements but other mutexes are enabled so that SQLite will be safe to use in a multi-threaded environment as long as no two threads attempt to use the same database connection at the same time." So, if you do decide to put this transaction on another thread, be careful of what else you try to do with the database.

That being said, I'd first follow Yonel's advice and switch to "BEGIN" AND "COMMIT". If that doesn't help, move the transaction to another thread. Working with "blobs" can be pretty slow, from what I've heard.

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。