JPA OneToOne 关联,其中 2 个实体使用复合主键但使用不同的列名称?

2024-01-17

我们正在尝试将 Hibernate 与数据库一起使用,该数据库使用lot复合键的使用一直让我们很头疼。 不幸的是,我们无法更改架构,因此我们必须在字段之间进行大量额外的映射。我们仅限于使用 JPA 1.0 和 Hibernate 3.3。

最大的问题 到目前为止,我们使用 2 个值的复合键来处理两个实体之间的一对一关联,其中表具有不同的名称 对于这些列(数据库的命名约定是在每列上都有特定于表的前缀。)

每当我们执行查询时,都会收到此异常:

Caused by: org.hibernate.TypeMismatchException Provided id of the wrong type for class com.business.entity.InvestorIssuerEmailEntity.  
Expected: class com.business.entity.InvestorIssuerEmailEntityPK, got class com.business.entity.InvestorIssuerEntityPK; 

这些表的两个类 InvestorIssuerEntity 和 InvestorIssuerEmailEntity 有一个可选的 @OneToOne 关联 (某些情况下 InvestorIssuer 在 InvestorIssuerEmail 中没有匹配记录):

@IdClass(InvestorIssuerEntityPK.class)
@Table(name = "T090_INVESTOR_ISSUER")
@Entity
InvestorIssuerEntity
    @Column(name = "T090_091_INVESTOR_ID", nullable = false, insertable = true, 
updatable = true, length = 18, precision = 0)
    @Id
    private Long investorId;

    @Column(name = "T090_102_ISSUER_ID", nullable = false, insertable = true, 
updatable = true, length = 18, precision = 0)
    @Id
    private Long issuerId;

(other fields omitted)

    @OneToOne(optional = true)
    @JoinColumns(value = {
            @JoinColumn(name="T090_091_INVESTOR_ID", referencedColumnName = "T284_INVESTOR_ID", nullable = false, insertable = false, updatable = false),
            @JoinColumn(name = "T090_102_ISSUER_ID", referencedColumnName = "T284_ISSUER_ID", nullable = false, insertable = false, updatable = false)
    })
    @NotFound(action = NotFoundAction.IGNORE)
    private InvestorIssuerEmailEntity investorIssuerEmail;

... 

InvestorIssuerEntityPK 
    @Id
    @Column(name = "T090_091_INVESTOR_ID", nullable = false, insertable = true, 
updatable = true, length = 18, precision = 0)
    private Long investorId;

    @Id
    @Column(name = "T090_102_ISSUER_ID", nullable = false, insertable = true, 
updatable = true, length = 18, precision = 0)
    private Long issuerId;

...

@IdClass(InvestorIssuerEmailEntityPK.class)
@Table(name = "T284_INVESTOR_ISSUER_EMAIL")
@Entity
InvestorIssuerEmailEntity
    @Column(name = "T284_INVESTOR_ID", nullable = false, insertable = true, 
updatable = true, length = 18, precision = 0)
    @Id
    private Long investorId;

    @Column(name = "T284_ISSUER_ID", nullable = false, insertable = true, 
updatable = true, length = 18, precision = 0)
    @Id
    private Long issuerId;

...

InvestorIssuerEmailEntityPK 

    @Column(name = "T284_INVESTOR_ID", nullable = false, insertable = true, 
updatable = true, length = 18, precision = 0)
    @Id
    private Long investorId;

    @Column(name = "T284_ISSUER_ID", nullable = false, insertable = true, 
updatable = true, length = 18, precision = 0)
    @Id
    private Long issuerId;  

我尝试通过使用与两个实体的 @EmbeddableId 相同的类,然后使用 @AttributeOverrides 来解决类型不匹配问题,如下所示:

@Id
@EmbeddedId
@AttributeOverrides({
        @AttributeOverride(name = "investorId",
                column = @Column(name = "T284_INVESTOR_ID", nullable = false, insertable = true, updatable = true, length = 18, precision = 0)),
        @AttributeOverride(name = "issuerId",
                column = @Column(name = "T284_ISSUER_ID", nullable = false, insertable = true, updatable = true, length = 18, precision = 0))
})
private InvestorIssuerId investorIssuerId;

我只对这两个实体进行了更改,但仍然对其他实体使用 @IdClass 方法(是否是仅对您的实体使用 @IdClass 或 @EmbeddableId,而不是两者都使用的情况?)

我们最终遇到了其他问题,例如“实体映射中的重复列”,因此我们恢复了这种方法,看看是否有其他解决方法。

有没有人有任何解决方案来解决这个问题?我查看了 StackOverflow,但没有遇到关联中使用的复合键具有不同名称的情况。

Note:即使尝试下面的建议后,我们仍然收到此错误:org.hibernate.MappingException:实体映射中的重复列:com.business.entity.InvestorIssuerEntity列:T090_091_INVESTOR_ID(应使用 insert="false" update="false 进行映射”)

我什至从 InvestorIssuerEntity 中删除了所有关联,并且still遇到了同样的问题。当我删除复合键类中的 @Column 注释时,该错误才消失。当然,查询不起作用,因为投资者 ID 没有映射!我不明白whereHibernate 正在查找“映射中的重复列”,因为除了复合键之外,我已经删除了所有提到的 T090_091_INVESTOR_ID 。

我们在 InvestorIssuerEntity 中还有其他关联,它们对相同的主键进行联接,但关联实体的组合键中也有附加列。一旦使用@EmbeddedId,您是否应该将它们用于所有实体?我们仍然对其他类使用@IdClass。但这是如何导致任何地方出现“重复列”的呢?


看来我为你的情况找到了一个可行的解决方案:

@Entity
public class InvestorIssuerEntity {
    @EmbeddedId 
    private InvestorIssuerEntityPK investorIssuerEntityPK;

    @OneToOne(optional=true, mappedBy="investorIssuerEntity")
    private InvestorIssuerEmailEntity investorIssuerEmailEntity;
}

@Entity
public class InvestorIssuerEmailEntity {
    @EmbeddedId @AttributeOverrides({
        @AttributeOverride(name="investorId", column=@Column(name="T02_INV_ID")),
        @AttributeOverride(name="issuerId", column=@Column(name="T02_ISS_ID"))
    })
    private InvestorIssuerEntityPK investorIssuerEntityPK;

    @OneToOne(optional=true) @PrimaryKeyJoinColumns({
        @PrimaryKeyJoinColumn(name="T02_ISS_ID", referencedColumnName="T01_ISS_ID"), 
        @PrimaryKeyJoinColumn(name="T02_INV_ID", referencedColumnName="T01_INV_ID")
    })
    private InvestorIssuerEntity investorIssuerEntity;
}

@Embeddable
public class InvestorIssuerEntityPK implements Serializable {
    private static final long serialVersionUID = -1176248537673293674L;

    @Column(name="T01_INV_ID")
    private Long investorId;

    @Column(name="T01_ISS_ID")
    private Long issuerId;
}

它生成以下 DDL,这似乎就是您正在寻找的内容:

create table InvestorIssuerEmailEntity (
    T02_INV_ID bigint not null,
    T02_ISS_ID bigint not null,
    primary key (T02_INV_ID, T02_ISS_ID)
)

create table InvestorIssuerEntity (
    T01_INV_ID bigint not null,
    T01_ISS_ID bigint not null,
    primary key (T01_INV_ID, T01_ISS_ID)
)

alter table InvestorIssuerEmailEntity 
    add constraint FKC2FBCC4E1E26612E 
    foreign key (T02_INV_ID, T02_ISS_ID) 
    references InvestorIssuerEntity
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

JPA OneToOne 关联,其中 2 个实体使用复合主键但使用不同的列名称? 的相关文章

随机推荐

  • spring data couchbase中的id是如何自动生成的?

    我想在 spring data couchbase 中为我的文档创建 ID 自动生成 Spring 文档对此有步骤 https docs spring io spring data couchbase docs current refere
  • 在 Laravel 查询生成器中进行投射

    我想要castLaravel 查询生成器中的 orderBy 因为我的price is varchar type 所以当它排序时 结果离我想要的很远 我的脚本是这样的 DB table test gt where gt orderBy pr
  • 如何在javascript中组合数组

    您好 我想根据数组中的唯一项合并数组 我拥有的对象 totalCells 在这个totalCells数组中我有几个像这样的对象 totalCells cellwidth 15 552999999999999 lineNumber 1 cel
  • 如何在选项卡关闭时删除 jquery cookie

    我的 cookie 工作正常 我没有提及日期 因此当浏览器窗口关闭时 cookie 就会被删除 但是当我关闭浏览器窗口中的选项卡时 cookie 不会被删除 并且当我打开网站时会打开相同的保留的 cookie 状态页面 当用户关闭浏览器选项
  • Java,循环结果集

    在Java中 我有一个这样的查询 String querystring1 SELECT rlink id COUNT FROM dbo Locate GROUP BY rlink id 表 rlink id 有以下数据 Sid lid 3
  • 迭代WPF Datagrid中的所有单元格[重复]

    这个问题在这里已经有答案了 可能的重复 WPF DataGrid 如何在 DataGrid 中迭代以获取行和列 https stackoverflow com questions 1295023 wpf datagrid how do yo
  • Tailwind css,如何设置默认字体颜色?

    我在我的项目中使用 tailwind css 由于我们的应用程序样式 我们使用默认字体颜色 但是我似乎找不到如何在 tailwind 中执行此操作 文档 https tailwindcss com docs text color页面只讨论了
  • Prolog 是否有像 Common Lisp 一样的条件和重启系统?

    Common Lisp 允许异常处理条件并重新启动 http www gigamonkeys com book beyond exception handling conditions and restarts html 粗略地说 当函数抛
  • 解析线性方程的系数

    在java中 我试图找到线性方程的系数 以在我的计算器应用程序中找到线性方程的解 例如 3x 2 6x 3 2 4x 我渴望得到的是 x 的系数和形式的常数ax b 0 在这个特定的例子中 coefficient 19 constant 8
  • 将内存中的 HTML 保存到 S3 AWS Python Boto3

    import boto3 from io import StringIO s3 boto3 client s3 display Altair Charting buff StringIO display save str obj html
  • 如何启动 Matlab 分析器

    我切换到 Matlab 2012b 从 2011a 但未能找到如何在新的 matlab gui 中启动分析器 gui GUI 选项仍然存在 在编辑器选项卡中 一旦函数崩溃 您将能够指定输入参数
  • 我们可以向 super() 传递什么参数?

    我创建了一个Vehicle类并且还想有一个Car从它派生的类调用父构造函数来设置name and color 但是我收到这个错误 super takes at least 1 argument 0 given 这是我的代码 class Ve
  • Promise 构造函数的返回值

    考虑下面两个例子 TEST 1 function test1 return new Promise function return 123 test1 then function data console log DATA data ret
  • 如何取消合并单元格 EPPlus?

    我正在尝试根据表列的数量取消合并并重新合并更短或更长的范围 我使用了下面的代码 但它似乎不起作用 tableSheet Cells C1 J1 Merge false 任何帮助将不胜感激 您运行的是 EPP 4 0 1 吗 如果是这样 则这
  • 一个属性可以访问另一个属性吗?

    我刚刚接触Python 这是一个关于类的逻辑和实现的非常普遍的问题 我对这个问题的基本水平表示歉意 但希望它对其他人也有用 这里有一些上下文可以使它更清楚 Context 我想创建一个代表图像的类 该图像包括 3 个波段 R G B 与 3
  • 从udf访问hdfs文件

    我想通过 udf 调用访问文件 这是我的脚本 files LOAD docs in USING PigStorage AS id stopwords id2 file buzz FOREACH files GENERATE pigbuzz
  • 依赖下拉框 CakePHP 3

    我创建了一个国家 城市和客户表 我试图确保当我从下拉列表中添加新客户时 我可以选择一个国家 然后选择与该国家 地区相关的城市 目前我无法从下拉列表中选择任何城市和国家 地区组合 这是我的数据库 CREATE TABLE IF NOT EXI
  • MySql 全天候查询结果

    我需要获取一天中所有时间的数据 即使计数为 0 现在它输出 clicks hour 1 7 2 13 我现在的查询 SELECT count as clicks hour time as hour FROM clicks WHERE DAT
  • DOM 中相邻的文本节点可以用 Javascript 合并吗?

    假设我在网页 DOM 中有一个句子 当我检查它时 它由 3 个文本节点组成 后跟一些元素 例如粗体或斜体 我想将文本节点合并为一个文本节点 因为相邻的文本节点是没有意义的 没有理由拥有它们 有没有办法轻松合并它们 谢谢 看起来Node no
  • JPA OneToOne 关联,其中 2 个实体使用复合主键但使用不同的列名称?

    我们正在尝试将 Hibernate 与数据库一起使用 该数据库使用lot复合键的使用一直让我们很头疼 不幸的是 我们无法更改架构 因此我们必须在字段之间进行大量额外的映射 我们仅限于使用 JPA 1 0 和 Hibernate 3 3 最大