hibernate一对多、多对一关系(注解)
写了一个新闻模型,一个新闻类型模型,模型如下:新闻模型:@Id@AccessType(value="property")@GeneratedValue(strategy=...
写了一个新闻模型,一个新闻类型模型,模型如下:
新闻模型:
@Id
@AccessType(value = "property")
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@AccessType(value="property")
@Column(name="title")
private String title;
@AccessType(value="property")
@Column(name="content")
private String content;
@AccessType(value="property")
@Column(name="write_date")
private Date write_date;
@ManyToOne
@JoinColumn(name="news_type", nullable=true, insertable = true, updatable = true)
private NewsType newsType;
新闻类型模型:
@Id
@AccessType(value = "property")
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@AccessType(value="property")
@Column(name="type_name")
private String typeName;
@OneToMany(mappedBy = "news_type", cascade = CascadeType.ALL,fetch = FetchType.LAZY)
private List<NewsInfo> nif;
报错如下:
Error creating bean with name 'menuDAO' defined in file [E:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\fykj\WEB-INF\classes\base\system\dao\MenuDAO.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.hibernate.SessionFactory]: : Error creating bean with name 'sessionFactory' defined in file [E:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\fykj\WEB-INF\classes\applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: news.model.NewsInfo.news_type in news.model.NewsType.nif; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in file [E:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\fykj\WEB-INF\classes\applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: news.model.NewsInfo.news_type in news.model.NewsType.nif
如果我去掉新闻类型这边的一对多关系,就不会报错,大家帮我看看,估计是哪里的问题?
居然没有人回答~自己摸索了一下,让我来告诉大家,
@OneToMany(mappedBy = "news_type", cascade = CascadeType.ALL,fetch = FetchType.LAZY) 这里的mappedBy 要对应
@ManyToOne
@JoinColumn(name="news_type", nullable=true, insertable = true, updatable = true)
private NewsType newsType;的NewsType对象名,也就是newsType而不是字段名news_type。
就说这么多,希望大家能看懂,祝大家学习进步,工作顺利! 展开
新闻模型:
@Id
@AccessType(value = "property")
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@AccessType(value="property")
@Column(name="title")
private String title;
@AccessType(value="property")
@Column(name="content")
private String content;
@AccessType(value="property")
@Column(name="write_date")
private Date write_date;
@ManyToOne
@JoinColumn(name="news_type", nullable=true, insertable = true, updatable = true)
private NewsType newsType;
新闻类型模型:
@Id
@AccessType(value = "property")
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@AccessType(value="property")
@Column(name="type_name")
private String typeName;
@OneToMany(mappedBy = "news_type", cascade = CascadeType.ALL,fetch = FetchType.LAZY)
private List<NewsInfo> nif;
报错如下:
Error creating bean with name 'menuDAO' defined in file [E:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\fykj\WEB-INF\classes\base\system\dao\MenuDAO.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.hibernate.SessionFactory]: : Error creating bean with name 'sessionFactory' defined in file [E:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\fykj\WEB-INF\classes\applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: news.model.NewsInfo.news_type in news.model.NewsType.nif; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in file [E:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\fykj\WEB-INF\classes\applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: news.model.NewsInfo.news_type in news.model.NewsType.nif
如果我去掉新闻类型这边的一对多关系,就不会报错,大家帮我看看,估计是哪里的问题?
居然没有人回答~自己摸索了一下,让我来告诉大家,
@OneToMany(mappedBy = "news_type", cascade = CascadeType.ALL,fetch = FetchType.LAZY) 这里的mappedBy 要对应
@ManyToOne
@JoinColumn(name="news_type", nullable=true, insertable = true, updatable = true)
private NewsType newsType;的NewsType对象名,也就是newsType而不是字段名news_type。
就说这么多,希望大家能看懂,祝大家学习进步,工作顺利! 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询