简体   繁体   English

JPA OneToOne-访问主键对象时发生内部错误

[英]JPA OneToOne - An internal error occurred accessing the primary key object

I'm trying to model a very simple OneToOne relationship using JPA (EclipseLink) and I'm geting an exception java.lang.NoSuchFieldException with a description "An internal error occurred accessing the primary key object". 我正在尝试使用JPA(EclipseLink)为一个非常简单的OneToOne关系建模,并且正在获取异常java.lang.NoSuchFieldException,其描述为“访问主键对象时发生内部错误”。

TableA has a OneToOne relationship with TableB. TableA与TableB具有OneToOne关系。 I need to have an entity of TableB on my TableA entity. 我需要在TableA实体上具有TableB的实体。

What I've tried 我尝试过的

@NamedNativeQueries(... ommitted for breviety ...)
@Entity
@Table(name = "TableA")
@Cache(isolation = CacheIsolationType.ISOLATED)
public class TableA implements Serializable {
    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private TableA_Id id; //Code is part of the composite embeded

    @OneToOne
    @JoinColumn(name = "CODE", insertable = false, updatable = false)
    private TableB b;

    //getters + setters 

@NamedNativeQueries(... ommitted for breviety ...)
@Entity
@Table(name = "TableB")
@Cache(isolation = CacheIsolationType.ISOLATED)
public class TableB implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "CODE")
    private String code;

    @Column(name = "DESCRIPTION")
    private String description;

    //getters + setters 

Exception: 例外:

Exception [EclipseLink-0] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: An internal error occurred accessing the primary key object [202].
Internal Exception: java.lang.NoSuchFieldException: b
Descriptor: RelationalDescriptor(com.foo.TableA --> [DatabaseTable(TableA)])

It may be worth noting; 可能值得注意。 there is no foreign key defeined between code on TableA and TableB; TableA和TableB上的代码之间没有伪造的外键; I don't have the ability to change that. 我没有能力改变这种状况。

You have no foreign key in Table A referring to Table B, therefore you cannot have the join column on Table A as you have done. 在表A中没有引用表B的外键,因此您不能像在表A上那样具有连接列。

@OneToOne
@JoinColumn(name = "CODE", insertable = false, updatable = false)
private TableB b;

This statement requires that the join column 'Code' is in Table A – but as it is in Table B it is not found by JPA and therefore the exception. 该语句要求连接列“ Code”在表A中–但是,正如在表B中一样,JPA找不到它,因此是例外。

java.lang.NoSuchFieldException: b

You require the TableB entity as a field in TableA, and you have stated that you cannot add a foreign key to TableA. 您需要将TableB实体作为TableA中的字段,并且您已经声明不能向TableA添加外键。 That should not be a big issue in this case as the join can be on either side of the relationship. 在这种情况下,这应该不是什么大问题,因为联接可以位于关系的任一侧。

Given your restrictions, you can make the relationship bidirectional with the owning side being TableB (The side that contains the join) and the inverse side TableA. 根据您的限制,您可以使关系双向,拥有方为TableB(包含联接的方),而相反方为TableA。

Try the following: 请尝试以下操作:

@Entity
@Table(name = "TableB")
public class TableB ……
//Owning side of the relationship with the @JoinColumn annotation.
    @OneToOne
    @JoinColumn(name = "CODE", insertable = false, updatable = false)
    private TableA tableA;

@Entity
@Table(name = "TableA")
public class TableA ……..
//Inverse side of the relationship with the MappedBy attribute.
    @OneToOne(MappedBy = tableA)
    private TableB tableB;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM