简体   繁体   中英

JPA : Mapping a OneToOne Long value (not an entity) to another tables column which is not a primary key

So I have an entity which has a one to one relationship to an existing database in the table. The field I want it mapped to is not a primary key, however. How can I achieve this? What do I add to the below?

@Entity
public class MyEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @OneToOne
    private Long otherTableField;
}

You need to explicitly specify the @JoinColumn with referencedColumnName :

@OneToOne
@JoinColumn(name = "other_table_non_primary_id", referencedColumnName= "non_primary_id"
private OtherTable otherTableField;

Also, the type has to be OtherTable not Long.

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