hibernate.CatetoryAndArticleTest.java Source code

Java tutorial

Introduction

Here is the source code for hibernate.CatetoryAndArticleTest.java

Source

/*
 * Hibernate, Relational Persistence for Idiomatic Java
 *
 * Copyright (c) 2010, Red Hat Inc. or third-party contributors as
 * indicated by the @author tags or express copyright attribution
 * statements applied by the authors.  All third-party contributions are
 * distributed under license by Red Hat Inc.
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */
package hibernate;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.junit.Test;

import com.lichhao.blog.model.Article;
import com.lichhao.blog.model.Category;

/**
 * Illustrates the use of Hibernate native APIs. The code here is unchanged from
 * the {@code basic} example, the only difference being the use of annotations
 * to supply the metadata instead of Hibernate mapping files.
 * 
 * @author Steve Ebersole
 */
public class CatetoryAndArticleTest extends BaseHibernateConfig {

    @Test
    public void test() throws Exception {
        // SessionFactory
        createAnnotataionSessionFactory();
        testCategoryAndArticle();
        closeSessionFactory();

        // XmlSessionFactory
        createXmlSessionFactory();
        testCategoryAndArticle();
        closeSessionFactory();
    }

    public void testCategoryAndArticle() {
        Session session = sessionFactory.openSession();
        session.beginTransaction();

        Category category = new Category();
        category.setCatName("");
        category.setCreateDate(new Date());
        session.save(category);// ?sql insert???
        assertNotNull("??Category???", category.getCatId());

        // session.persist(category);// ?sql insert??flush??

        Article article = new Article();
        // ?
        // hibernate???
        article.setCategory(category);

        article.setTitle("");
        article.setContent("");
        article.setCreateDate(new Date());
        article.setModifyDate(new Date());
        article.setSummary("?");
        article.setVisitCount(0);
        article.setIsPublished(Boolean.TRUE);
        article.setType("post");
        article.setCommentStatus("open");
        article.setArticleStatus("public");
        article.setLink("http://localhost:8080/newblog/post/abce");
        session.save(article);
        assertNotNull("??Article???", article.getArticleId());
        // ?
        // category.getArticles().add(article);
        // session.update(category);

        Query createQuery = session.createQuery("from Category c where c.catId = :catId");
        createQuery.setParameter("catId", category.getCatId());

        Category cat = (Category) createQuery.uniqueResult();
        // hibernate???
        session.refresh(cat);

        assertEquals(cat.getCatId(), category.getCatId());
        assertEquals(cat.getCatName(), category.getCatName());
        assertEquals(cat.getCreateDate(), category.getCreateDate());

        logger.debug("" + cat.getCatId());
        logger.debug("??" + cat.getCatName());
        logger.debug("");
        List<Article> articles = cat.getArticles();
        for (Article bean : articles) {
            logger.debug("------------");
            logger.debug("" + bean.getArticleId());
            logger.debug("" + bean.getTitle());
            logger.debug("" + bean.getContent());
            logger.debug("------------");
        }

        for (Article bean : cat.getArticles()) {
            session.delete(bean); // ??
        }

        session.delete(cat);
        session.getTransaction().commit();
        session.close();
    }
}