JPA在表中创建了太多字段
我试图使用Spring Boot JPA将某些实体映射到MySQL数据库中的表格。我对其中一个表有问题,因为添加了一个太多的外国钥匙。我强调了图片中的列。我想问题可能与教程表与其他3个表有一对一或多个关系的事实有关,但我不确定
@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;
}
教程是出现问题的表,因为出现了4个外国键。而不是两个
@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<>();
}
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<>();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试以下更改:
@joincolumn(name =“ fure_id”)
来自furet
,然后放在tutorial
@ joincolumn(name =“ category_id”)
来自类别
,并将其放入tutorial
:以获取更多信息,请查看此处: baeldung-冬眠一
Try this changes:
@JoinColumn(name = "author_id")
fromAuthor
and place inTutorial
:@JoinColumn(name = "category_id")
fromCategory
and place it inTutorial
as well:To get more information look here: Baeldung - Hibernate One to Many