简体   繁体   English

Spring Hibernate一对一关系

[英]Spring Hibernate onetomany relationship

I am doing some Spring + Hibernate exercise. 我正在做一些春季+冬眠运动。 As an example project I have chosen a Blog. 作为一个示例项目,我选择了一个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 : Blog模型中的标签

@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.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): blogBo(使用与blogDao相同的实现):

public interface BlogBo {

    void save(Blog blog);
    void update(Blog blog);
    void delete(Blog blog);
    Blog getByTitle(String str);

}

test Servlet: 测试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. 我认为我需要更改blogBo逻辑,以使构造函数可以具有标签列表,但是这样我就不需要一个全注解,并且我想了解如果我想使用此注解,我会缺少什么。

EDIT: My temporary solution is I have changeg to manytomany. 编辑:我的临时解决方案是我已更改为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: 然后我将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);

    }

And final servlet: 最后的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);

Try to manually add Tag to your Blog, so that instead of: 尝试将标记手动添加到您的Blog中,以代替:

tag.setBlog(blog);
tag2.setBlog(blog);

do: 做:

blog.getBlogTags().add(tag);
blog.getBlogTags().add(tag2);

On @OneToMany relation side ie on Blog entity side set cascade style to CascadeType.SAVE_UPDATE to get the save or update operation get cascade to it's associated child entities. @OneToMany关系方面,即在Blog实体方面,将级联样式设置为CascadeType.SAVE_UPDATE以获取保存或更新操作,以级联其关联的子实体。 Otherwise you have to save them manually. 否则,您必须手动保存它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM