What is referencedColumnName used for in JPA?

前端 未结 5 600
不知归路
不知归路 2020-11-29 19:42

In JPA there is an attribute called referencedColumnName that can be set on @JoinColumn, @PrimaryKeyJoinColumn what is the idea behind this setting

5条回答
  •  时光取名叫无心
    2020-11-29 20:15

    "referencedColumnName" property is the name of the column in the table that you are making reference with the column you are anotating. Or in a short manner: it's the column referenced in the destination table. Imagine something like this: cars and persons. One person can have many cars but one car belongs only to one person (sorry, I don't like anyone else driving my car).

    Table Person
    name char(64) primary key
    age int

    Table Car
    car_registration char(32) primary key
    car_brand (char 64)
    car_model (char64)
    owner_name char(64) foreign key references Person(name)

    When you implement classes you will have something like

    class Person{
       ...
    }
    
    class Car{
        ...
        @ManyToOne
        @JoinColumn([column]name="owner_name", referencedColumnName="name")
        private Person owner;
    }
    

    EDIT: as @searchengine27 has commented, columnName does not exist as a field in persistence section of Java7 docs. I can't remember where I took this property from, but I remember using it, that's why I'm leaving it in my example.

提交回复
热议问题