几天来我一直试图找到这个问题的答案,我希望有人能指出我正确的方向。我有一个使用 Java 配置的 SpringMVC 应用程序,在我尝试将 Apache-Shiro 集成到其中之前,我一直做得很好。我能够构建和运行我的测试。但是由于代理/CGLIB 问题,我的部署失败了。
这是我在部署/重启时遇到的异常:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'menuRepository': Post-processing of the FactoryBean's object failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class com.sun.proxy.$Proxy69]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Cannot subclass final class class com.sun.proxy.$Proxy69
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:165)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:102)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1454)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:442)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:416)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:550)
at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:150)
at …Run Code Online (Sandbox Code Playgroud) 最近一直在用Spring boot + spring data jpa + hibernate。我在 spring 事务中遇到了一个问题。这是我的服务类和两个问题:
@Transactional
@Service
class MyService {
@Autowired
private MyRepository myRep;
public void method_A() {
try {
method_C();
.....
method_B();
} catch(Exception e) {}
}
public void method_B() {
Entity e = new Entity();
e.set(...);
myRep.save(e);
}
public void method_C() throws Exception {
....
}
}
Run Code Online (Sandbox Code Playgroud)
1.如果方法method_C()抛出异常,我想捕获它并记录它,事务不会在方法中回滚method_B(),因为异常没有到达Spring框架。那么我应该怎么做才能捕获异常method_C()并同时不失去method_B()回滚方法的能力?
2.考虑新方法method_A()。
public void method_A() {
for(...) {
...
...
method_B();
}
}
Run Code Online (Sandbox Code Playgroud)
我想method_B()在循环中调用。如果在 …
在 JPA Spring 数据中使用 DELETE 别名是否有任何已知问题?这就像删除别名的魅力一样。我有一个 JPA Spring 数据本机查询,如下所示:-
@Modifying
@Transactional
@Query(value = "delete from Customer d where d.name = ?1", nativeQuery = true)
public void removeOnName(String name);
Run Code Online (Sandbox Code Playgroud)
这将引发如下异常:-
01:11:13.616 [main] WARN o.h.e.jdbc.spi.SqlExceptionHelper - SQL Error: 1064, SQLState: 42000
01:11:13.616 [main] ERROR o.h.e.jdbc.spi.SqlExceptionHelper - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'd where d.name = 'kiran'' at line 1
01:11:13.621 [main] WARN …Run Code Online (Sandbox Code Playgroud) 在我的测试项目中,我通过 spring-data-redis 和 jedis 构建了我的缓存系统。和valueSerializer的RedisTemplate是GenericJackson2JsonRedisSerializer。但这里有一个问题,当我从 java.lang.Long 类型的缓存中范围一个列表时,有一个java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long. 任何人都知道原因,它让我困惑了很长时间;
Redis 配置细节如下:
<bean id="objRedisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="redisConnectionFactory" />
<property name="keySerializer">
<bean
class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer">
<bean
class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
代码详情如下:
public List<Long> findByProductId(Long productId) {
String key = ThemeRedisKeyGenerator.genProductThemeKey(productId);
List<Long> themeIdList = redisService.range(key, Long.class);
if (themeIdList == null) {
themeIdList = themeProductMapper
.selectThemeIdByProductId(productId);
if (!redisService.containKey(key)) {
redisService.rightPush(key,
ThemeRedisKeyGenerator.PRODUCT_THEME_KEY_EXPIRE_HOURS,
TimeUnit.HOURS, themeIdList.toArray());
}
}
return themeIdList;
} …Run Code Online (Sandbox Code Playgroud) 我想知道在使用 DELETE 方法调用我的 REST API 之后我必须返回什么。我无法为此找到任何标准/最佳实践。目前我的代码库使用 2 种不同的方法,首先将已删除的资源返回到响应正文中,我只返回 null。第二种方法(我不太喜欢)我实例化一个新对象并返回它。你认为最好的方法是什么?如果这两种方法对您来说都不是很好,那么哪一种是最好的(实践)方法?
注意:当然,所描述的两种方法都是在对 DB 进行实际删除之后执行的。
通常有一个业务问题来显示所有类别以及这些类别的使用频率。
这个问题很容易通过查询来回答:
SELECT c.*, count(*) FROM category_assignment ca LEFT JOIN category c on ca.c_id = c.id group by c.id
Run Code Online (Sandbox Code Playgroud)
我要问的是您建议的基于以下内容映射结果集的方法:
@Entity
public class CategoryAssignment {
@Id
int id;
@ManyToOne(fetch = FetchType.EAGER)
private Category category;
@ManyToOne(fetch = FetchType.EAGER)
private Car car;
}
@Entity
public class Category {
@Id
String id;
TypeEnum type;
...
}
@Entity
public class Car {
@Id
int id;
String name;
...
}
Run Code Online (Sandbox Code Playgroud)
从我的角度来看,映射的最佳结果是从存储库调用中获取一个包含类别作为实体和计数编号作为附加变量的自定义对象:
MappingResult result = repository.getCategoriesAndTheirCountOfType(TypeEnum type);
public class MappingResult {
Category category;
BigInteger count;
} …Run Code Online (Sandbox Code Playgroud) 目前我正在将 Hibernate(MySQL) 与 Spring 一起使用,配置对我来说运行良好,但是一旦我配置了另一个配置 mongo-config.xml 文件并尝试使用 mongodb 运行测试用例,它就会显示 Error created bean with name .. ..从第一次配置。
下面是我的 mongo-config.xml
<context:annotation-config />
<context:component-scan base-package="com.test.mongo" />
<context:property-placeholder location="classpath:mongo-dao.properties" />
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
</bean>
<bean id="mongoDbFactory" class="org.springframework.data.mongodb.core.MongoFactoryBean">
<property name="driverClassName" value="${spring.datasource.driverClassName}" />
<property name="host" value="${spring.data.mongodb.host}" />
<property name="port" value="${spring.data.mongodb.port}" />
<property name="databaseName" value="${spring.data.mongodb.database}" />
Run Code Online (Sandbox Code Playgroud)
我的第一个休眠配置看起来像
<context:component-scan base-package="com.hb.dao" />
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${db.jdbc.driverClassName}" />
<property name="url" value="${db.jdbc.url}" />
<property name="username" value="${db.jdbc.username}" />
<property name="password" value="${db.jdbc.password}" …Run Code Online (Sandbox Code Playgroud) 我一直在使用 JPA CRUD 存储库默认方法,例如 find、findAll、delete 等,用于我所有的数据库操作。
现在我有两个实体:
@Entity
public class Parent implements Serializable {
@Id
@GeneratedValue
private long id;
@OneToMany(mappedBy = "parent", cascade = CascadeType.REMOVE)
private Set<Child> children;
}
@Entity
public class Child implements Serializable {
@Id
@GeneratedValue
private long id;
@ManyToOne
@JoinColumn
private Parent parent;
}
Run Code Online (Sandbox Code Playgroud)
有没有办法让我在 ParentRepository 中创建一个新方法,让我根据父级的 ID 检索父级的所有子级的计数?
所以在我的 ParentRepository 中,我可以创建一个看起来像这样的方法:
int findAllChildrenCount(Long parentID);
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Spring Data/Crud Repository(.save) 在 DB 中保存一个实体,其中包含另一个通过 @Cache 方法加载的实体。换句话说,我试图保存一个包含 Attributes 实体的 Ad Entity,并且这些属性是使用 Spring @Cache 加载的。
因此,我将分离的实体传递给持久性异常。
我的问题是,有没有办法保存实体仍然使用@Cache 作为属性?
我查了一下,但找不到任何人做同样的事情,特别是我知道我使用的 CrudRepository 只有方法 .save(),据我所知,它管理持久化、更新、合并等。
很感谢任何形式的帮助。
提前致谢。
广告.java
@Entity
@DynamicInsert
@DynamicUpdate
@Table(name = "ad")
public class Ad implements SearchableAdDefinition {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
private User user;
@OneToMany(mappedBy = "ad", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Set<AdAttribute> adAttributes;
(.....) }
Run Code Online (Sandbox Code Playgroud)
广告属性.java
@Entity
@Table(name = "attrib_ad")
@IdClass(CompositeAdAttributePk.class)
public class AdAttribute {
@Id
@ManyToOne(fetch …Run Code Online (Sandbox Code Playgroud) 在我的存储库中,我需要将 Projection 用于 findById 方法:
@Repository
public interface ForumRepository extends CrudRepository<Forum,Integer> {
ForumDTO findById(Integer id);
}
Run Code Online (Sandbox Code Playgroud)
但是由于 findByID 已经存在于 CrudRepository 中,所以我不能用不同的返回类型覆盖它,还有其他方法可以存档吗?