main sources of the myLibraryServices project :
src/main/resources :database.propertiesCode:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://xxx.com:3306/databasename
jdbc.username=xxx
jdbc.password=xxx
DataSource.xmlCode:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:database.properties</value>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
</beans>
Hibernate.xmlCode:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 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.MySQLInnoDBDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.connection.autocommit">false</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>com.cambyze.my_library.services.model.Book</value>
<value>com.cambyze.my_library.services.model.Author</value>
</list>
</property>
</bean>
</beans>
BeanLocations.xmlCode:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- Database Configuration -->
<import resource="../database/DataSource.xml" />
<import resource="../database/Hibernate.xml" />
<!-- Auto scan the components -->
<context:component-scan base-package="com.cambyze.my_library.services" />
</beans>
src/main/java :CustomHibernateDaoSupport.javaCode:
package com.cambyze.my_library.util;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public abstract class CustomHibernateDaoSupport extends HibernateDaoSupport {
@Autowired
public void anyMethodName(SessionFactory sessionFactory) {
setSessionFactory(sessionFactory);
}
}
Book.javaCode:
package com.cambyze.my_library.services.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* @SuppressWarnings("serial")
* @author admin
*
*/
@SuppressWarnings("serial")
@Entity
@Table(name = "book")
public class Book implements java.io.Serializable {
/**
* Constant to identify invalid book.
*/
public static final String INVALID_ISBN = "-1";
/**
* unique DB id.
*/
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
/**
* isbn.
*/
@Column(name = "isbn")
private String isbn;
/**
* year.
*/
@Column(name = "year")
private Integer year;
/**
* language.
*/
@Column(name = "lang")
private String language;
/**
* edition.
*/
@Column(name = "edition")
private String edition;
/**
* title.
*/
@Column(name = "title")
private String title;
/**
* publisher.
*/
@Column(name = "publisher")
private String publisher;
/**
* city.
*/
@Column(name = "city")
private String city;
/**
* url of OLCL.
*/
@Column(name = "url")
private String url;
/**
* authors as a composite string.
*/
@Column(name = "authors_desc")
private String authorsDesc;
public Book() {
super();
}
public final Long getId() {
return id;
}
public final void setId(final Long id) {
this.id = id;
}
public final String getIsbn() {
return isbn;
}
public final void setIsbn(final String isbn) {
this.isbn = isbn;
}
public final Integer getYear() {
return year;
}
public final void setYear(final Integer year) {
this.year = year;
}
public final String getLanguage() {
return language;
}
public final void setLanguage(final String language) {
this.language = language;
}
public final String getEdition() {
return edition;
}
public final void setEdition(final String edition) {
this.edition = edition;
}
public final String getTitle() {
return title;
}
public final void setTitle(final String title) {
this.title = title;
}
public final String getPublisher() {
return publisher;
}
public final void setPublisher(final String publisher) {
this.publisher = publisher;
}
public final String getCity() {
return city;
}
public final void setCity(final String city) {
this.city = city;
}
public final String getUrl() {
return url;
}
public final void setUrl(final String url) {
this.url = url;
}
public final String getAuthorsDesc() {
return authorsDesc;
}
public final void setAuthorsDesc(final String authorsDesc) {
this.authorsDesc = authorsDesc;
}
@Override
public final String toString() {
String result = "Book [";
if (this.id != null) {
result = result + "dbid=" + Long.toString(id) + " ";
}
if (this.isbn != null) {
result = result + "isbn=" + isbn + " ";
}
if (this.title != null) {
result = result + "title=" + title + " ";
}
if (this.authorsDesc != null) {
result = result + "authors=" + authorsDesc + " ";
}
result = result + "]";
return result;
}
}
Author.javaCode:
package com.cambyze.my_library.services.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* @SuppressWarnings("serial")
* @author admin
*
*/
@SuppressWarnings("serial")
@Entity
@Table(name = "author")
public class Author implements java.io.Serializable {
/**
* Constant to identify invalid author.
*/
public static final String INVALID_AUTHOR = "-1";
/**
* unique DB id.
*/
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
/**
* name of the author.
*/
@Column(name = "name")
private String name;
public Author() {
super();
}
public final Long getId() {
return id;
}
public final void setId(final Long id) {
this.id = id;
}
public final String getName() {
return name;
}
public final void setName(final String name) {
this.name = name;
}
@Override
public final String toString() {
String result = "Author [";
if (this.id != null) {
result = result + "dbid=" + Long.toString(id) + " ";
}
if (this.name != null) {
result = result + "name=" + name + " ";
}
result = result + "]";
return result;
}
}
AuthorDao.javaCode:
package com.cambyze.my_library.services.dao;
import java.util.List;
import com.cambyze.my_library.services.model.Author;
import com.cambyze.my_library.services.model.Book;
/**
* Data Access Object (DAO) for the author.
*
* @author admin
*
*/
public interface AuthorDao {
/**
* Save the author in DB.
*
* @param author
* author to store.
* @return id of the author.
*/
Long save(Author author);
/**
* Update the author in DB.
*
* @param author
* author to update (known by its id).
*/
void update(Author author);
/**
* @param author
* author to delete (known by its id).
*/
void delete(Author author);
/**
* Find book in the DB with its name.
*
* @param name
* name to search.
* @return author from DB.
*/
Author findByName(String name);
/**
* Find author in the DB with its id.
*
* @param id
* id of the author.
* @return author from DB.
*/
Author findById(Long id);
/**
* Find the books of an author in the DB.
*
* @param author
* author.
* @return list of books.
*/
List<Book> getBooks(Author author);
}
BookDao.javaCode:
package com.cambyze.my_library.services.dao;
import java.util.List;
import com.cambyze.my_library.services.model.Author;
import com.cambyze.my_library.services.model.Book;
/**
* Data Access Object (DAO) for the book.
*
* @author admin
*
*/
public interface BookDao {
/**
* Save the book in DB.
*
* @param book
* book to store.
* @return if of the book.
*/
Long save(Book book);
/**
* Update the book in DB.
*
* @param book
* book to update (known by its id).
*/
void update(Book book);
/**
* Add the list of authors of the book.
* @param book book.
* @param list list of authors.
*/
void addAuthors(Book book, List<Author> list);
/**
* @param book
* book to delete (known by its id).
*/
void delete(Book book);
/**
* Find book in the DB with its isbn.
*
* @param isbn
* isbn to search.
* @return book from DB.
*/
Book findByIsbn(String isbn);
/**
* Find book in the DB with its id.
*
* @param id
* id of the book.
* @return book from DB.
*/
Book findById(Long id);
}
AuthorDaoImpl.javaCode:
package com.cambyze.my_library.services.dao.impl;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.SQLQuery;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.cambyze.my_library.services.dao.AuthorDao;
import com.cambyze.my_library.services.dao.BookDao;
import com.cambyze.my_library.services.model.Author;
import com.cambyze.my_library.services.model.Book;
import com.cambyze.my_library.util.CustomHibernateDaoSupport;
/**
* Data Access Object (DAO) for the author.
*
* @author admin
*
*/
@Repository("authorDao")
@Transactional(readOnly = false, propagation = Propagation.MANDATORY, rollbackFor = Exception.class)
public class AuthorDaoImpl extends CustomHibernateDaoSupport implements AuthorDao {
/**
* Declaration of a logger.
*/
private static final Logger LOGGER = LoggerFactory.getLogger(AuthorDaoImpl.class);
/**
* Session factory.
*/
@Autowired
private transient SessionFactory sessionFactory;
/**
* DAO for books.
*/
@Autowired
private transient BookDao bookDao;
/**
* {@inheritDoc}
*
* @return
*/
public final Long save(final Author author) {
final Long authorId = (Long) getHibernateTemplate().save(author);
LOGGER.debug("author {} inserted with id {}", author.getName(), authorId);
return authorId;
}
/**
* {@inheritDoc}
*/
public void update(final Author author) {
getHibernateTemplate().update(author);
}
/**
* {@inheritDoc}
*/
public void delete(final Author author) {
getHibernateTemplate().delete(author);
}
/**
* {@inheritDoc}
*/
@SuppressWarnings("rawtypes")
public Author findByName(String name) {
Author author;
final List list = getHibernateTemplate().find("from Author where name='" + name + "'");
LOGGER.debug("findByName name:{} nb:{}", name, list.size());
if (list.isEmpty()) {
author = new Author();
author.setName(Author.INVALID_AUTHOR);
} else {
author = (Author) list.get(0);
}
LOGGER.debug("author sent : {}", author.toString());
return author;
}
/**
* {{@inheritDoc}
*/
@SuppressWarnings("rawtypes")
public Author findById(final Long id) {
Author author;
final List list = getHibernateTemplate().find("from Author where id=?", id);
LOGGER.debug("findByIsbn id:{} nb:{}", id, list.size());
if (list.isEmpty()) {
author = new Author();
author.setName(Author.INVALID_AUTHOR);
} else {
author = (Author) list.get(0);
}
return author;
}
/**
* {{@inheritDoc}
*/
@SuppressWarnings({ "unchecked", "deprecation" })
public List<Book> getBooks(Author author) {
List<Book> list = new ArrayList<Book>();
String query = "select book_id from authorBooks where author_id=" + Long.toString(author.getId());
LOGGER.debug("select query : {}", query);
SQLQuery sqlQuery = sessionFactory.getCurrentSession().createSQLQuery(query).addScalar("book_id", Hibernate.LONG);
List<Long> rows = sqlQuery.list();
LOGGER.debug("result query : {}", rows.size());
for (Long row : rows) {
LOGGER.debug("book to find : {}",row);
Book book = bookDao.findById(row);
list.add(book);
}
LOGGER.debug("result list : {}", list.toString());
return list;
}
}
BookDaoImpl.javaCode:
package com.cambyze.my_library.services.dao.impl;
import java.util.List;
import org.hibernate.SQLQuery;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.cambyze.my_library.services.dao.BookDao;
import com.cambyze.my_library.services.model.Author;
import com.cambyze.my_library.services.model.Book;
import com.cambyze.my_library.util.CustomHibernateDaoSupport;
/**
* Data Access Object (DAO) for the book.
*
* @author admin
*
*/
@Repository("bookDao")
@Transactional(readOnly = false, propagation = Propagation.MANDATORY, rollbackFor = Exception.class)
public class BookDaoImpl extends CustomHibernateDaoSupport implements BookDao {
/**
* Session factory.
*/
@Autowired
private transient SessionFactory sessionFactory;
/**
* Declaration of a logger.
*/
private static final Logger LOGGER = LoggerFactory.getLogger(BookDaoImpl.class);
/**
* {@inheritDoc}
*/
public final Long save(final Book book) {
Long bookId = (Long) getHibernateTemplate().save(book);
LOGGER.debug("book {} inserted with id {}", book.getTitle(), bookId);
return bookId;
}
/**
* {@inheritDoc}
*/
public void update(final Book book) {
getHibernateTemplate().update(book);
}
/**
* {@inheritDoc}
*/
public void delete(final Book book) {
// delete links with authors.
String query = "delete from authorBooks where book_id=" + Long.toString(book.getId());
LOGGER.debug("delete query : {}", query);
SQLQuery sqlQuery = sessionFactory.getCurrentSession().createSQLQuery(query);
int deleted = sqlQuery.executeUpdate();
LOGGER.debug("delete authorBooks : {}", deleted);
// then delete the book.
getHibernateTemplate().delete(book);
}
/**
* {@inheritDoc}
*/
@SuppressWarnings("rawtypes")
public Book findByIsbn(final String isbn) {
Book book;
final List list = getHibernateTemplate().find("from Book where isbn='" + isbn + "'");
LOGGER.debug("findByIsbn isbn:{} nb:{}", isbn, list.size());
if (list.isEmpty()) {
book = new Book();
book.setIsbn(Book.INVALID_ISBN);
} else {
book = (Book) list.get(0);
}
LOGGER.debug("book sent : {}", book.toString());
return book;
}
/**
* {{@inheritDoc}
*/
@SuppressWarnings("rawtypes")
public Book findById(final Long id) {
Book book;
final List list = getHibernateTemplate().find("from Book where id=?", id);
LOGGER.debug("findById id:{} nb:{}", id, list.size());
if (list.isEmpty()) {
book = new Book();
book.setIsbn(Book.INVALID_ISBN);
} else {
book = (Book) list.get(0);
}
return book;
}
/**
* {{@inheritDoc}
*/
public void addAuthors(Book book, List<Author> list) {
LOGGER.debug("addAuthors for book : {} list of authors : {}", book.getId(), list.toString());
for (Author author : list) {
String query = "insert into authorBooks(author_id,book_id) values (" + Long.toString(author.getId()) + ","
+ Long.toString(book.getId()) + ")";
LOGGER.debug("insert query : {}", query);
SQLQuery sqlQuery = sessionFactory.getCurrentSession().createSQLQuery(query);
int inserted = sqlQuery.executeUpdate();
LOGGER.debug("inserted authorBooks : {}", inserted);
}
}
}
AuthorBo.javaCode:
package com.cambyze.my_library.services.bo;
import java.util.List;
import com.cambyze.my_library.services.model.Author;
import com.cambyze.my_library.services.model.Book;
/**
* Business Object (BO) for the authors.
*
* @author admin
*
*/
public interface AuthorBo {
/**
* Save the author in DB.
*
* @param author
* author to store.
* @return generated id of the author in DB.
*/
Long save(Author author);
/**
* Update the author in DB.
*
* @param author
* author to update (known by its id).
*/
void update(Author author);
/**
* Delete the author from the DB.
*
* @param author
* author to delete (known by its id).
*/
void delete(Author author);
/**
* Find author in the DB with its name.
*
* @param name
* name to search.
* @return author from DB.
*/
Author findByName(String name);
/**
* Find author in the DB with its id.
*
* @param id
* id of the author.
* @return author from DB.
*/
Author findById(Long id);
/**
* Find the books of an author in the DB.
*
* @param author
* author.
* @return list of books.
*/
List<Book> getBooks(Author author);
}
BookBo.javaCode:
package com.cambyze.my_library.services.bo;
import java.util.List;
import com.cambyze.my_library.services.model.Author;
import com.cambyze.my_library.services.model.Book;
/**
* Business Object (BO) for the books.
*
* @author admin
*
*/
public interface BookBo {
/**
* Save the book in DB.
*
* @param book
* book to store.
* @return id of the book.
*/
Long save(Book book);
/**
* save the book in DB with its authors.
*
* @param book
* book to store.
* @param listAuthor
* authors of the book.
* @return id of the book generated in DB.
*/
Long saveWithAuthors(Book book, List<Author> listAuthor);
/**
* Update the book in DB.
*
* @param book
* book to update (known by its id).
*/
void update(Book book);
/**
* Delete the book from the DB.
*
* @param book
* book to delete (known by its id).
*/
void delete(Book book);
/**
* Find book in the DB with its isbn.
*
* @param isbn
* isbn to search.
* @return book from DB.
*/
Book findByIsbn(String isbn);
/**
* Find book in the DB with its id.
*
* @param id
* id of the book.
* @return book from DB.
*/
Book findById(Long id);
}
AuthorBoImpl.javaCode:
package com.cambyze.my_library.services.bo.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cambyze.my_library.services.bo.AuthorBo;
import com.cambyze.my_library.services.dao.AuthorDao;
import com.cambyze.my_library.services.model.Author;
import com.cambyze.my_library.services.model.Book;
/**
* Business Object (BO) for the authors.
*
* @author admin
*
*/
@Service("authorBo")
public class AuthorBoImpl implements AuthorBo {
/**
* DAO used to manage book in the DB.
*/
@Autowired
private transient AuthorDao authorDao;
/**
* {@inheritDoc}
*/
public Long save(final Author author) {
return authorDao.save(author);
}
/**
* {@inheritDoc}
*/
public void update(final Author author) {
authorDao.update(author);
}
/**
* {@inheritDoc}
*/
public void delete(final Author author) {
authorDao.delete(author);
}
/**
* {@inheritDoc}
*/
public Author findByName(final String name) {
return authorDao.findByName(name);
}
/**
* {@inheritDoc}
*/
public Author findById(final Long id) {
return authorDao.findById(id);
}
/**
* {@inheritDoc}
*/
public List<Book> getBooks(Author author) {
return authorDao.getBooks(author);
}
}
BookBoImpl.javaCode:
package com.cambyze.my_library.services.bo.impl;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cambyze.my_library.services.bo.BookBo;
import com.cambyze.my_library.services.dao.AuthorDao;
import com.cambyze.my_library.services.dao.BookDao;
import com.cambyze.my_library.services.model.Author;
import com.cambyze.my_library.services.model.Book;
/**
* Business Object (BO) for the books.
*
* @author admin
*
*/
@Service("bookBo")
public class BookBoImpl implements BookBo {
/**
* Declaration of a logger.
*/
private static final Logger LOGGER = LoggerFactory.getLogger(BookBoImpl.class);
/**
* DAO used to manage book in the DB.
*/
@Autowired
private transient BookDao bookDao;
/**
* DAO used to manage author in the DB.
*/
@Autowired
private transient AuthorDao authorDao;
/**
* {@inheritDoc}
*/
public Long save(final Book book) {
return bookDao.save(book);
}
/**
* {@inheritDoc}
*/
public void update(final Book book) {
bookDao.update(book);
}
/**
* {@inheritDoc}
*/
public void delete(final Book book) {
bookDao.delete(book);
}
/**
* {@inheritDoc}
*/
public Book findByIsbn(final String isbn) {
return bookDao.findByIsbn(isbn);
}
/**
* {@inheritDoc}
*/
public Book findById(final Long id) {
return bookDao.findById(id);
}
/**
* {@inheritDoc}
*/
public Long saveWithAuthors(final Book book, final List<Author> listAuthor) {
// save book.
Long bookId = null;
bookId = bookDao.save(book);
LOGGER.debug("book saved with id : {}", bookId);
// Add authors if not exist.
List<Author> authors = new ArrayList<Author>();
LOGGER.debug("saveWithAuthors called for the book : {} and {} authors", book.toString(), listAuthor.size());
for (Author author : listAuthor) {
LOGGER.debug("one of the author : {}", author.getName());
Author authorDB = authorDao.findByName(author.getName());
// if INVALID_AUTHOR, it means not in DB.
if (authorDB.getName() == Author.INVALID_AUTHOR) {
// store the author in DB.
Long authorId = authorDao.save(author);
LOGGER.debug("author id created : {}", authorId);
authors.add(author);
} else {
// retrieve the id of the author in DB.
Long authorId = authorDB.getId();
LOGGER.debug("author id retrieved : {}", authorId);
authors.add(authorDB);
}
}
// update book with relationship
bookDao.addAuthors(book, authors);
LOGGER.debug("book updated with its authors : {}", authors.toString());
return bookId;
}
}
src/test/javaMyLibraryTest.javaCode:
package com.cambyze.my_library.services;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.oclc.xid.client.jdk14.ClientException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.cambyze.my_library.services.bo.AuthorBo;
import com.cambyze.my_library.services.bo.BookBo;
import com.cambyze.my_library.services.model.Author;
import com.cambyze.my_library.services.model.Book;
import com.cambyze.my_library.services.xisbn.Xisbn;
/**
* Unit test for books management
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:/test-spring-services.xml" })
@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class MyLibraryTest {
@Autowired
@Qualifier("bookBo")
private BookBo bookBo;
@Autowired
@Qualifier("authorBo")
private AuthorBo authorBo;
/**
* Declaration of a logger.
*/
private static final Logger LOGGER = LoggerFactory.getLogger(MyLibraryTest.class);
/**
* Test Book database management.
*/
@Test
@Rollback(true)
public void testBookDB() {
Book book = new Book();
book.setIsbn("0000");
book.setTitle("Test spring-hibernate");
if (bookBo == null) {
LOGGER.error("No spring injection of bookBo");
}
Long bookId = bookBo.save(book);
LOGGER.info("Book inserted with id : {}", bookId);
book = bookBo.findByIsbn("0000");
LOGGER.info("Book retrieved : " + book.toString());
assertEquals("id should be equal", bookId, book.getId());
bookBo.delete(book);
LOGGER.info("Book deleted");
book = bookBo.findByIsbn("0000");
LOGGER.info("book from db after deletion : {}", book.toString());
LOGGER.debug("isbn invalid : {}", book.getIsbn());
assertEquals("isbn should be invalid", Book.INVALID_ISBN, book.getIsbn());
}
/**
* Test Book database management.
*/
@Test
@Rollback(true)
public void testBookDB2() {
// now test with authors
// two books
Book book1 = new Book();
book1.setIsbn("0001");
book1.setTitle("Test spring-hibernate 1");
Book book2 = new Book();
book2.setIsbn("0002");
book2.setTitle("Test spring-hibernate 2");
// two authors
Author author1 = new Author();
author1.setName("foo1");
Author author2 = new Author();
author2.setName("foo2");
// link between them
ArrayList<Author> list1 = new ArrayList<Author>();
list1.add(author1);
ArrayList<Author> list2 = new ArrayList<Author>();
list2.add(author1);
list2.add(author2);
// now store the books
Long bookId1 = bookBo.saveWithAuthors(book1, list1);
LOGGER.debug("1st book : {} inserted with id : {}", book1.toString(), bookId1);
Long bookId2 = bookBo.saveWithAuthors(book2, list2);
LOGGER.debug("2nd book : {} inserted with id : {}", book2.toString(), bookId2);
// list of books of the author1
Author author = authorBo.findByName("foo1");
List<Book> listBooks = authorBo.getBooks(author);
if (listBooks == null) {
LOGGER.error("nb of books of 1st author is null");
fail("nb of books of 1st author is null");
} else {
LOGGER.debug("nb of books of 1st author : {} content : {}", listBooks.size(), listBooks.toString());
assertEquals("Nb of books should be 2", 2, listBooks.size());
}
// now deleting books & authors
bookBo.delete(book1);
Book book = bookBo.findByIsbn("0001");
LOGGER.info("book from db after deletion : {}", book.toString());
LOGGER.debug("isbn invalid : {}", book.getIsbn());
assertEquals("isbn should be invalid", Book.INVALID_ISBN, book.getIsbn());
bookBo.delete(book2);
book = bookBo.findByIsbn("0002");
LOGGER.info("book from db after deletion : {}", book.toString());
LOGGER.debug("isbn invalid : {}", book.getIsbn());
assertEquals("isbn should be invalid", Book.INVALID_ISBN, book.getIsbn());
authorBo.delete(author1);
author = authorBo.findByName("foo1");
LOGGER.info("author from db after deletion : {}", author.toString());
assertEquals("name should be invalid", Author.INVALID_AUTHOR, author.getName());
authorBo.delete(author2);
author = authorBo.findByName("foo2");
LOGGER.info("author from db after deletion : {}", author.toString());
assertEquals("name should be invalid", Author.INVALID_AUTHOR, author.getName());
}
...
}