我有2个表:菜单和课程。我映射到2
package ir.parhoontoos.hibernate;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="course")
public class Course implements Serializable{
public Course() {
}
public Course(String title , String description , byte[] image , Menu menu){
this.title = title;
this.description = description;
this.image = image;
this.menu = menu;
}
private Integer course_id;
private String title;
private String description;
private byte[] image;
private Menu menu;
@Id @GeneratedValue
@Column(name="course_id" , unique = true, nullable = false)
public Integer getCourse_id() {
return course_id;
}
public void setCourse_id(Integer course_id) {
this.course_id = course_id;
}
@Column(name="title")
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Column(name="description")
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Column(name="image")
public byte[] getImage() {
return image;
}
public void setImage(byte[] image) {
this.image = image;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "menu_id", nullable = false , insertable=false , updatable=false)
public Menu getMenu() {
return menu;
}
public void setMenu(Menu menu) {
this.menu = menu;
}
}
和
package ir.parhoontoos.hibernate;
import java.io.Serializable;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="menu")
public class Menu implements Serializable{
public Menu() {
}
public Menu(String title) {
this.title = title;
}
@Id @GeneratedValue
@Column(name="menu_id" , unique = true, nullable = false)
private int id;
@Column(name="title")
private String title;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "menu")
private Set<Course> posts;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Set<Course> getPosts() {
return posts;
}
public void setPosts(Set<Course> posts) {
this.posts = posts;
}
}
表:
CREATE TABLE `course` (
`course_id` int(11) NOT NULL AUTO_INCREMENT,
`title` text NOT NULL,
`description` text NOT NULL,
`image` binary(1) NOT NULL,
`menu_id` int(11) NOT NULL,
PRIMARY KEY (`id`,`menu_id`),
KEY `fk_course_menu_idx` (`menu_id`),
CONSTRAINT `fk_course_menu` FOREIGN KEY (`menu_id`) REFERENCES `menu` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
)
和
CREATE TABLE `menu` (
`menu_id` int(11) NOT NULL AUTO_INCREMENT,
`title` text,
PRIMARY KEY (`id`)
)
当尝试创建会话对象时,我有这个例外:
org.hibernate.MappingException:实体映射中的重复列: ir.parhoontoos.hibernate.Course列:id(应该映射为 插入=&#34;假&#34;更新=&#34;假&#34)