小编Oli*_*ohm的帖子

Spring Data JPA就像不依赖于Spring的项目

有没有人知道遵循存储库方法的任何Java框架,自动实现查询方法(例如findByNameAndLastName(…))但不依赖于Spring,只有纯JPA.GORM中也存在这样的特征.我想看看是否有任何项目可以在Guice或纯JavaEE环境中使用,而不会将Spring作为依赖项.

spring guice java-ee spring-data-jpa

6
推荐指数
1
解决办法
3063
查看次数

组合弹簧数据规范的查询在同一个表上有多个连接

对不起,如果我的术语不正确.

我们使用spring数据,JpaRepositories和条件查询作为查询数据库数据的方法.

我有一个问题,当我在下面的代码示例中结合两个规范,例如我在hasCityAndTimeZone中使用hasTimeZone和hasCity时,它会在同一个表上连接两次,所以下面的查询看起来像

select * from Staff, Location, Location
Run Code Online (Sandbox Code Playgroud)

有没有办法让这两个规范使用相同的连接而不是每个定义它们自己的连接基本相同?

对不起代码可能不完整我只是想展示一个简单的例子.

class Staff {
    private Integer id;
    private Location location;
}

class Location {
    private Integer id; 
    private Integer timeZone;
    private Integer city;
}

class StaffSpecs {
    public static Specification<Staff> hasTimeZone(Integer timeZone) {
        return new Specification<Staff>() {
            @Override
            public Predicate toPredicate(Root<Staff> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path<Integer> timeZonePath = root.join(Staff_.location).get(Location_.timeZone);
                return cb.equal(timeZonePath, timeZone);
            }
        }
    }

    public static Specification<Staff> hasCity(Integer city) {
        return new Specification<Staff>() {
            @Override
            public Predicate …
Run Code Online (Sandbox Code Playgroud)

spring jpa spring-data spring-data-jpa

6
推荐指数
1
解决办法
1万
查看次数

如何使用Spring Data JPA查询Map值?

所以我的数据库模型是这样的:我有Stores,每个Store都有一个本地化的名称.所以我选择将本地化名称表示为Map:

public class Store {
   private Map<Locale,LocalizedValue> name;
}
Run Code Online (Sandbox Code Playgroud)

你可以看到这是一个地图<Locale, LocalizedValue>,其中LocalizedValue是这样一类:

@Embeddable
public class LocalizedValue {

   @Column(name = "value")
   private String value;
}
Run Code Online (Sandbox Code Playgroud)

这一切都很棒.但是我遇到了一个问题,我想查询我的Spring Data JPA存储库并查找具有给定英文名称的所有商店.所以我的存储库方法如下所示:

Store findByName(Map.Entry<Locale, LocalizedValue> name);
Run Code Online (Sandbox Code Playgroud)

但它抛出了这个异常:

 2014-10-07 23:49:55,862 [qtp354231028-165] ERROR: Parameter value [en=Some Value] did not match expected type [com.test.LocalizedValue(n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [en=Some Value] did not match expected type [com.test.LocalizedValue (n/a)]
org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [en=Some Value] did not match expected type …
Run Code Online (Sandbox Code Playgroud)

java spring jpa spring-data spring-data-jpa

6
推荐指数
1
解决办法
2万
查看次数

Hibernate没有在Spring Boot Project中抛出LazyInitializationException

我使用Hibernate 5.0处理Spring Boot项目.不幸的是,LazyInitializationException即使提交了事务,Hibernate 也会在不抛出的情况下读取延迟的初始化对象.如何启用LazyInitializationException交易外部?

(当前行为隐藏了代码中的错误.)

java spring hibernate jpa

6
推荐指数
1
解决办法
1061
查看次数

如何使用 Spring Data JPA 保存具有手动分配标识符的实体?

我正在更新现有代码,该代码将一个表中的副本或原始数据处理为同一数据库中的多个对象。

以前,每种对象都有一个使用每个表的序列生成的 PK。

类似的东西:

@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
Run Code Online (Sandbox Code Playgroud)

为了重用导入表中的现有 ID,我们删除了某些实体的 GeneratedValue,如下所示:

@Id
@Column(name = "id")
private Integer id;
Run Code Online (Sandbox Code Playgroud)

对于这个实体,我没有改变我的 JpaRepository,看起来像这样:

public interface EntityRepository extends JpaRepository<Entity, Integer> {
    <S extends Entity> S save(S entity);
}
Run Code Online (Sandbox Code Playgroud)

现在我正在努力理解以下行为,在具有默认传播和隔离级别的 spring 事务 (@Transactional) 中:

  • 使用实体上的@GeneratedValue,当我调用 entityRepository.save(entity) 时,我可以看到 Hibernate show sql 已激活,插入请求被触发(但似乎只在缓存中,因为数据库没有改变)
  • 在实体上没有 @GeneratedValue 时,只会触发一个选择请求(没有插入尝试)

当我的实体(没有生成的值)在一个或多个关系中映射到 MyOtherEntity(有生成的值)时,这是一个大问题。

因此,我有以下错误:

ERROR: insert or update on table "t_other_entity" violates foreign key constraint "other_entity_entity"
Détail : Key (entity_id)=(110) is not present in table "t_entity" …
Run Code Online (Sandbox Code Playgroud)

java hibernate spring-data spring-data-jpa spring-boot

6
推荐指数
1
解决办法
5563
查看次数

如何在 Spring Data MongoDB 中使用低级驱动程序 API

我正在使用 Spring Data MongoDB。但我不想将我的结果映射到域类。此外,我想在少数情况下访问低级 MongoAB API。但我希望 spring 管理连接池等。

我怎样才能得到一个实例com.mongodb.MongoClient来执行低级操作。这是我想要做的:

MongoClient mongoClient = new MongoClient();
DB local = mongoClient.getDB("local");
DBCollection oplog = local.getCollection("oplog.$main");
DBCursor lastCursor = oplog.find().sort(new BasicDBObject("$natural", -1)).limit(1);
Run Code Online (Sandbox Code Playgroud)

或者我只是想要一个 JSON 对象/DBCursor/DBObject。

spring mongodb-java spring-data spring-data-mongodb

5
推荐指数
1
解决办法
2182
查看次数

为什么我的JPA实体的更改不会持久存储到数据库中?

在Spring Boot Applicaion中,我有Task一个状态在执行期间发生变化的实体:

@Entity
public class Task {

  public enum State {
    PENDING,
    RUNNING,
    DONE
  }

  @Id @GeneratedValue
  private long id;
  private String name;
  private State state = State.PENDING;

  // Setters omitted

  public void setState(State state) {
    this.state = state; // THIS SHOULD BE WRITTEN TO THE DATABASE
  }

  public void start() { 
    this.setState(State.RUNNING);

    // do useful stuff
    try { Thread.sleep(2000); } catch(InterruptedException e) {}
    this.setState(State.DONE);
  }
} 
Run Code Online (Sandbox Code Playgroud)

如果状态发生更改,则应将对象保存在数据库中.我正在使用这个Spring Data接口作为存储库:

 public interface TaskRepository extends CrudRepository<Task,Long> {}
Run Code Online (Sandbox Code Playgroud)

而这段代码创建并启动Task …

jpa spring-data spring-data-jpa

5
推荐指数
1
解决办法
4409
查看次数

部署到独立Tomcat时,Spring Boot应用程序无法正常工作

我已经按照本教程.现在,如果我通过Spring Boot使用它,它可以工作,但如果我尝试在Apache Tomcat 7上部署它(删除应用程序类),我会收到404响应.我也尝试过自己的配置 - 像这样:

@Configuration
public class MongoConfiguration {

    public @Bean MongoDbFactory mongoDbFactory() throws Exception {
        return new SimpleMongoDbFactory(new Mongo("127.0.0.1", 27017), "movies");
    }

    public @Bean MongoTemplate mongoTemplate() throws Exception {
        return new MongoTemplate(mongoDbFactory());
    }
}
Run Code Online (Sandbox Code Playgroud)

它仍然无法正常工作.所以2个问题.

  1. 如何在tomcat上运行它(如何在tomcat上进行简单的休息和运行)
  2. 如何通过注释配置mongo(只是为了获取我的数据库)

注意:默认情况下,它使用测试vile运行spring boot,我可以通过简单的控制器(而不是@RepositoryRestResource)使其工作,但我希望能够卷曲http://localhost:8080并获得选项响应.

spring-data-rest spring-data-mongodb spring-boot

5
推荐指数
1
解决办法
1万
查看次数

spring数据JPA方法findAll()与Predicate - QueryDslPredicateExecutor

我坚持这个错误:

我想使用一个使用谓词进行搜索的方法QueryDslPredicateExecutor.当该方法在我的服务实现上运行时,我收到此错误:

16:59:44,165 DEBUG [org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver] (http-localhost-127.0.0.1-9090-4) Resolving exception from handler [public br.com.cleartech.itx.web.dto.PageDto br.com.cleartech.itx.web.controller.CngController.list(br.com.cleartech.itx.core.domain.Cng,org.springframework.data.domain.Pageable)]: org.springframework.data.mapping.PropertyReferenceException: No property cng found for type Cng!
16:59:44,167 DEBUG [org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver] (http-localhost-127.0.0.1-9090-4) Invoking @ExceptionHandler method: public org.springframework.web.servlet.ModelAndView br.com.cleartech.itx.web.exception.WebExceptionHandler.runtime(java.lang.Exception)
16:59:44,169 ERROR [br.com.cleartech.itx.web.exception.WebExceptionHandler] (http-localhost-127.0.0.1-9090-4) No property cng found for type Cng!
16:59:44,170 DEBUG [org.springframework.web.servlet.DispatcherServlet] (http-localhost-127.0.0.1-9090-4) Handler execution resulted in exception - forwarding to resolved error view: ModelAndView: reference to view with name 'error'; model is {exception=org.springframework.data.mapping.PropertyReferenceException: No property cng found for type Cng!}: org.springframework.data.mapping.PropertyReferenceException: No property cng …
Run Code Online (Sandbox Code Playgroud)

java hibernate jpa querydsl spring-data

5
推荐指数
1
解决办法
4546
查看次数

如何将JpaTemplate的使用从Spring 3.2迁移到4.1.4?

当前,我们已经配置并运行了Spring 3.2.9.RELEASE(已经运行了几年),需要迁移到4.1.4.RELEASE。我们有一个抽象的DAO类,它扩展org.springframework.orm.jpa.support.JpaDaoSupport了以下内容:

  • org.springframework.orm.jpa.JpaCallback
  • org.springframework.orm.jpa.JpaTemplate

我已经看到JpaDaoSupport在Spring 4中已将其删除。我已删除了对Jpa *类的引用,并替换为

@PersistenceContext 
protected EntityManager theEntityManager;
Run Code Online (Sandbox Code Playgroud)

并在中找到我们DAO中的方法引用(如findByNamedParams()JpaDaoSupport,然后将其复制到我们的DAO中。

完成上述更改后,我们便可以编译我们的代码,但是当涉及到运行我们的JUnit测试时,我们applicationContext-test.xml

<bean id="jpaTemplate" class="org.springframework.orm.jpa.JpaTemplate">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<bean id="abstractDAO" abstract="true" class="my.company.package.AbstractDAO">
    <property name="jpaTemplate" ref="jpaTemplate" />
</bean>

<bean id="genericDAO" parent="abstractDAO" class="my.company.package.GenericDAO" />
<bean id="securityDAO" parent="abstractDAO" class="my.company.package.SecurityDAOImpl" />
Run Code Online (Sandbox Code Playgroud)

基本上错误是没有的类引用org.springframework.orm.jpa.JpaTemplate。我们如何JpaTemplate在Spring 4.1.4中替换此配置?

请注意,我选择的是这段代码,而不是最初配置系统的代码。另外,我对Spring及其配置设置还很陌生。

spring jpa spring-4

5
推荐指数
1
解决办法
3763
查看次数