toeo 发表于 2013-1-14 23:42:48

Guice 3.0 学习 persist jpa

 
guice 3.0 的 persist 实现 使用的是 jpa 
 
代码可以从 googlecode 上面下载 
 
http://code.google.com/p/google-guice/downloads/list
 
guice-3.0-rc3-src.zip
 
现在还是 beate的版本 但是可以 看api 学习了。
 
首先 测试使用的是 hsqldb 数据库
 
jpa 的配置 文件 test/META-INF/persistence.xml
 

<?xml version="1.0" encoding="UTF-8" ?><persistence xmlns="http://java.sun.com/xml/ns/persistence"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence      http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">    <!-- JPA test "unit" -->    <persistence-unit name="testUnit" transaction-type="RESOURCE_LOCAL"><provider>org.hibernate.ejb.HibernatePersistence</provider>                <class>com.google.inject.persist.jpa.JpaTestEntity</class>      <class>com.google.inject.persist.jpa.JpaParentTestEntity</class>      <exclude-unlisted-classes>true</exclude-unlisted-classes>      <properties>            <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>            <property name="hibernate.connection.url" value="jdbc:hsqldb:mem:persistence"/>            <property name="hibernate.connection.username" value="sa"/>            <property name="hibernate.connection.password" value=""/>            <property name="hibernate.connection.pool_size" value="2"/>            <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>            <property name="hiberante.cache_provider" value="org.hibernate.cache.NoCacheProvider"/>            <property name="hibernate.hbm2ddl.auto" value="create"/>      </properties>    </persistence-unit></persistence> 
实体类
 

/** * Copyright (C) 2010 Google, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.google.inject.persist.jpa;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;/** @author Dhanji R. Prasanna (dhanji@gmail.com) */@Entitypublic class JpaTestEntity {private Long id;private String text;@Id @GeneratedValuepublic Long getId() {    return id;}public void setId(Long id) {    this.id = id;}public String getText() {    return text;}public void setText(String text) {    this.text = text;}@Overridepublic boolean equals(Object o) {    if (this == o) {      return true;    }    if (o == null || getClass() != o.getClass()) {      return false;    }    JpaTestEntity that = (JpaTestEntity) o;    if (id != null ? !id.equals(that.id) : that.id != null) {      return false;    }    if (text != null ? !text.equals(that.text) : that.text != null) {      return false;    }    return true;}@Overridepublic int hashCode() {    int result = id != null ? id.hashCode() : 0;    result = 31 * result + (text != null ? text.hashCode() : 0);    return result;}} 
测试的方法
 

public static class TransactionalObject {    private final EntityManager em;    @Inject    public TransactionalObject(EntityManager em) {      this.em = em;    }    @Transactional    public void runOperationInTxn() {      JpaTestEntity entity = new JpaTestEntity();      entity.setText(UNIQUE_TEXT);      em.persist(entity);    }    @Transactional    public JpaTestEntity runOperationInTxnWithMerge() {      JpaTestEntity entity = new JpaTestEntity();      entity.setText(UNIQUE_TEXT_MERGE);      return em.merge(entity);    }    @Transactional(rollbackOn = IOException.class)    public void runOperationInTxnThrowingChecked() throws IOException {      JpaTestEntity entity = new JpaTestEntity();      entity.setText(TRANSIENT_UNIQUE_TEXT);      em.persist(entity);      throw new IOException();    }    @Transactional    public void runOperationInTxnThrowingUnchecked() {      JpaTestEntity entity = new JpaTestEntity();      entity.setText(TRANSIENT_UNIQUE_TEXT);      em.persist(entity);      throw new IllegalStateException();    }} 在 一个事物 拦截的地方 用@Transactional 标记 其中 EntityManager em; 通过构造 创建
 
JpaPersistModule extends PersistModule 这个 在 src 目录 下。也就是在 guice3.0 的jar 里面。
 

private Properties properties;private MethodInterceptor transactionInterceptor;@Override protected void configurePersistence() {    bindConstant().annotatedWith(Jpa.class).to(jpaUnit);    if (null != properties) {      bind(Properties.class).annotatedWith(Jpa.class).toInstance(properties);    } else {      bind(Properties.class).annotatedWith(Jpa.class)          .toProvider(Providers.<Properties>of(null));    }    bind(JpaPersistService.class).in(Singleton.class);    bind(PersistService.class).to(JpaPersistService.class);    bind(UnitOfWork.class).to(JpaPersistService.class);    bind(EntityManager.class).toProvider(JpaPersistService.class);    bind(EntityManagerFactory.class)      .toProvider(JpaPersistService.EntityManagerFactoryProvider.class);    transactionInterceptor = new JpaLocalTxnInterceptor();    requestInjection(transactionInterceptor);    // Bind dynamic finders.    for (Class<?> finder : dynamicFinders) {      bindFinder(finder);    }} 其中 那个 finder 没有明白。。
 
其中 
 

    bind(JpaPersistService.class).in(Singleton.class);    bind(PersistService.class).to(JpaPersistService.class);    bind(UnitOfWork.class).to(JpaPersistService.class);    bind(EntityManager.class).toProvider(JpaPersistService.class);    bind(EntityManagerFactory.class)      .toProvider(JpaPersistService.EntityManagerFactoryProvider.class); 就是 帮定了 jpa的 类。初始化了事物,和事物工厂类。
 
附件 是 官方的 jpa的 测试 代码。
 
jpa 的 实现用的 是 hibernate 。
 
等 guice 3.0 正式发布了 打算 将现在的工程 修改下。。变成 struts2+guice3+jpa这样的实现。
好处就是启动快。。运行快。。调试,开发 的方便。
而且 代码也少很多了。 
页: [1]
查看完整版本: Guice 3.0 学习 persist jpa