"无法验证新建立的连接"错误发生.
我用Google搜索并阅读与此错误相关的每个问题.但是没能找到解决方案.
我正在使用spring-boot-starter-data-jpa
.
它与Postgresql没有任何错误.但我想使用嵌入式数据库!
application.properties:
#We don't need JMX here - disabling it allows for faster startup
spring.jmx.enabled=false
spring.datasource.testOnBorrow=true
spring.datasource.validationQuery=SELECT 1
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.datasource.driver-class-name=org.hsqldb.jdbcDriver
spring.jpa.database-platform=org.hibernate.dialect.HSQLDialect
spring.datasource.url=jdbc:hsqldb:file:${user.home}/db/data;user=sa;password=123;
spring.datasource.username=sa
spring.datasource.password=123
Run Code Online (Sandbox Code Playgroud)
MainApplication类的标题:
@ComponentScan(value = {"db", "app", "ui"})
@EnableJpaRepositories(basePackages = "db")
@EntityScan(basePackages = "db")
@EnableTransactionManagement
@SpringBootApplication
Run Code Online (Sandbox Code Playgroud)
仅当我使用嵌入式数据库(至少是Derby,HSQLDB)时才抛出此错误,而不是总是如此.有时它会正常启动,查找并保存实体而不会出现错误,但有时会在等待一段时间后或成功交易后立即发生.
我怎么解决这个问题?
我试图说服"更高层次"使用querydsl sql来保持持久性.但是他们更喜欢spring jdbctemplate,原因是它提供了最好的原始性能.
表现是我们对课程的首要要求.这就是为什么JPA根本不是一个选择.QueryDSL SQL开销是否过多,无法从我们的选项中解脱出来?
我想知道是否有任何"最近的"性能测试来显示querydsl sql如何使用jdbctemplate和jpa.
我遇到过这个.我想知道querydsl sql与jdbctemplate和jpa实现进行比较时的相对性能.
我正在为一个大学项目编写一个简单的库 API。我有一个包含书籍的数据库,每个数据库都有自己的 ID。我正在使用 Spring Boot 来制作服务。我有一个BookRepository
which extendsJpaRepository<Book, Long>
和一个服务实现。
@Service
public class BookServiceImpl implements BookService{
@Autowired
private BookRepository bookRepository;
@Async
@Override
public void delete (Long id){
bookRepository.delete(id);
}
}
Run Code Online (Sandbox Code Playgroud)
稍后,REST 控制器处理请求:
@RestController
public class BookServiceController{
@Autowired
private BookService bookService;
@RequestMapping(value="books/{id}", method = RequestMethod.DELETE)
public ResponseEntity<Book> deleteBook (@PathVariable("id") Long id){
bookService.delete(id);
return new ResponseEntity<Book>(HttpStatus.OK);
}
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我要删除不在数据库中的 Book(例如 ID 为 123),则会抛出 EmptyResultDataAccessException。
我的问题是,我如何以及在哪里处理异常,以及如何避免以这种方式投射 NullPointerException?
提前致谢。
这就是我的hibernate.cfg.xml的样子.我的程序正在处理这个问题.我连接到MySQL数据库.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.default_schema">mintaalkalmazas2</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="Kerdes.hbm.xml"/>
<mapping resource="TestTable.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Run Code Online (Sandbox Code Playgroud)
这将是persistence.xml.我知道它还没有正确配置.我想连接到同一个MySQL数据库.
<?xml version="1.0" encoding="UTF-8" ?>
<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_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="todos" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>Todo</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306"></property>
<property name="javax.persistence.jdbc.user" value="root" />
<!-- EclipseLink should create the database schema automatically -->
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode"
value="database" …
Run Code Online (Sandbox Code Playgroud) 我正在使用Spring Boot和Spring Boot JPA编写一个组件.我有这样的设置:
界面:
public interface Something {
// method definitions
}
Run Code Online (Sandbox Code Playgroud)
实施:
@Component
public class SomethingImpl implements Something {
// implementation
}
Run Code Online (Sandbox Code Playgroud)
现在,我有一个运行的JUnit测试SpringJUnit4ClassRunner
,我想用它测试我的SomethingImpl
.
当我做
@Autowired
private Something _something;
Run Code Online (Sandbox Code Playgroud)
它有效,但是
@Autowired
private SomethingImpl _something;
Run Code Online (Sandbox Code Playgroud)
导致测试失败抛出一条NoSuchBeanDefinitionException
消息No qualifying bean of type [com.example.SomethingImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
但是在测试用例中,我想明确地注入my,SomethingImpl
因为它是我想要测试的类.我怎么做到这一点?
我想在spring with mongodb中使用spring将orderby添加到以下存储库方法中.我试过各种方法,但没有用
public interface StageRepository extends MongoRepository<Stage, String> {
@Query("{$and: [ { 'categoryId': { $eq: ?0 } }, { 'isDeleted': { $eq: ?1 } } ]}")
public List<Stage> findByCategoryIdAndIsNotDeleted(String categoryId, Boolean deleted);
}
Run Code Online (Sandbox Code Playgroud)
我想在查询中添加orderby'order'.
不知道怎么做.
例如,我想有@Nonnegative
,定义为@Min(0)
,并@DaySeconds
定义为@Min(0) @Max(86399)
.
我有一个非常标准的MVC设置,包括用于DAO层的Spring Data JPA存储库,一个处理Transactional关注和实现业务逻辑的Service层,以及一个具有一些可爱的基于REST的JSON端点的视图层.
我的问题是将Java 8批量采用Stream
到这个可爱的架构中:如果我的所有DAO都返回Stream
s,我的服务返回那些相同的Stream
s(但是做Transactional工作),我的视图处理并处理这些Stream
,然后通过当我的视图开始处理我的Stream
s中的Model对象时,Service层创建的事务将被关闭.如果底层数据存储尚未实现我的所有模型对象(它Stream
毕竟是尽可能的懒惰),那么我的视图将在尝试访问事务之外的新结果时出错.以前这不是问题,因为我会将结果完全实现到List中 - 但现在我们处于Stream
s 的勇敢新世界.
那么,处理这个问题的最佳方法是什么?完全将服务层内的结果实现为List并将其交还?让View层将Service层交给一个完成块,以便在事务内部进行进一步的处理吗?
谢谢您的帮助!
我想配置一个 Spring Boot 应用程序,以便根本不使用数据库。所以我注释了我的 Application 类以排除 JPA 自动配置类:
@SpringBootApplication
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class Application {
public static void main(final String... args) {
run(Application.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
当服务独立运行时,这工作正常
不幸的是,我的测试类似乎忽略了注释,尽管我使用 Application 类进行测试
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SwaggerJsonExistenceTest {
...
}
Run Code Online (Sandbox Code Playgroud)
测试失败并显示以下错误消息
引起:org.springframework.beans.factory.BeanCreationException:创建名为“dataSource”的bean时出错:调用init方法失败;嵌套异常是 java.lang.IllegalStateException:无法确定用于测试的嵌入式数据库。如果您想要一个嵌入式数据库,请在类路径上放置一个受支持的数据库。
更新:类路径上没有数据库驱动程序。
org.springframework.boot:spring-boot-starter-data-jpa 用于测试(通过 gradle 中的 testCompile 指令包含)
如何配置测试以使其不使用与数据库相关的自动配置?
修复:我已经删除了所有 jpa starter 依赖项(因为不需要 DB),所以根本没有完成数据源自动配置。
我正在使用 spring 框架和工作存储库级别的实现。
我有一堂课:
@Repository
public interface MyClassReadRepository extends ReadRepository<MyClass, Integer>
在这个类中有一个看起来像这样的方法:
@Query("SELECT a FROM MyClass a WHERE a.type IN :myType ORDER BY :someProperty :someOrder")
Page<MyClass> findByTypeAndOrder(@Param("myType") List<MyType> myType, @Param("someProperty")String someProperty, @Param("someOrder")String someOrder, Pageable pageable)
但显然查询结构是错误的:“:someProperty”应该是一个标识符......
我的问题是:如何在上面的示例中传递 order 和 sort 参数?
提前致谢!
java ×7
spring ×7
hibernate ×3
spring-boot ×3
spring-data ×2
spring-mvc ×2
annotations ×1
java-8 ×1
java-stream ×1
jdbc ×1
jpql ×1
mongodb ×1
mysql ×1
postgresql ×1
querydsl ×1
rest ×1
spring-mongo ×1