标签: spring-orm

NoUniqueBeanDefinitionException:没有定义[javax.persistence.EntityManagerFactory]类型的限定bean:期望的单个匹配bean

我有2个独立的数据库,我试图在存储库中访问它们.不幸的是,我收到以下异常.

我尝试过的事情

  • 尝试将其中一个bean作为Primary.
  • 使用了PersistenceContext,如下面的代码所示.

我的ExceptionTrace

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'orderController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.crud.repository.mymerkNew.OrderRepository com.crud.controller.OrderController.nextGenorder; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'orderRepository': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined: expected single matching bean but found 2: mymerkLimsEntityManagerFactory,nextGenEntityManagerFactory
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at …
Run Code Online (Sandbox Code Playgroud)

spring spring-mvc spring-orm spring-boot beancreationexception

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

JPA DescriptorEventAdapter ChangeSet始终为null

我正在尝试将更改跟踪器包含到我的JPA实体(到日志文件,而不是数据库),但是我的DescriptorEventAdapter返回的changeSet始终为null.我正在使用EclipseLink 2.5.2,ojdbc6,spring-orm 4.1.1.

调用所有事件(包括preUpdateWithChanges),并将更改推送到数据库.我正在使用entityManager.merge(实体)来更新实体.

HistoryEventListener.java

public class HistoryEventListener extends DescriptorEventAdapter {
    @Override
    public void preUpdate(DescriptorEvent event) {
        ObjectChangeSet changeSet = event.getChangeSet(); // Always null
    }

    @Override
    public void preUpdateWithChanges(DescriptorEvent event) {
        ObjectChangeSet changeSet = event.getChangeSet();
        ...
    };

    @Override
    public void postUpdate(DescriptorEvent event) {
        ...
    }

    @Override
    public void postMerge(DescriptorEvent event) {
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

一些实体

@Entity
@Table(name = "XXX", schema = "XXX")
@EntityListeners(HistoryEventListener.class)
@Cache(databaseChangeNotificationType = DatabaseChangeNotificationType.NONE, isolation = CacheIsolationType.ISOLATED)
public class XXXX implements Serializable {
  // id + fields
} …
Run Code Online (Sandbox Code Playgroud)

java jpa eclipselink spring-orm

10
推荐指数
1
解决办法
769
查看次数

Spring Hibernate与jdbc模板对比spring春天

我们有一个以MySQL数据库和Spring为框架的项目.我是Spring的新手,希望实现数据库访问层,并发现有几个选项,比如

  • 使用Spring + Hibernate
  • 使用Spring JDBC模板
  • 使用Spring ORM模块

我已经浏览了stackoverflow中的各个帖子,并在网上进行了一些研究,但每个问题都有不同的答案支持不同的选项.此外,我确实看到提及现在不推荐使用Spring JDBC模板.

该应用程序每小时可能有大约1000个事务,大约有60%的读取和40%的写入.

任何人都可以帮我找到答案,哪3个选项适合哪个,为什么?或者,如果你能指出一些资源,那也将受到高度赞赏.

hibernate spring-orm spring-data

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

java.lang.ClassNotFoundException:org.springframework.orm.hibernate4.LocalSessionFactoryBean

我是Spring的新手,这是我的第一个例子(JSF 2,PrimeFaces 3,Spring和Hibernate Integration),这里是pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-  
v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>MVNO.ONP.project</groupId>
<artifactId>MVNOONPProject</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>MVNOONPProject Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>3.8.1</version>
  <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring</artifactId>
    <version>2.5.6.SEC03</version>
</dependency>
<dependency>
  <groupId>com.sun.faces</groupId>
  <artifactId>jsf-api</artifactId>
  <version>2.1.0-b03</version>
</dependency>
<dependency>
  <groupId>com.sun.faces</groupId>
  <artifactId>jsf-impl</artifactId>
  <version>2.1.0-b03</version>
</dependency>
 <dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>jstl</artifactId>
  <version>1.2</version>
</dependency>
 <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>4.1.9.Final</version>
 </dependency>

 <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.22</version>
 </dependency>
 </dependencies>
 <build>
  <finalName>MVNOONPProject</finalName>
 </build>
 </project>
Run Code Online (Sandbox Code Playgroud)

当我运行该项目时,我收到此错误:

Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.orm.hibernate4.LocalSessionFactoryBean] for bean with name 'SessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]; …
Run Code Online (Sandbox Code Playgroud)

spring maven spring-orm

9
推荐指数
3
解决办法
4万
查看次数

使用Spring JPA和Hibernate访问Session以启用过滤器

在Spring JPA + Hibernate环境中,我需要启用Hibernate实体过滤器.所以我应该有权访问Hibernate Session对象,但我正在使用EntityManagerFactory和Spring JPA魔法.有任何Session拦截器,所以每次Spring创建一个新Session时我都可以调用enableFilters()方法吗?

spring hibernate spring-orm

7
推荐指数
1
解决办法
1620
查看次数

org.hibernate.UnknownEntityTypeException:无法找到persister:entity.Settings

我正在尝试将Hibernate 5(5.2.11)与Spring ORM一起使用.

在以下教程中,我提出了以下配置:

春豆

<bean id="sessionFactorySettings" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
            <prop key="hibernate.connection.driver_class">org.h2.Driver</prop>
            <prop key="hibernate.connection.url">jdbc:h2:~/.dummy/settings</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
    <property name="mappingResources">
        <list>
            <value>/hibernate.cfg.xml</value>
        </list>
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

Hibernate(hibernate.cfg.xml)

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <mapping class="entity.Settings"/>
    </session-factory>
</hibernate-configuration>
Run Code Online (Sandbox Code Playgroud)

这种配置导致了org.hibernate.UnknownEntityTypeException: Unable to locate persister: entity.Settings.

但是,一旦我全部移动

<prop key="hibernate.xxx">..</prob>
Run Code Online (Sandbox Code Playgroud)

将属性转换为hibernate.cfg.xml并将Spring配置更改为

<bean id="sessionFactorySettings" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="configLocation" value="/hibernate.cfg.xml"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

一切正常.

知道我做错了吗?

PS:依赖关系:

dependencies {
    compile 'ch.qos.logback:logback-classic:1.2.3'
    compile 'org.springframework:spring-context:4.3.11.RELEASE'
    compile 'org.springframework:spring-jdbc:4.3.11.RELEASE' …
Run Code Online (Sandbox Code Playgroud)

spring hibernate spring-orm hibernate-5

7
推荐指数
1
解决办法
4480
查看次数

二级缓存在 Hibernate + Spring + JPA 和 EhCache 中不起作用

让我清楚地了解二级缓存。我的 Web 应用程序的基类中有一个查询。几乎每个操作都会调用此查询(我使用的是 Struts,这就是应用程序的设计方式,因此不能真正弄乱它),例如加载我的主页调用三个单独的 Struts 操作,并且为每个操作执行此查询。QueryDsl 形式的查询看起来像 Iterable<Event> eventsFromDb2 = eventRepository.findAll(EventExpressions.queryAllEvents()); ,在简化形式中看起来像Select e from Event e where e.deleted = false

这个查询占用了大约 10 秒的甜蜜时间,因此它使应用程序变得非常慢,因为它调用了 Web 应用程序的每个操作(CRUD)。根据我的理解,在启用二级缓存时,hibernate+ Spring orm 应该从缓存中获取结果并避免数据库请求。但是,它不起作用。persistence.xml 如下所示

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc
                       http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
                       http://www.springframework.org/schema/beans
                       http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                       http://www.springframework.org/schema/tx
                       http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">



<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" depends-on="flyway">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="de.mm.moreevent.type" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="false" />
            <property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="generateDdl" value="true" />
        </bean>
    </property>
<property name="jpaPropertyMap">
    <map>
        <entry key="hibernate.cache.use_query_cache" value="true" />
        <entry key="hibernate.cache.use_second_level_cache" …
Run Code Online (Sandbox Code Playgroud)

hibernate jpa ehcache second-level-cache spring-orm

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

无法从数据库 ERROR 中获取 SequenceInformation 但一切正常

我已经创建了 user 和 userRole 表

用户实体

@Entity
@Table(name = "USERS")
public class User {
    @Id
    @Column(name = "USERNAME",  nullable = false,  unique = true)
    private String username;

    @Column(name = "PASSWORD", nullable = false)
    private String password;

    @Column(name = "ENABLED", nullable = false)
    private boolean enabled = true;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "user", fetch = FetchType.EAGER)
    private Set<UserRole> userRole = new HashSet<>();
Run Code Online (Sandbox Code Playgroud)

用户角色实体

@Entity
@Table(name = "USER_ROLES", uniqueConstraints = @UniqueConstraint(
        columnNames = { "ROLE", "USERNAME" }))
public class UserRole { …
Run Code Online (Sandbox Code Playgroud)

spring hibernate spring-orm

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

使用@Transactional的类层次结构问题

项目设置:Spring 3.0.5 / JPA 2 / Hibernate / @Transactional

我们使用多个不同的数据源(因此也使用不同的事务管理器),但具有通用的服务基类,因为许多功能被重用。

所以我们认为可以通过使用自定义注释来解决这个问题。我们在抽象基类中定义所有方法,并为每个事务管理器创建一个空的实现类。

现在的问题是:

在 中AbstractFallbackTransactionAttributeSource,这是正在完成的查找:

TransactionAttribute txAtt = findTransactionAttribute(specificMethod);
// ...
// Second try is the transaction attribute on the target class.
txAtt = findTransactionAttribute(specificMethod.getDeclaringClass());
Run Code Online (Sandbox Code Playgroud)

首先检查方法,然后检查声明该方法的类(及其祖先)。但在我们的上下文中,自定义注释驻留在子类中,无法通过向上查找来找到该子类。

那么我们该如何解决这个问题呢?

【这个问题我也在Spring社区论坛问过】

java spring spring-aop spring-transactions spring-orm

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

为什么 Spring-Data-JPA 异步不起作用?

我正在尝试使用 Spring Boot 和 Spring data JPA 创建一个非阻塞休息服务。

如何使用 Spring Data JPA @Async 支持对实体进行异步保存。尽管其他选择似乎在同一实体上工作,但下面的代码对我不起作用。

我正在尝试在 JPA 存储库中执行此操作。这是完整的存储库:除了保存。这些方法运行良好,我可以测试它们

public interface LoanRepository extends JpaRepository<Loan,Long> {

@Query("select distinct loan from Loan loan left join fetch loan.collaterals left join fetch loan.partys")
@Async
CompletableFuture<List<Loan>> findAllWithEagerRelationships();

@Query("select loan from Loan loan left join fetch loan.collaterals left join fetch loan.partys where loan.id =:id")
@Async
CompletableFuture<Loan> findOneWithEagerRelationships(@Param("id") Long id);

@Async
void delete(Long id);
Run Code Online (Sandbox Code Playgroud)

}

但是,当我尝试添加以下保存方法时:

    @Async
    <S extends CompletableFuture<Loan>> S save(Loan loan);
Run Code Online (Sandbox Code Playgroud)

我收到一个明显的编译错误,上面写着"The method save(Loan) is ambiguous …

asynchronous spring-orm spring-data-jpa

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