您好我的Spring启动应用程序不会以MySql数据库启动。也许我的映射不正常?
@Entity
@Table(name = "tweets")
public class Tweet {
@Id
@GeneratedValue
private long tweetId;
@ManyToOne()
@JoinColumn(name = "user_id", nullable = false)
private User userId;
@OneToMany(mappedBy = "tweet")
private Collection<Like> likes;
@Column(nullable = false)
private String content;
private long score;
public Tweet(){}
public Tweet(User user, String content) {
this.userId = user;
this.content = content;
this.score = 0;
}
public long getTweetId() {
return tweetId;
}
public long getUserId() {
return userId.getUserId();
}
public Collection<Like> getLikes() {
return likes;
}
public String getContent() {
return content;
}
public long getScore() {
return score;
}
}
这是用户表:
@Entity
@Table(name = "Users")
public class User {
@Id
@GeneratedValue
private long userId;
private String email;
@JsonIgnore
private String password;
private String www;
private String firstName;
private String lastName;
@OneToMany(mappedBy = "userId", fetch = FetchType.EAGER)
private Collection<Tweet> tweets;
@OneToMany(mappedBy = "userId", fetch = FetchType.EAGER)
private Collection<Like> likes;
public User() {
}
public User(User user) {
this.userId = user.getUserId();
this.email = user.getEmail();
this.password = user.getPassword();
this.www = user.getWww();
this.firstName = user.getFirstName();
this.lastName = user.getLastName();
}
public User(String email, String password, String www, String firstName, String lastName) {
this.email = email;
// this.password = BCrypt.hashpw(password,BCrypt.gensalt());
this.www = www;
this.firstName = firstName;
this.lastName = lastName;
}
public long getUserId() {
return userId;
}
public String getEmail() {
return email;
}
public String getPassword() {
return password;
}
public String getWww() {
return www;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public Collection<Tweet> getTweets() {
return tweets;
}
public Collection<Like> getLikes() {
return likes;
}
@Override
public String toString() {
return "User{" +
"userId=" + userId +
", email='" + email + '\'' +
", password='" + password + '\'' +
", www='" + www + '\'' +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", tweets=" + tweets +
", likes=" + likes +
'}';
}
}
这里就像桌子一样:
@Entity
@Table(name = "likes")
public class Like {
@Id
@GeneratedValue
private long likeId;
@ManyToOne()
@JoinColumn(name = "user_id", nullable = false)
private User userId;
@ManyToOne()
@JoinColumn(name = "tweet_id", nullable = false)
private Tweet tweet;
public Like() {
}
public Like(User user, Tweet tweet) {
this.userId = user;
this.tweet = tweet;
}
public long getLikeId() {
return likeId;
}
public User getUserId() {
return userId;
}
public Tweet getTweet() {
return tweet;
}
public void setLikeId(long likeId) {
this.likeId = likeId;
}
public void setUserId(User userId) {
this.userId = userId;
}
public void setTweet(Tweet tweet) {
this.tweet = tweet;
}
}
我不得不承认我有点困惑,因为当我使用H2数据库时应用程序启动,但我无法使用Mysql。 application.properties文件没问题,我用另一个项目来修改它,其中我有相同的配置值。
错误如下:
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory