【问题标题】:JPA @OneToOne with an Entity that is not exported from it's OSGi BundleJPA @OneToOne 具有未从其 OSGi 捆绑包中导出的实体
【发布时间】:2013-10-29 22:11:25
【问题描述】:

我正在尝试在相当复杂的 OSGi 环境中使用 JPA 建立一个简单的外键关系。

我要使用的两个实体的结构如下:

masterbundle
|->org.masterpackage.persistence
   |-> MasterEntityDto.java
slavebundle
|->org.slavepackage.persistence
   |-> SlaveEntity.java

SlaveEntity想参考MasterEntityDto这样的

@Entity(name = "SlaveEntity")
public class SlaveEntity {
  @Id
  @Column(name = "slaveID")
  @GeneratedValue(strategy = GenerationType.AUTO)
  private long id;

  @OneToOne
  @JoinColumn(name = "masterEntity_id")
  private MasterEntity masterEntity;
  // snip..
}

现在,我认为这失败了,因为 masterbundle 没有导出 MasterEntityDto(或其包)。我们正在使用 OSGi 的服务方面,masterBundle 是 provide-interface-ing 一个 使用 Dto 而不是 Dto 的服务。 我在捆绑包开始时看到的异常是org.osgi.framework.BundleException: Unresolved constraint in bundle slavebundle [121]: Unable to resolve 121.8: missing requirement [121.8] osgi.wiring.package;

问题:如何创建从SlaveEntityMasterEntityDto@OneToOne 关系?这在使用 OSGi 服务平台时是不可能的吗?我只公开服务而不是整个包/包?

编辑1 根据要求:MasterEntityDto 没什么特别的。

@Entity(name = "MasterEntityDto")
public class MasterEntityDto {
@Id
@Column(name = "id", length = 128)
private String masterId;
// snip 
}

我希望 JPA 制作一个 SlaveEntity - 表,其中包含 SlaveId 列(这是这个表 PK)和 masterEntity_id 作为外键,指向表 MasterEntityDtoid 列.

【问题讨论】:

  • 你能显示你的MasterEntityDto的代码吗?数据库中的哪一列包含外键?
  • 我用MasterEntityDto 的代码更新了我的帖子。我不确定这是否真的相关。还试图描述表格应该是什么样子。我说的是单向的一对一关系,因为我无法修改MasterEntityDto。我只是想增强它。此外,不用说,实际代码是不同的——更复杂,更渴望这篇文章。这只是它的精髓。

标签: jpa osgi


【解决方案1】:

包含域类(例如 MasterEntityDto)的包确实需要导出,以便 JPA 包可以看到实例化它们。

因此,将此类包与其他包含实现/逻辑代码的包分开非常重要,这些包应该是私有的。

【讨论】:

  • 我明白了。但是,对于 OSGi 服务平台,仅公开服务组件的接口(在<scr:component>-definition 中使用<provide-interface>)。这意味着,如果我想正确使用 JPA,我不能单独依赖服务功能并且需要 OSGi 组件功能来公开某些包(在某些 manifest.mf 中使用 export-package)。正确的?只要确保我正确理解 OSGi+JPA。
  • 是的。添加这句话是为了满足 StackOverflow 最小评论大小规则。
  • 跟进问题。我仍然无法让它工作。我不断收到:[SlaveEntity] uses a non-entity [MasterEntity] as target entity in the relationship… 。但是,正如您所见,MasterEntity 是一个工作实体。这两个实体都在各自的persistence.xmls 中定义。是否有可能仅导出 MasterEntitys 包不会因为它不包含 MasterEntitys persistence.xml? (那将在捆绑包中,而不是在包中)。
猜你喜欢
  • 2021-02-26
  • 2012-06-29
  • 2020-07-15
  • 2018-09-29
  • 2011-03-14
  • 2014-05-02
  • 2014-11-02
  • 2020-05-19
  • 1970-01-01
相关资源
最近更新 更多