Hibernate 表互相关联,注解实现以及两表的更新

本文介绍了使用ORM技术进行数据库表映射的方法,并展示了如何通过Java实体类Article和Category实现文章及栏目的更新操作。涉及实体类定义、注解使用及具体业务逻辑处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

与article bean对应的表tbl_article
与article bean对应的表tbl_article
与category bean对应的表tbl_category
这里写图片描述

Article bean

/**
 * 信息类
 */
@Entity
@Table(name="tbl_article")
public class Article {
    private Long id;

    //标题
    private String title;
    //内容
    private String content;
    //作者
    private String author;
    //发布日期
    private Date publishDate;
    //点击次数
    private Integer clickTimes = 0;
    //所属栏目 文章和栏目之间关系为多对一关系
    private Category category;



    public Article() {
        super();
    }
    public Article(String title, String content, String author, Date publishDate,Category category) {
        super();
        this.title = title;
        this.content = content;
        this.author = author;
        this.publishDate = publishDate;
        this.category=category;
    }



    public Article(Long id, String title, String content, String author, Date publishDate, Integer clickTimes,
            Category category) {
        super();
        this.id = id;
        this.title = title;
        this.content = content;
        this.author = author;
        this.publishDate = publishDate;
        this.clickTimes = clickTimes;
        this.category = category;
    }
    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "id", unique = true)
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }

    /*@Transient
    public String getDateStr(){
        if(publishDate==null){
            return null;
        }
        return new SimpleDateFormat("yyyy-MM-dd").format(publishDate);
    }*/

    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public Date getPublishDate() {
        return publishDate;
    }
    public void setPublishDate(Date publishDate) {
        this.publishDate = publishDate;
    }
    public Integer getClickTimes() {
        return clickTimes;
    }
    public void setClickTimes(Integer clickTimes) {
        this.clickTimes = clickTimes;
    }
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name="c_id")
    public Category getCategory() {
        return category;
    }
    public void setCategory(Category category) {
        this.category = category;
    }
    @Override
    public String toString() {
        return "Article [id=" + id + ", title=" + title + ", content=" + content + ", author=" + author
                + ", publishDate=" + publishDate + ", clickTimes=" + clickTimes + ", category=" + category + "]";
    }
}

category bean

/**
 * 栏目类
 */
@Entity
@Table(name="tbl_category")
public class Category {
    private Long id;    
    //栏目名称: 财经
    private String name;    
    //栏目码
    private Integer code;   
    //一对多关系,一个栏目下有多个文章
    private Set<Article> articles;

     public Category(Long id, String name, Integer code) {
        super();
        this.id = id;
        this.name = name;
        this.code = code;
    }

    public Category(Long id, String name, Integer code, Set<Article> articles) {
        super();
        this.id = id;
        this.name = name;
        this.code = code;
        this.articles = articles;
    }

    public Category(String name, Integer code) {
        super();
        this.name = name;
        this.code = code;
    }

    public Category() {

    }

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "id", unique = true)
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getCode() {
        return code;
    }
    public void setCode(Integer code) {
        this.code = code;
    }

    @OneToMany(cascade=CascadeType.REMOVE)
    @JoinColumn(name="c_id")
    public Set<Article> getArticles() {
        return articles;
    }
    public void setArticles(Set<Article> articles) {
        this.articles = articles;
    }

    @Override
    public String toString() {
        return "Category [id=" + id + ", name=" + name + ", code=" + code + "]";
    }




}

更新文章

@Action("updateArticleInfo")
    public void updateArticleInfo() {
        String json=null;
            try {
                category=new Category();
                category.setId(c_id);
                article=new Article(id, title, content, author, publishDate, clickTimes, category);
                articleService.updateArticle(article);
                json="{\"msg\":\"OK\"}";
            } catch (ServiceException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                json=null;
            }
            WebUtil.sendResponse(json);
    }

更新栏目

@Action("updateCategory")
    public void updateCategory(){
        String json=null;
        try {
            category=categoryService.findById(id);
            Set<Article>    articles=(Set<Article>) category.getArticles();
            category.setName(name);
            category.setCode(code);
            category.setArticles(articles);
            categoryService.updateCategory(category);
            String[] str={"articles"};
            json="{\"msg\":\"OK\"}";

        } catch (ServiceException e) {
            e.printStackTrace();
            json=null;
        }
        WebUtil.sendResponse(json);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值