首页 \ 问答 \ 具有聚合属性的EJB3 / JPA实体(EJB3/JPA entity with an aggregated attribute)

具有聚合属性的EJB3 / JPA实体(EJB3/JPA entity with an aggregated attribute)

我想知道是否有办法在One2Many关系中获得One Side的一个领域,它是Many方面的聚合。

我们来看下面的例子:

@Entity
public class A {
 @Id
 private Long id;
 @OneToMany (mappedBy="parentA")
 private Collection<B> allBs;

 // Here I don't know how to Map the latest B by date
 private B latestB;
    // Acceptable would be to have : private Date latestBDate;
}

@Entity
public class B {
 @Id
 private Long id;
 private Date date;
 @ManyToOne (targetEntity=A.class)
 private A parentA;
}

我的问题是如何在A实体对象中进行字段latestB的映射而不进行任何去规范化(不与触发器/侦听器保持同步)?

也许这个问题给出了一些答案,但实际上我不明白它是如何工作的,因为我仍然希望能够获取所有的子对象。

感谢阅读/帮助。

PS:我使用hibernate作为ORM / JPA提供程序,因此如果不存在JPA解决方案,可以提供Hibernate解决方案。
PS2:或者只是告诉我,我不应该这样做(当然有论据);-)


I wanted to know if there is a way to get in a One2Many relationship a field of the One side that is an aggregate of the Many side.

Let's take the following example:

@Entity
public class A {
 @Id
 private Long id;
 @OneToMany (mappedBy="parentA")
 private Collection<B> allBs;

 // Here I don't know how to Map the latest B by date
 private B latestB;
    // Acceptable would be to have : private Date latestBDate;
}

@Entity
public class B {
 @Id
 private Long id;
 private Date date;
 @ManyToOne (targetEntity=A.class)
 private A parentA;
}

My question is how can I make the mapping of the field latestB in the A entity object without doing any de-normalization (not keeping in sync the field with triggers/listeners)?

Perhaps this question gives some answers, but really I don't understand how it can work since I still want to be able to fetch all childs objects.

Thanks for reading/helping.

PS: I use hibernate as ORM/JPA provider, so an Hibernate solution can be provided if no JPA solution exists.
PS2: Or just tell me that I should not do this (with arguments of course) ;-)


原文:https://stackoverflow.com/questions/3709010
更新时间:2022-04-13 20:04

最满意答案

哇,我终于解决了这个问题!

看来,即使我没有在日志文件中看到它(参见我的上一条评论),WCExtensionDelegate的init方法在唤醒应用程序时也能很好地调用。

所以我只需要将WCSession设置块移动到init方法中:

- (id) init
{
    if (self = [super init]) {

        // Setup the WatchConnectivity session
        WCSession *session = [WCSession defaultSession];
        session.delegate = self;
        [session activateSession];
    }

    return self;
}

而且它暂时工作正常......


Wow, I finally resolved the issue!

It seems that, even if I didn't see it in the log files (see my last comment), the init method of WCExtensionDelegate is well called when waking up the app.

So I just had to move the WCSession setting bloc into the init method :

- (id) init
{
    if (self = [super init]) {

        // Setup the WatchConnectivity session
        WCSession *session = [WCSession defaultSession];
        session.delegate = self;
        [session activateSession];
    }

    return self;
}

And for the while it works fine...

相关问答

更多
  • 我在iPhone设置中关闭了手表应用程序 I turned off the watch app in the iPhone setting
  • 几件事情要解压缩: 并发症并不意味着经常更新(思考分钟,而不是秒) - 这是为了保存电池。 ProviderUpdateRequester专为(通常不频繁)不定期更新(如通过聊天应用程序发送的消息)而设计。 时间依赖并发症 - 这里没有“更新”,但Wear提供了开发人员从某个时间开始向上/向下计数以及显示日期相关字段(世界时钟,月中的某天)而无需提供者将系统更新全部发送时间。 对于最后一篇,请参考ComplicationText.TimeDifferenceBuilder和ComplicationText. ...
  • 在WatchKit应用程序中没有UITableView这样的东西。 相反,您必须使用WKInterfaceTable 。 在继续之前,我还建议您阅读WatchKit编程指南中的文档。 它将使您更好地了解作为有抱负的Apple Watch开发人员可用的所有工具集。 WKInterfaceTable 一旦你了解了WKInterfaceTable ,你就会很快发现为什么你的方法存在缺陷有两个原因。 首先,您没有reloadData方法。 WatchKit中的替代方法是setNumberOfRows(_:withR ...
  • 由于您实际上可以选择手表上的复杂功能,因此听起来像项目明智的一切都设置正确。 由于没有达到断点,听起来像复杂功能找不到您的自定义控制器。 由于您使用的是swift,因此您需要指定控制器的模块。 尝试将手表应用程序的plist中的CLKComplicationPrincipalClass值更改为: $(PRODUCT_MODULE_NAME).ComplicationController Since you are actually able to select the complication on th ...
  • Apple推荐的在iPhone和手表之间共享信息的方法是通过应用程序组使用共享对象: Apple Watch Programming Guide ,请参阅“使用包含iOS应用程序共享数据”一章。 因此,在设置共享应用程序组之后,您可以使用AppDelegate的applicationDidEnterBackground和applicationWillEnterForeground (或适合您需要的类似方法)在watchkit扩展可以读取的共享对象中写入信息: AppDelegate中 func applic ...
  • 不要将hg数字与更改的实际版本号混淆。 (即,在一个仓库中更改88与另一个仓库中的更改88不同,除非它们具有相同的签名代码。日志编号是用于在单个仓库中引用变更集的便捷捷径,但不会在回购中转换)。 如果您有3个不同的存储库,那么几乎不可能避免合并。 通常,hg在自动合并方面做得很合理。 如果你设置一个像样的hgmerge来使用你最喜欢的3路差异设置,即使是偶尔的手动合并也不是那么痛苦。 但是,如果您想尽可能避免合并,则需要选择一个存储库作为“主要”存储库,并从主存储库中进行另一个存储库推送和拉取。 二级回购的 ...
  • 据我所知, topic.last_update(@section.topics) 应该: topic.last_update(topic) From what I can tell, topic.last_update(@section.topics) should be: topic.last_update(topic)
  • 不可能。 因为Apple Document for watchkit明确提到“ WatchKit扩展不支持后台执行模式,所以它们仅在用户与Apple Watch上的相应应用程序交互时运行。 ” 您可以参考苹果文档Link to Apple Doc 阅读直接与您的iOS应用程序通信 。 Not possible. Because Apple Document for watchkit clearly mentions that "WatchKit extensions do not support backg ...
  • 哇,我终于解决了这个问题! 看来,即使我没有在日志文件中看到它(参见我的上一条评论),WCExtensionDelegate的init方法在唤醒应用程序时也能很好地调用。 所以我只需要将WCSession设置块移动到init方法中: - (id) init { if (self = [super init]) { // Setup the WatchConnectivity session WCSession *session = [WCSession defaul ...
  • Device Motion( startDeviceMotionUpdatesToQueue )尚未在WatchOS2中可用( deviceMotionAvailable返回false),可能加速度计可以帮助你startAccelerometerUpdatesToQueue加速度计startAccelerometerUpdatesToQueue Device Motion (startDeviceMotionUpdatesToQueue) is not available in WatchOS2 yet ( ...

相关文章

更多

最新问答

更多
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • linux的常用命令干什么用的
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • Java中的不可变类(Immutable class in Java)
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • 绑定属性设置器未被调用(Bound Property Setter not getting Called)
  • linux ubuntu14.04版没有那个文件或目录
  • 如何使用JSF EL表达式在param中迭代变量(How to iterate over variable in param using JSF EL expression)
  • 是否有可能在WPF中的一个单独的进程中隔离一些控件?(Is it possible to isolate some controls in a separate process in WPF?)
  • 使用Python 2.7的MSI安装的默认安装目录是什么?(What is the default installation directory with an MSI install of Python 2.7?)
  • 寻求多次出现的表达式(Seeking for more than one occurrence of an expression)
  • ckeditor config.protectedSource不适用于editor.insertHtml上的html元素属性(ckeditor config.protectedSource dont work for html element attributes on editor.insertHtml)
  • linux只知道文件名,不知道在哪个目录,怎么找到文件所在目录
  • Actionscript:检查字符串是否包含域或子域(Actionscript: check if string contains domain or subdomain)
  • 将CouchDB与AJAX一起使用是否安全?(Is it safe to use CouchDB with AJAX?)
  • 懒惰地初始化AutoMapper(Lazily initializing AutoMapper)
  • 使用hasclass为多个div与一个按钮问题(using hasclass for multiple divs with one button Problems)
  • Windows Phone 7:检查资源是否存在(Windows Phone 7: Check If Resource Exists)
  • 无法在新线程中从FREContext调用getActivity()?(Can't call getActivity() from FREContext in a new thread?)
  • 在Alpine上升级到postgres96(/ usr / bin / pg_dump:没有这样的文件或目录)(Upgrade to postgres96 on Alpine (/usr/bin/pg_dump: No such file or directory))
  • 如何按部门显示报告(How to display a report by Department wise)
  • Facebook墙贴在需要访问令牌密钥后无法正常工作(Facebook wall post not working after access token key required)
  • Javascript - 如何在不擦除输入的情况下更改标签的innerText(Javascript - how to change innerText of label while not wiping out the input)
  • WooCommerce / WordPress - 不显示具有特定标题的产品(WooCommerce/WordPress - Products with specific titles are not displayed)