Persistence4j 1.1发布,Java ORM框架
近日,Persistence4j 1.1版本发布了,它是一个小型、轻量级的Java 对象持久层类库,实现关系数据库和Java 对象之间的持久化。此版本主要是对bug的修复,点击查看更新详情:http://code.google.com/p/persistence4j/
Persistence4j的目标就是开发一款操作简单使用方便的Java ORM框架,秉承这一设计理念设计出的Persistence4j拥有及其简单的配置,其语法基于JDK 1.6中的注释,使用起来十分方便。
实例代码:
- //First lets create a simple pojo which you like to persist.
- @Entity(schema="library",table="book")
- public class Book{
- @Column(isPrimaryKey=true)
- private String isbn;
- @Column
- private String title;
- @Column
- private int authorid;
- public Book(){
- }
- public Book(String isbn, String title, int authorid){
- this.isbn = isbn;
- this.title = title;
- this.authorid = authorid;
- }
- // getters
- }
- DataProviderFactory dataProviderFactory = new DataProviderFactoryImpl(config);
- String databaseName = "library";
- String dbmsName = "mysql"
- boolean isTransactional = false;
- DataProvider dataProvider = dataProviderFactory.getDataProvider(databaseName, dbmsName, isTransactional);
- // Now lets create a object of Book class and persist it
- Book book = new Book("123432","TestBook",5);
- TransferUtil.registerClass(Book.class);
- GenericDAO<Book> genericDAO = new GenericDaoImpl<Book>(dataProvider.getDataFetcher());
- //Persist Book
- genericDAO.createEntity(book);
- //Remove Book
- genericDAO.deleteEntity(book);
- //Test if Entity Exists
- genericDAO.isEntityExists(book);
- // findByPrimaryKey
- Object obj[] = new Object[1];
- obj[0] = "123432";
- genericDAO.findByPrimaryKey(Book.class, obj);
- //If you want to use transactions.This how to get TransactionService.Make sure //isTransactional variable should be true and underlying dbms supports ACID.
- TransactionService ts = dataProvider.getTransactionService();
- try{
- ts.beginTransaction();
- genericDAO.createEntity(book);
- ts.commitTransaction();
- }catch(Exception exp){
- ts.rollbackTransaction();
- }
- // Check the GenericDAO interface for all the available methods..
- You can extend GenericDAOImpl and override the methods and add your own methods.