Guice 3.0 学习 persist jpa
发布日期:2021-12-01 08:28:48 浏览次数:144 分类:技术文章

本文共 4917 字,大约阅读时间需要 16 分钟。

 

guice 3.0 的 persist 实现 使用的是 jpa 

 

代码可以从 googlecode 上面下载 

 

 

 

现在还是 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 (dhanji@gmail.com) */@Entitypublic 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这样的实现。

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

而且 代码也少很多了。 

转载地址:https://yanghuaiyuan.blog.csdn.net/article/details/83874363 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:一个基于Wave GWT Guice 的Sns 设想
下一篇:自己弄 开源 CMS 内容发布系统 主要表设计。

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月10日 06时40分42秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章