我在做一些Spring + Hibernate的練習。作爲一個示例項目,我選擇了一個博客。我有兩個模型;博客對象和標籤對象。他們有私人關係。當我發佈並在服務器上運行時,我可以看到數據庫上的博客項目,但休眠不會插入標籤項目。我不能找出我是缺少在這裏:Spring Hibernate onetomany relationship
標籤在博客模式:
@OneToMany(targetEntity=Tags.class, mappedBy="blog", fetch=FetchType.EAGER)
@Column(name="blog_tags")
public List<Tags> getBlogTags() {
return blogTags;
}
public void setBlogTags(List<Tags> blogTags) {
this.blogTags = blogTags;
}
博客在標籤型號:
@ManyToOne(targetEntity=Blog.class,fetch=FetchType.EAGER)
@JoinColumn(name="blog_id")
public Blog getBlog() {
return blog;
}
public void setBlog(Blog blog) {
this.blog = blog;
}
休眠。 hbm.xml:
<!-- Hibernate session factory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>com.udb.blog.model.Blog</value>
<value>com.udb.blog.model.Tags</value>
</list>
</property>
</bean>
</beans>
blogBo(使用相同的實現作爲blogDao):
public interface BlogBo {
void save(Blog blog);
void update(Blog blog);
void delete(Blog blog);
Blog getByTitle(String str);
}
測試的Servlet:
ApplicationContext appContext = new ClassPathXmlApplicationContext("spring/config/BeanLocations.xml");
BlogBo blogBo = (BlogBo) appContext.getBean("blogBo");
Blog blog = new Blog();
blog.setBlogTitle("My Second blog");
blog.setBlogDescription("testing onetomany relationship");
blog.setBlogBody("testing done!");
blog.setBlogDate(new Date(0));
Tags tag = new Tags();
tag.setTagName("first blog");
Tags tag2 = new Tags();
tag2.setTagName("Spring");
tag.setBlog(blog);
tag2.setBlog(blog);
blogBo.save(blog);
Blog blog2 = blogBo.getByTitle("My Second blog");
response.getWriter().write(blog2.toString());
冬眠輸出:
Hibernate: insert into burak1_udb.blog (blog_body, blog_date, blog_desc, blog_title) values (?, ?, ?, ?)
Hibernate: select blog0_.blog_id as blog1_0_, blog0_.blog_body as blog2_0_, blog0_.blog_date as blog3_0_, blog0_.blog_desc as blog4_0_, blog0_.blog_title as blog5_0_ from burak1_udb.blog blog0_ where blog_title=?
Hibernate: select blogtags0_.blog_id as blog3_1_, blogtags0_.tag_id as tag1_1_, blogtags0_.tag_id as tag1_1_0_, blogtags0_.blog_id as blog3_1_0_, blogtags0_.tag_name as tag2_1_0_ from burak1_udb.tags blogtags0_ where blogtags0_.blog_id=?
Hibernate: select blogtags0_.blog_id as blog3_1_, blogtags0_.tag_id as tag1_1_, blogtags0_.tag_id as tag1_1_0_, blogtags0_.blog_id as blog3_1_0_, blogtags0_.tag_name as tag2_1_0_ from burak1_udb.tags blogtags0_ where blogtags0_.blog_id=?
我想我需要改變blogBo的邏輯,構造函數可以有一個標籤列表,但我不需要onetomany註釋,我想了解我缺少的東西,如果我想使用這個註釋。
編輯: 我的臨時解決方案是我已換成許多東西。之後,我創建了第三個表,blogtags:
**@Entity
@Table(name="blog_tags")
public class BlogTags implements Serializable{
/**
*
*/
public BlogTags(Blog blog, Tags tag){
this.blogId = blog.getBlogId();
this.tagId = tag.getTagId();
}**
然後,我已經改變blogDao如下:
public void save(Blog blog, List<Tags> tags) {
getHibernateTemplate().save(blog);
for(Tags tag:tags){
getHibernateTemplate().saveOrUpdate(tag);
BlogTags blogtags = new BlogTags(blog, tag);
getHibernateTemplate().saveOrUpdate(blogtags);
}
和最終的servlet:
Tags tag = new Tags();
tag.setTagName("myTag");
Tags tag2 = new Tags();
tag2.setTagName("Spring");
Tags tag3 = new Tags();
tag3.setTagName("Hibernate");
Tags tag4 = new Tags();
tag4.setTagName("ManytoMany");
List<Tags> tags = new ArrayList<Tags>();
tags.add(tag);
tags.add(tag2);
tags.add(tag3);
tags.add(tag4);
Blog blog = new Blog();
blog.setBlogTitle("myTest");
blog.setBlogDescription("testDesc");
blog.setBlogBody("body");
blog.setBlogDate(new Date(0));
blogBo.save(blog, tags);
謝謝你的解決方案! – HRgiger 2012-01-03 00:03:39