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

 




    
    
		org.hibernate.ejb.HibernatePersistence
        
        com.google.inject.persist.jpa.JpaTestEntity
        com.google.inject.persist.jpa.JpaParentTestEntity
        true

        
            
            
            
            
            
            
            

            
        
    


 

实体类

 

/**
 * 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 ([email protected]) */
@Entity
public class JpaTestEntity {
  private Long id;
  private String text;

  @Id @GeneratedValue
  public 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;
  }

  @Override
  public 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;
  }

  @Override
  public 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.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这样的实现。

好处就是启动快。。运行快。。调试,开发 的方便。

而且 代码也少很多了。 

  • Guice3_jpa_test.tar.gz (5.9 MB)
  • 下载次数: 65

你可能感兴趣的:(JPA,Hibernate,Google,HSQLDB,XML)