Anand Rockzz
Anand Rockzz

Reputation: 6658

jpa referencing foreign table instead of foreign column in JoinColumn

App and History with a OneToMany:

@Entity
@Table(name = "app")
@SuppressWarnings("serial")
public class App implements Serializable {
    @Id     @Column(name = "app_id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long appId;
    private String name;
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "app", cascade = CascadeType.PERSIST)
    private Set<History> history = new HashSet<>();
    //get.set
}

@Entity
@Table(name = "app_history")
@SuppressWarnings("serial")
public class History implements Serializable {
    @Id
    @Column(name = "history_id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long historyId;
    @JoinColumn(name = "appId")
    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
    private App app;
    //get.set
}

Seeing in log:

create table app (app_id int8 not null, name varchar(255), primary key (app_id))

create table app_history (history_id int8 not null, app_id int8, primary key (history_id))

alter table app_history add constraint FKeuk3km6r7oklj5xc1ecwvxmkm foreign key (app_id) 
references app

Expecting the above line to be

alter table app_history add constraint FKeuk3km6r7oklj5xc1ecwvxmkm foreign key (app_id) 
references app (app_id)

So, why is (app_id) missing when jpa is trying to create the table?

Notes: I'm on

Upvotes: -1

Views: 606

Answers (2)

user10527814
user10527814

Reputation:

Here is the problem: you are using a MySQL database, but the dialect used by JPA is for PostgreSQL:

spring: 
  main: 
    banner-mode: "off"
  datasource: 
    url: jdbc:mysql://localhost:3306/cifi3?useSSL=false&allowPublicKeyRetrieval=true
    username: tester
    password: tester
  jpa: 
    database-platform: org.hibernate.dialect.PostgreSQLDialect
    show_sql: true
    hibernate:
      ddl-auto: create-drop
      format_sql: true
  data:
    rest:
      basePath: /repo

I suggest you to change database-platform: org.hibernate.dialect.PostgreSQLDialect in database-platform: org.hibernate.dialect.MySQLDialect

Upvotes: 1

BilledTrain380
BilledTrain380

Reputation: 66

Change your

@JoinColumn(name = "appId")

to

@JoinColumn(name = "app_id")

This should work.

Upvotes: 2

Related Questions