【Java开源代码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。网学会员整理了Java开源代码-BagMappingTest.java的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
/* $Id$
*
* Copyright (c) DAKOSY AG and Riege Software. All rights reserved.
* Use is subject to license terms.
*/
package com.h3book.penguin.examples.dao.c8.bag;
import static com.h3book.penguin.examples.util.HibernateServiceProvider.getSession;
import static com.h3book.penguin.examples.util.HibernateServiceProvider.initHibernate;
import static org.junit.Assert.assertEquals;
import java.util.Collection;
import java.util.Date;
import junit.framework.JUnit4TestAdapter;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.After;
import org.junit.BeforeClass;
import org.junit.Test;
import com.h3book.penguin.examples.dao.InitTest;
import com.h3book.penguin.examples.domain.c8.bag.*;
/**
* Ge: Add docs
*
* @author <a href="mailto:
BagMappingTest.java为[网学网-网友上传,谢谢支持]。">Jing Ge</a>
* @version $Revision$
*/
public class BagMappingTest extends InitTest {
/**
* needed when running this test on the Junit 3.8 or earlier version.
*/
public static junit.framework.Test suite() {
return new JUnit4TestAdapter(BagMappingTest.class);
}
@BeforeClass
public static void setUp() {
initHibernate(Book.class, Author.class);
}
@Override
@After
public void emptyBooks() {
Session session = getSession();
Transaction t = session.beginTransaction();
for (Book book : (Collection<Book>) session.createCriteria(Book.class).list()) {
session.delete(book);
}
t.commit();
}
@Test
public void duplicateAuthors() {
Book book1 = createC8Book("Thinking in Matrix",
"one of the best Matrix book", getDateByString("20032006"));
Book book2 = createC8Book("Hibernate3", "new book about Hibernate 3",
getDateByString("06062007"));
Author author1 = createC8Author("Jing", "Ge",
getDateByString("01011975"), true);
Author author2 = createC8Author("Michael", "Peter",
getDateByString("01011970"), true);
Author author3 = createC8Author("Anderson", "Neo",
getDateByString("01011966"), true);
book1.addAuthor(author3);
author3.addBook(book1);
book1.addAuthor(author2);
book1.addAuthor(author2);
author2.addBook(book1);
book2.addAuthor(author1);
author1.addBook(book2);
Transaction t = null;
try {
Session session = getSession();
t = session.beginTransaction();
session.saveOrUpdate(book1);
session.saveOrUpdate(book2);
t.commit();
} catch (HibernateException e) {
if (t != null) {
t.rollback();
e.printStackTrace();
throw e;
}
}
try {
Session session = getSession();
t = session.beginTransaction();
Book book = (Book) session.load(Book.class, book1.getOid());
assertEquals(3, book.getAuthors().size());
t.commit();
} catch (HibernateException e) {
if (t != null) {
t.rollback();
e.printStackTrace();
throw e;
}
}
}
// help
private Book createC8Book(String name, String description, Date publish) {
Book book = new Book();
book.setName(name);
book.setDescription(description);
book.setPublish(publish);
return book;
}
private Author createC8Author(String firstName, String lastName,
Date birthday, boolean isMale) {
Author author = new Author();
author.setFirstName(firstName);
author.setLastName(lastName);
author.setBirthday(birthday);
author.setMale(isMale);
return author;
}
}
上一篇:
Bag.java
下一篇:
多年来只想说一句,我不怪你