简体   繁体   中英

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".

TableA has a OneToOne relationship with TableB. I need to have an entity of TableB on my TableA entity.

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; 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.

@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.

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. 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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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