'JPA created too many fields in table

enter image description here

I am trying to map some entities to tables in MySQL database using Spring Boot JPA. I have a problem with one of the tables because in that one too many foreign keys are added. I highlighted the columns in the picture. I suppose that the problem might be linked with the fact that the Tutorial table has either One to Many or Many to Many relations with the other 3 tables, but I am not sure

@Entity(name = "authors")
public class Author {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "author_id")
private Long authorId;

@Column(name = "first_name", nullable = false, length = 100)
private String firstName;

@Column(name = "last_name", nullable = false, length = 100)
private String lastName;

@Column(name = "email", length = 320, unique = true)
private String email;

@Column(name = "job_title", length = 255)
private String jobTitle;

@Lob
@Type(type = "org.hibernate.type.BinaryType")
@Column(name = "profile_picture")
private byte[] profilePicture;

@Column(name = "about", length = 2000)
private String about;

@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "author_id")
private List<Tutorial> tutorials;
}

@Entity(name = "categories")
public class Category {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "category_id")
private Long categoryId;

@Column(name = "category_name", nullable = false, unique = true, length = 100)
private String categoryName;

@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "category_id")
private List<Tutorial> tutorials;
}

@Entity(name = "tutorials")
public class Tutorial {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "tutorial_id")
private Long tutorialId;

@Column(name = "tutorial_title", nullable = false, length = 150)
private String tutorialTitle;

@Column(name = "tutorial_description", nullable = false, length = 2000)
private String tutorialDescription;

@Column(name = "time_to_complete")
private Integer timeToComplete;

@Column(name = "date_published")
private Long datePublished;

@Column(name = "last_updated")
private Long lastUpdated;

@ManyToMany(fetch = FetchType.LAZY,
        cascade = {
                CascadeType.PERSIST,
                CascadeType.MERGE
        },
        mappedBy = "tutorials")
private List<User> users = new ArrayList<>();

@ManyToOne(fetch = FetchType.EAGER)
private Category category;

@ManyToOne(fetch = FetchType.EAGER)
private Author author;


}

Tutorials is the table where the problems appear as 4 foreign keys are generate instead of two

@Entity(name = "users")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id")
private Long userId;

@Column(name = "first_name", nullable = false, length = 100)
private String firstName;

@Column(name = "last_name", nullable = false, length = 100)
private String lastName;

@Column(name = "user_name", nullable = false, unique = true, length = 100)
private String userName;

@Column(name = "age")
private Integer age;

@Column(name = "email", length = 320, unique = true)
private String email;

@ManyToMany(fetch = FetchType.LAZY,
        cascade = {
                CascadeType.PERSIST,
                CascadeType.MERGE
        })
@JoinTable(name = "users_tutorials",
        joinColumns = { @JoinColumn(name = "user_id")  },
        inverseJoinColumns = { @JoinColumn(name = "tutorial_id") })
private List<Tutorial> tutorials = new ArrayList<>();


}


Solution 1:[1]

Try this changes:

  1. remove @JoinColumn(name = "author_id")from Author and place in Tutorial:
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "author_id")
    private Author author;
  1. remove @JoinColumn(name = "category_id")from Category and place it in Tutorial as well:
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "category_id")
    private Author author;

To get more information look here: Baeldung - Hibernate One to Many

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 questioner