所有Spring配置都已正确编写.非Multi-Exec Redis操作完美运行.
@Autowired
@Qualifier("stringRedisTemplate")
StringRedisTemplate template;
void test(){
template.multi();
template.boundValueOps("somevkey").increment(1);
template.boundZSetOps("somezkey").add("zvalue", timestamp);
template.exec();
}
Run Code Online (Sandbox Code Playgroud)
通过Junit Test运行上面的代码后,抛出了异常.
org.springframework.data.redis.RedisSystemException: Unknown exception; nested exception is org.springframework.data.redis.RedisSystemException: Unknown jedis exception; nested exception is java.lang.NullPointerException
at org.springframework.data.redis.connection.jedis.JedisUtils.convertJedisAccessException(JedisUtils.java:93)
at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.translateExceptionIfPossible(JedisConnectionFactory.java:155)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:58)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:163)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy66.appendUserStream(Unknown Source)
at com.uniu.test.repository.StreamCacheRepositoryTest.testAppendUserStream(StreamCacheRepositoryTest.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) …Run Code Online (Sandbox Code Playgroud) 根据其JavaDoc,PersistenceAnnotationBeanPostProcessor似乎负责使用注释@PersistenceContext注入EntityManager.它似乎暗示如果没有在Spring应用程序上下文xml中声明这个bean,@ PerersContext注释将不起作用.
但是,根据我的实验,这不是事实.
<persistence xmlns="http://java.sun.com/xml/ns/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_1_0.xsd"
version="1.0">
<persistence-unit name="default" transaction-type="RESOURCE_LOCAL" />
</persistence>
Run Code Online (Sandbox Code Playgroud)
<context:component-scan base-package="com.test.dao" />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="persistenceUnitName" value="default"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true"/>
<property name="generateDdl" value="true"/>
<property name="databasePlatform" value="org.hibernate.dialect.DerbyDialect"/>
</bean>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="url" value="jdbc:derby://localhost:1527/c:\derbydb\mydb"/>
<property name="username" value="APP"/>
<property name="password" value="APP"/>
</bean>
<tx:annotation-driven/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!--
<bean id="persistenceAnnotation" class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
-->
Run Code Online (Sandbox Code Playgroud)
@Repository("userDao")
public class UserDaoImpl …Run Code Online (Sandbox Code Playgroud) 我尝试使用MongoDB访问数据的Spring Guide.我无法弄清楚的是如何配置我的代码不使用默认服务器地址而不使用默认数据库.我已经看到很多方法可以用XML来完成它,但我试图保持完全没有XML的配置.
有没有人有一个例子来设置没有XML的服务器和数据库,并且可以很容易地集成到他们在Spring Guide中显示的示例中?
注意:我确实找到了如何设置集合(在此页面上搜索短语"我的文档将保存到哪个集合中").
谢谢!
与JPA春季指南相同的故事 - 如何配置数据库属性 - 但这是另一篇文章:)
我有一个@Entity Video有一个一对多有关系,List<Tag> tags因为它的领域之一.我用下面的@Repository使用弹簧的数据以获得最热门的标签:
@Repository
public interface TagRepository extends CrudRepository<Tag, Integer>{
@Query("SELECT t FROM Tag t WHERE (SELECT SUM(v.views) FROM Video v WHERE t MEMBER OF v.tags) > 0")
public List<Tag> findMostViewedTags(int maxTags);
}
Run Code Online (Sandbox Code Playgroud)
该查询处理和认为是有效的春天,我测试生成的SQL VS我的数据库在本地,它返回2个标签.但是在我的代码中,当我调用方法findMostViewedTags(100)时,我收到值Null.
查询查找策略是默认的"CREATE_IF_NOT_FOUND".
Null而不是List<Tag>size()2?说我有一个像这样的postgres表:
CREATE TABLE sal_emp (
name text,
pay_by_quarter integer[],
schedule text[][]
);
Run Code Online (Sandbox Code Playgroud)
我甚至可以使用Spring数据插入到列pay_by_quarter或schedule?如果可能的话,它将如何看作存储库和实体?我无法找到解决此问题的任何文档或示例,可能是因为它与更常见的用例重叠,将多个表作为一对多关系插入.说到这一点,我完全打算使用Postgresql array数据类型而不使用关系表.
什么是典型的现实生活场景,人们会选择Spring Data JDBC/Spring Data JPA vs Hibernate?我想了解其中任何一种实现最适合的场景.
我是hibernate世界的新手,面对,
WARN: GenerationTarget encountered exception accepting command : Error executing DDL via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL via JDBC Statement
Run Code Online (Sandbox Code Playgroud)
我在hibernate 5.2.9版本中运行我的独立程序时出现异常.但是在hibernate 4版本中我的代码运行良好.我寻找了许多问题并解决了但我没有得到的答案.
配置文件
hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">admin</property>
<property name="hibernate.connection.pool_size">20</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping class="com.test.hibernate14417.MyTable"></mapping>
</session-factory>
</hibernate-configuration>
Run Code Online (Sandbox Code Playgroud)
的pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>Hibernate14417</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<repositories>
</repositories>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.9.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.1.Final</version>
</dependency> …Run Code Online (Sandbox Code Playgroud) 我在User和GameMap之间有一对多的关系.一个用户可以拥有许多地图.
用户类:
// LAZY LOADED
@OneToMany(cascade = CascadeType.ALL, mappedBy = "creater")
private final List<GameMap> maps = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)
但是,有时我需要急于加载地图.为了避免在关闭Session之后出现LazyInitializationException,我有两种检索用户的变体.
用户存储库:
public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findById( Long id );
@Query("SELECT u FROM User u JOIN FETCH u.maps WHERE u.id = (:id)")
public User findByIdEagerFetch( @Param("id") Long id );
}
Run Code Online (Sandbox Code Playgroud)
问题:
但是,如果表中没有此用户的映射,则JPQL JOIN FETCH变量可以在一个用户中执行,并且如果该用户没有映射,则他的映射将返回NULL用户.
问题:
如何重写JPQL语句以便检索用户并可选地(!)所有他的地图,但如果没有地图,那可以,但不要返回NULL用户.
我有一个带有另一个对象列表的对象.它映射如下:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "products")
public class Product extends DateAudit {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank
@Size(min = 3, max = 30)
private String name;
@NotBlank
private String shortDescription;
@NotBlank
private String description;
@NotNull
private Double regularPrice;
private Double promotionPrice;
@NotNull
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "category_id", nullable = false)
private Category category;
@NotNull
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "store_id", nullable = false)
private Store store;
@Size(max …Run Code Online (Sandbox Code Playgroud) 我使用Vert.x工具包来创建反应式应用程序,支持MySQL和Postgres等关系数据库.我知道Spring为像Cassandra和Mongo这样的NoSQL DB提供了反应支持,但是他们是否愿意为关系数据库提供相同的支持?
spring-data ×10
java ×9
hibernate ×4
spring ×4
jpa ×3
java-ee ×1
jdbc ×1
lazy-loading ×1
mongodb ×1
one-to-many ×1
postgresql ×1
r2dbc ×1
reactive ×1
redis ×1