Hibernate 关于注解的错误,请教下
[code=Java][/code]ERROR [ContainerBackgroundProcessor[StandardEngine[Catalina]]] ContextLoader.initWebApplicationContext(220) | Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0' defined in class path resource [spring/applicationContext-dao.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in file [F:\webserver\apache-tomcat-6.0.32\webapps\blognms\WEB-INF\classes\spring\applicationContext-dao.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException:
问题重点:
Foreign key (FKAECBABBE3114B171:ibokee_report_comment [post])) must have same number of columns as the referenced primary key (ibokee_wb_postcomment [postID,postComments_id])
- Java code
@Entity@Table(name = "ibokee_report_comment")@Searchable@XmlRootElementpublic class ReportComment implements Serializable { private static final long serialVersionUID = 1L; private Long id; /** 日志评论 */ private ArticleComment article; /** 照片评论 */ private PhotoComment photo; /** 微博评论 */ private PostComment post; @Id @GeneratedValue(strategy = GenerationType.AUTO) @SearchableId public Long getId() { return id; } public void setId(Long id) { this.id = id; } @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "article") public ArticleComment getArticle() { return article; } public void setArticle(ArticleComment article) { this.article = article; } @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "photo") public PhotoComment getPhoto() { return photo; } public void setPhoto(PhotoComment photo) { this.photo = photo; } @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "post") public PostComment getPost() { return post; } public void setPost(PostComment post) { this.post = post; }}- Java code
@Entity@Table(name = "ibokee_wb_postcomment")@Searchable@XmlRootElementpublic class PostComment implements Serializable { private static final long serialVersionUID = 1L; private Integer id; private Post post; private User user; private String content; private Date createTime; private Boolean del = Boolean.FALSE; /** 评论回复 */ private PostComment parent; public PostComment() { } @Id @GeneratedValue(strategy = GenerationType.AUTO) @SearchableId public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "post") public Post getPost() { return post; } public void setPost(Post post) { this.post = post; } @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "user") public User getUser() { return user; } public void setUser(User user) { this.user = user; } @Column public String getContent() { return content; } public void setContent(String content) { this.content = content; } @Column public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } @Column public Boolean getDel() { return del; } public void setDel(Boolean del) { this.del = del; } @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "parent") public PostComment getParent() { return parent; } public void setParent(PostComment parent) { this.parent = parent; }}
[解决办法]
applicationContext-dao.xml
里sessionfactory配置错了
你打红字的不是问题关键,因为sessionfactory配置里的初始化方法错了,才导致你那红字的错误。
好好看下 applicationContext-dao.xml 怎么配置的。
[解决办法]
没看出来哪里错了。。帮顶。
[解决办法]
楼主,你这代码的错误提示是加载完毕那个xml文件的时候就这样的错误,原因出在你的外键上,一定是你的数据不对,请楼主仔细查找一下数据!!
[解决办法]
帮顶 请高手指教
[解决办法]
是不是在主表里没有关联的数据啊
[解决办法]
ManyToOne()这里有问题,
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "post")
改改
@JoinColumn(name = "post" class="类名" column="外键ID")
不过LZ好象manyToOne和onetomany有点混淆,
[解决办法]
刘诗诗姑娘说的很有道理~