论坛首页 入门技术论坛

hibernate3.2 @OneToMany异常

浏览 3440 次
该帖已经被评为新手帖
作者 正文
   发表时间:2008-04-16  
TbComment 类和TbMyDiary 类之间是多对一的关系.
package com.hj.vo;

// default package

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Entity;
import org.hibernate.annotations.Generated;
import org.jaxen.function.FalseFunction;

/**
 * TbComment generated by MyEclipse - Hibernate Tools
 */
@Entity
@Table(name="tbComment") //如不写默认类名
public class TbComment implements java.io.Serializable {

	// Fields 所有没有定义注解的属性等价于在其上面添加了@Basic注解
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 8808366677482487429L;

	private Integer mainid;

	private String content;

	private Date addTime;

	private Integer type;

	private TbMyDiary myDiary;

	/** default constructor */
	public TbComment() {
	}

	/** minimal constructor */
	public TbComment(Integer mainid) {
		this.mainid = mainid;
	}

	/** full constructor */
	public TbComment(Integer mainid, String content, Date addTime, Integer type) {
		this.mainid = mainid;
		this.content = content;
		this.addTime = addTime;
		this.type = type;
		// this.targetId = targetId;
	}

	// Property accessors
	@Id 	@GeneratedValue
	public Integer getMainid() {
		return this.mainid;
	}

	public void setMainid(Integer mainid) {
		this.mainid = mainid;
	}

	public String getContent() {
		return this.content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	@Column(name="add_time",updatable=false,insertable=false)
	public Date getAddTime() {
		return this.addTime;
	}

	public void setAddTime(Date addTime) {
		this.addTime = addTime;
	}

	public Integer getType() {
		return this.type;
	}

	public void setType(Integer type) {
		this.type = type;
	}
	
	@ManyToOne
	@JoinColumn(name="target_id")
	public TbMyDiary getMyDiary() {
		return myDiary;
	}

	public void setMyDiary(TbMyDiary myDiary) {
		this.myDiary = myDiary;
	}

}

package com.hj.vo;

// Generated Mar 1, 2008 12:07:56 AM by Hibernate Tools 3.2.0.CR1

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;

/**
 * TbMyDiary generated by hbm2java
 */
@Entity
public class TbMyDiary implements java.io.Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5391171835504714756L;
	private int mainid;
	private String loginName;
	private String title;
	private String weather;
	private String mood;
	private String content;
	private Date diarytime;
	private Integer openLevel;
	private Integer viewNum;
	private Integer status;
	
	private Set<TbComment> tbComments=new HashSet(0);
	
	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public TbMyDiary() {
	}

	public TbMyDiary(int mainid) {
		this.mainid = mainid;

	}

	public TbMyDiary(int mainid, int FUserid, String loginName, String title,
			String weather, String mood, String content, Date diarytime,
			Integer openLevel) {
		this.mainid = mainid;
		this.loginName = loginName;
		this.title = title;
		this.weather = weather;
		this.mood = mood;
		this.content = content;
		this.diarytime = diarytime;
		this.openLevel = openLevel;
	}

	@Id @GeneratedValue
	public int getMainid() {
		return this.mainid;
	}

	public void setMainid(int mainid) {
		this.mainid = mainid;
	}

	public String getLoginName() {
		return this.loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getTitle() {
		return this.title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getWeather() {
		return this.weather;
	}

	public void setWeather(String weather) {
		this.weather = weather;
	}

	public String getMood() {
		return this.mood;
	}

	public void setMood(String mood) {
		this.mood = mood;
	}

	public String getContent() {
		return this.content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Date getDiarytime() {
		return this.diarytime;
	}

	public void setDiarytime(Date diarytime) {
		this.diarytime = diarytime;
	}

	public Integer getOpenLevel() {
		return this.openLevel;
	}

	public void setOpenLevel(Integer openLevel) {
		this.openLevel = openLevel;
	}

	public Integer getViewNum() {
		return viewNum;
	}

	public void setViewNum(Integer viewNum) {
		this.viewNum = viewNum;
	}

	@OneToMany(mappedBy="myDiary")
	public Set<TbComment> getTbComments() {
		return tbComments;
	}

	public void setTbComments(Set<TbComment> tbComments) {
		this.tbComments = tbComments;
	}

}

在执行Session session=HibernateSessionFactory.getSession();时报如下异常.
%%%% Error Creating SessionFactory %%%%
org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.hj.vo.TbMyDiary.tbComments[com.hj.vo.TbComment]
at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1068)
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:600)
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:541)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:43)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1136)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:324)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1292)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:859)
at com.hj.HibernateSessionFactory.rebuildSessionFactory(HibernateSessionFactory.java:63)
at com.hj.HibernateSessionFactory.getSession(HibernateSessionFactory.java:45)
at com.hj.dao.CommentDAO.insert(CommentDAO.java:21)
at com.hj.dao.CommentDAO.main(CommentDAO.java:14)
   发表时间:2008-04-16  
检查大小写错误
0 请登录后投票
   发表时间:2008-04-16  
@OneToMany(mappedBy="myDiary")   里的mappedBy的值不是TbComment类里的TbMyDiary对象名嘛?
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics