I am doing some Spring + Hibernate exercise. As an example project I have chosen a Blog. I have two model; Blog object and Tags Object. They have onetomany relationship. When I publish and run on server I can see blog items on database but hibernate not inserting tag items. I couldnt figure out what I am missing here:

tags in Blog model:
@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; } blog in Tags model:
@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 (using same implementation as blogDao):
public interface BlogBo { void save(Blog blog); void update(Blog blog); void delete(Blog blog); Blog getByTitle(String str); } test 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 output:
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=? I think I need to change blogBo logic that constructor can have a list of tags but then I will not need onetomany annotation andI want to understand what I is missing if I want to use this annotation.
EDIT: My temporary solution is I have changeg to manytomany. After I have created a third table, 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(); }** Then I have changed blogDao as below:
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); } And final 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);