我对Web编程和hibernate / jpa等还有些陌生。 我正在使用maven / jpa / hibernate进行动态Web项目。
我已经成功创建了用户页面和系列页面,但是当我尝试添加评论时出现错误:
ERROR: Table 'webproject.user_series_review' doesn't exist
org.hibernate.exception.SQLGrammarException: could not extract ResultSet
但是我检查了几次,但是我没有这样的表或fk,所以我也不从任何地方调用此表。
休眠地图:
评论:
@Entity(name="series_review")
@Table(name="series_review")
public class SeriesReview {
@Id
@Column(name="id", updatable = false, nullable = false)
private int id;
@Column(nullable=false)
private String review;
@Column(nullable=false)
private Date date;
@ManyToOne
@JoinColumn(name = "fk_user", insertable = false, updatable = false)
private User user;
@ManyToOne
@JoinColumn(name = "fk_series", insertable = false, updatable = false)
private Series series;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
private List<Comment> comments = new ArrayList<Comment>();
用户:
@Entity(name="user")
@Table(name="user")
@NamedQueries({..})
public class User implements Serializable {
@Id
@Column(name="id", updatable = false, nullable = false)
private int id;
private String name;
....
@ManyToMany
@JoinTable(name = "fav_series",
joinColumns = { @JoinColumn(name="fk_user") },
inverseJoinColumns = { @JoinColumn(name="fk_series") })
private List<Series> favoriteSeries = new ArrayList<Series>();
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
private List<SeriesReview> reviews = new ArrayList<SeriesReview>();
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
private List<Comment> comments = new ArrayList<Comment>();
系列:
@Entity
@Table(name = "series")
@NamedQueries({ @NamedQuery(name = "allSeries", query = "SELECT s FROM Series s"),
@NamedQuery(name = "findSeriesByName", query = "SELECT s FROM Series AS s WHERE s.name = :name"),
@NamedQuery(name = "topRatedSeries", query = "SELECT DISTINCT s FROM Series AS s ORDER BY s.imdb_rating DESC"),
@NamedQuery(name = "seriesType", query = "SELECT s FROM Series AS s WHERE s.genre = :type"),
@NamedQuery(name = "findByID", query = "SELECT s FROM Series AS s WHERE s.id = :id")
})
public class Series {
@Id
@Column(name = "id", updatable = false, nullable = false)
private int id;
@Column(nullable = false, unique = true)
private String name;
private String info;
private String start_year;
private String end_year;
private Double imdb_rating;
private int site_rating;
private String video;
private String image;
private String genre;
private String director;
private String cast;
private String creator;
@ManyToMany
@JoinTable(name = "fav_series", joinColumns = { @JoinColumn(name = "fk_series") }, inverseJoinColumns = {
@JoinColumn(name = "fk_user") })
private List<User> fans = new ArrayList<User>();
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
private List<SeriesReview> reviews = new ArrayList<SeriesReview>();
我添加评论代码:
public class ReviewService {
private EntityManagerFactory emf;
private EntityManager em;
private EntityTransaction tx;
public ReviewService() {
emf = Persistence.createEntityManagerFactory("WebProject");
em = emf.createEntityManager();
tx = em.getTransaction();
em.getTransaction().begin();
}
public Boolean insert(SeriesReview review, int userID, int seriesID) {
try {
User user = em.find(User.class, userID);
review.setUser(user);
user.getReviews().add(review);
Series series = em.find(Series.class, seriesID);
review.setSeries(series);
series.getReviews().add(review);
em.persist(review);
tx.commit();
return true;
}
catch(Exception e){
System.out.println(e);
return false;
}
}
}
我的数据库的一部分:
DROP TABLE IF EXISTS `user`;
SET character_set_client = utf8mb4 ;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`email` varchar(255) NOT NULL,
`username` varchar(255) NOT NULL,
`password` varchar(255) DEFAULT NULL,
`photo` varchar(255) DEFAULT NULL,
`role_type` int(11) NOT NULL DEFAULT '0',
`created_at` date NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `email_UNIQUE` (`email`),
UNIQUE KEY `username_UNIQUE` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `series`;
SET character_set_client = utf8mb4 ;
CREATE TABLE `series` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`info` mediumtext,
`start_year` varchar(70) DEFAULT NULL,
`end_year` varchar(70) DEFAULT NULL,
`imdb_rating` decimal(10,0) DEFAULT NULL,
`site_rating` decimal(10,0) DEFAULT NULL,
`video` varchar(255) DEFAULT NULL,
`image` varchar(255) DEFAULT NULL,
`genre` varchar(255) NOT NULL,
`cast` varchar(255) DEFAULT NULL,
`director` varchar(255) DEFAULT NULL,
`creator` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`),
UNIQUE KEY `name_UNIQUE` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `series_review`;
SET character_set_client = utf8mb4 ;
CREATE TABLE `series_review` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`series_id` int(11) NOT NULL,
`review` longtext NOT NULL,
`date` DATETIME DEFAULT NOW(),
PRIMARY KEY (`id`,`user_id`,`series_id`),
KEY `user_id_INDEX` (`user_id`),
KEY `series_id_INDEX` (`series_id`),
CONSTRAINT `fk_series_review_series1` FOREIGN KEY (`series_id`) REFERENCES `series` (`id`),
CONSTRAINT `fk_series_review_user1` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
答案 0 :(得分:0)
由于您没有为此映射定义mapply(rnorm, n = 3, mean = V, sd = VU)
属性
mappedBy
Hibernate正在寻找您没有的联接表
将其更改为
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
private List<SeriesReview> reviews = new ArrayList<SeriesReview>();