Entity mapping is creating unwanted column on the fly

Trevor_zam :

I have 2 tables (User and Feed) linked by a foreign key. Following that, I am using Spring Boot and Hibernate to make a query to just print out all the values in Feed table. But when it comes to the foreign key and its value, my Entity seems to be going wrong where it creates a new column on the fly when I already have a column for the foreign key.

Can I please know what I am doing wrong? New to this JPA setup. Confused as to whether I should even create my schema first or let JPA just handle things according to my Entity setups. Clearly I am missing something vital this but just can't place a finger on it. Please assist.

Question is how do I map to the foreign key? As in map Feed table's foreign key 'owner_name' to User table's 'username' on the Entity?

Table structures

enter image description here

Entities

User Entity

@Entity
public class User {
    @Id
    @Column(name = "username")
    public String username;

    @Column(name = "email")
    public String email;

    @Column(name = "password")
    public String password;

    @Basic
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "created_at")
    private Date createdAt;

    @Column(name = "online_status")
    private long onlineStatus;

    @Column(name = "account_status")
    private long accountStatus;

    //updated this based on Gopi's suggestion
    @OneToMany(mappedBy = "ownerName")
    private List<Feed> feeds;
}

Feed Entity

@Entity
public class Feed {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "feed_id")
    private long feedId;

    @Column(name = "title")
    private String title;

    @Basic
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "created_at")
    private Date createdAt;

    @Basic
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "updated_at")
    private Date updatedAt;

    @ManyToOne
    @JoinColumn(name = "username")
//    @Column(name = "owner_name")
    private User ownerName;
}

My query to just get all the Feed data where I do get all data less the value for foreign key which comes out as null. (expecting to get the foreign key value instead).

My controller is calling this method where I get all results less the foreign key value.

public interface FeedRepository extends JpaRepository<Feed, Long> {
    @Query("select f from Feed as f order by f.createdAt desc")
    List<Feed> getCurrentFeeds();
}

Test values inside the tables.

User table data

enter image description here

Feed table data

enter image description here

If I run my current code, I end up with an additional column on the fly as follows which I do not want as mentioned above.

enter image description here

Alexandar Petrov :

Hi this is not surprising, you have specified as a name attribute to the @JoinColumn the column that is actualy referenced. You need to specify the foreign key column in the Feed table which is the "owner_name". The correct complete definition of the @JoinColumn would be:

@JoinColumn(name="owner_name",referencedcolumn = "username")
    private User ownerName;

Where you don't actualy need to define the referencedcolumn, but I have defined it for completion so that you understand what is what.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=152159&siteId=1