【发布时间】:2012-01-01 12:16:44
【问题描述】:
我正在做一些 Spring + Hibernate 练习。作为示例项目,我选择了一个博客。我有两个模型;博客对象和标签对象。他们有一对一的关系。当我在服务器上发布和运行时,我可以在数据库中看到博客项目,但休眠时不插入标签项目。我无法弄清楚我在这里缺少什么:
博客模型中的标签:
@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;
}
hibernate.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 逻辑,构造函数可以有一个标签列表,但是我不需要一个注释,如果我想使用这个注释,我想了解我缺少什么。
编辑: 我的临时解决方案是我已更改为 manytomany。在我创建了第三个表之后,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);
【问题讨论】:
标签: hibernate spring many-to-many one-to-many