- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 JPA 实体中使用自定义类型,然后将其转换为 JSON 字符串并将其存储到数据库中时遇到问题。
这是基本的 JPA 实体:Discount(我们使用 Lombok 和构建器来创建对象实例。问题发生在 LocalizedTexts 类型的两个字段上):
@Value
@NoArgsConstructor(force = true)
@AllArgsConstructor
@Builder(builderMethodName = "internalBuilder")
@Entity
@Table(name="discount")
public class Discount {
@Id
@Column(name = "ID", nullable = false, unique = true)
@Type(type="uuid-char")
UUID id;
@Column(name="NO", nullable = false, unique = true)
String no;
@Column(name="I_NO", nullable = false, unique = true)
Integer iNo;
@Convert(converter = JpaJsonConverter.class)
@Column(name="DESIGNATION", nullable = false)
LocalizedTexts designation;
@Convert(converter = JpaJsonConverter.class)
@Column(name="PRINT_TEXT")
LocalizedTexts printText;
...
/**
* Builder with required parameters
*/
public static Discount.DiscountBuilder builder(UUID id, String no, LocalizedTexts designation) {
return internalBuilder()
.id(id)
.no(no)
.designation(designation);
}
// Getters
...
}
我正在使用属性转换器来转换自定义类型(扩展 Map
@Converter
public class JpaJsonConverter implements AttributeConverter<Object, String> {
private final static ObjectMapper objectMapper = new ObjectMapper();
@Override
public String convertToDatabaseColumn(Object localizedTexts) {
String localizedTextsJson = null;
try {
localizedTextsJson = objectMapper.writeValueAsString(localizedTexts);
} catch (JsonProcessingException ex) {
//throw new RuntimeException();
}
return localizedTextsJson;
}
@Override
public Object convertToEntityAttribute(String localizedTextsJSON) {
Object localizedTexts = null;
try {
localizedTexts = objectMapper.readValue(localizedTextsJSON, Object.class);
} catch (IOException ex) {
//throw new RuntimeException();
}
return localizedTexts;
}
}
这是自定义类型:LocalizedTexts (Map
public class LocalizedTexts extends HashMap<Language, String> implements EntityBase {
public LocalizedTexts() {
}
public LocalizedTexts(Map map) {
this.putAll(map);
}
public static LocalizedTextsBuilder internalBuilder() {
return new LocalizedTextsBuilder();
}
public static class LocalizedTextsBuilder {
LocalizedTextsBuilder() {
}
public LocalizedTexts build() {
return new LocalizedTexts();
}
public String toString() {
return "LocalizedTexts.LocalizedTextsBuilder()";
}
}
}
当我尝试将 Discount 对象保存到数据库中时出现问题:
...
discountRepository.save(discount);
...
我已经检查过,属性转换器按预期工作(将 LocalizedTexts 转换为 JSON 字符串)。我得到的异常的完整堆栈跟踪如下:
org.springframework.orm.jpa.JpaSystemException: Could not set field value [{de=Rabatt 1, en=Discount 1}] value by reflection : [class net.xxxxx.yyyyy.svc_voucher.entity.Discount.designation] setter of net.xxxxx.yyyyy.svc_voucher.entity.Discount.designation; nested exception is org.hibernate.PropertyAccessException: Could not set field value [{de=Rabatt 1, en=Discount 1}] value by reflection : [class net.xxxxx.yyyyy.svc_voucher.entity.Discount.designation] setter of net.xxxxx.yyyyy.svc_voucher.entity.Discount.designation
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:331)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:233)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:551)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:152)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:174)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215)
at jdk.proxy2/jdk.proxy2.$Proxy116.save(Unknown Source)
at net.xxxxx.yyyyy.svc_voucher.messagehandler.DiscountHandler.handleCommand(DiscountHandler.java:58)
at net.xxxxx.yyyyy.svc_voucher.handlers.TestDiscountHandler.Sending_script_to_messageHandler_handleCommand(TestDiscountHandler.java:109)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: org.hibernate.PropertyAccessException: Could not set field value [{de=Rabatt 1, en=Discount 1}] value by reflection : [class net.xxxxx.yyyyy.svc_voucher.entity.Discount.designation] setter of net.xxxxx.yyyyy.svc_voucher.entity.Discount.designation
at org.hibernate.property.access.spi.SetterFieldImpl.set(SetterFieldImpl.java:72)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:681)
at org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:144)
at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:5252)
at org.hibernate.event.internal.DefaultMergeEventListener.copyValues(DefaultMergeEventListener.java:498)
at org.hibernate.event.internal.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:241)
at org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:318)
at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:172)
at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:70)
at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:107)
at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:829)
at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:816)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:311)
at jdk.proxy2/jdk.proxy2.$Proxy113.merge(Unknown Source)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:650)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.springframework.data.repository.core.support.RepositoryMethodInvoker$RepositoryFragmentMethodInvoker.lambda$new$0(RepositoryMethodInvoker.java:289)
at org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:137)
at org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:121)
at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:529)
at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:285)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:639)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:163)
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:138)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:80)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:388)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137)
... 42 more
Caused by: java.lang.IllegalArgumentException: Can not set final net.xxxxx.yyyyy.entity.internal.LocalizedTexts field net.xxxxx.yyyyy.svc_voucher.entity.Discount.designation to java.util.LinkedHashMap
at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at java.base/jdk.internal.reflect.UnsafeQualifiedObjectFieldAccessorImpl.set(UnsafeQualifiedObjectFieldAccessorImpl.java:83)
at java.base/java.lang.reflect.Field.set(Field.java:799)
at org.hibernate.property.access.spi.SetterFieldImpl.set(SetterFieldImpl.java:52)
... 81 more
最佳答案
起初,人们很容易相信 final
修饰符是问题的原因,但实际上不是。
让我们尝试这个简单的测试:
import java.lang.reflect.Field;
class Discount
{
private final Integer id = 0;
}
public class Test
{
public static void main(String[] args) throws IllegalAccessException, NoSuchFieldException
{
Discount d = new Discount();
Field f = Discount.class.getDeclaredField("id");
f.set(d, 1);
}
}
我们得到:
java.lang.IllegalAccessException: class Test cannot access a member of class Discount with modifiers "private final"
这不是您收到的错误(您的堆栈跟踪显示 IllegalArgumentException
,而不是 IllegalAccessException
)。
让我们更改代码以添加 setAccessible(true)
:
import java.lang.reflect.Field;
class Discount
{
private final Integer id = 0;
}
public class Test
{
public static void main(String[] args) throws IllegalAccessException, NoSuchFieldException
{
Discount d = new Discount();
Field f = Discount.class.getDeclaredField("id");
f.setAccessible(true);
f.set(d, 1);
}
}
这次成功了,没有抛出异常。
现在让我们替换 1
与 "1"
:
import java.lang.reflect.Field;
class Discount
{
private final Integer id = 0;
}
public class Test
{
public static void main(String[] args) throws IllegalAccessException, NoSuchFieldException
{
Discount d = new Discount();
Field f = Discount.class.getDeclaredField("id");
f.setAccessible(true);
f.set(d, "1");
}
}
我们得到:
java.lang.IllegalArgumentException: Can not set final java.lang.Integer field Discount.id to java.lang.String
这次和你的错误完全一样。 IllegalArgumentException
意味着我们正在尝试分配一个不兼容的值(此处为 String
到 Integer
)。
您的堆栈跟踪表明您正在尝试分配 LinkedHashMap
到designation
字段,这确实不兼容( designation
是 LocalizedTexts
)。
了解LinkedHashMap
在哪里来自,我们来看看你的JpaJsonConverter
。负责将 JSON 字符串转换为 LocalizedTexts
的方法对象是:
public Object convertToEntityAttribute(String localizedTextsJSON) {
Object localizedTexts = null;
try {
localizedTexts = objectMapper.readValue(localizedTextsJSON, Object.class);
} catch (IOException ex) {
//throw new RuntimeException();
}
return localizedTexts;
}
您正在调用 readValue()
包含一个对象的 JSON 字符串,以及 Object.class
作为第二个参数。在这种情况下, jackson 返回 LinkedHashMap
。它无法知道您想要 LocalizedTexts
.
以下是修复该方法的可能方法:
@SuppressWarnings("unchecked")
public Object convertToEntityAttribute(String localizedTextsJSON) {
LocalizedTexts localizedTexts = new LocalizedTexts();
try {
Map<String,String> map = (Map<String,String>)objectMapper.readValue(localizedTextsJSON, Object.class);
for(Map.Entry<String,String> e : map.entrySet())
localizedTexts.put(Language.valueOf(e.getKey()), e.getValue());
} catch (IOException ex) {
throw new RuntimeException(ex);
}
return localizedTexts;
}
关于java - 无法将自定义类型作为 json 字符串存储到数据库中 - java.lang.IllegalArgumentException : Can not set final field to java. util.LinkedHashMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72542192/
关闭。这个问题需要更多 focused .它目前不接受答案。 想要改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 2 年前。 Improve this q
1.final final修饰类,说明这个类不能被继承,是以个顶级类。 final修饰变量,说明这个变量是常量。 final修饰方法,表示这个方法不能被重写,不过可以冲在final方法。 比如有个
我喜欢使用 -Wsuggest-final-types 编译我的代码和 -Wsuggest-final-methods以便在可能使用 final 关键字以允许编译器更积极地优化的机会时收到警告。 不过
我在 Java 8 中有一个异步操作,它返回一个 onError 回调或一个 onSuccess 回调。如果操作成功与否,我需要在我的方法内部返回。所以我返回一个 boolean 值来说明这个信息。我
我正在阅读一些内容,需要对最终类和方法进行一些说明。我的理解是,将一个类声明为 final 会阻止该类被扩展。因此,是否有必要将最终类中的方法声明为最终的?在我看来,如果类不能扩展,则没有必要将方法声
有什么区别 try { // action A } catch(Exception e) { // action B } finally { // action C }
这个程序是我类(class)的最终作业,我无法弄清楚为什么我收到错误“从内部类引用的局部变量必须是最终的或有效的最终”。该程序正在运行并发线程来对# 数组进行排序,然后找到该数组的高值和低值。当我在没
C++11 added final. 终于! 我了解 final 做了两件事: 使类不可继承。 使类中的(虚拟)函数不可覆盖(在派生类中)。 这两者似乎是相互独立的。但以以下为例: class Foo
我想使用具有多个提交按钮的react-final-form构建表单,其中每个提交按钮在表单中设置不同的值。本质上,我想创建一个在呈现的HTML中看起来像这样的表单: Are you over 1
我想知道什么时候应该对变量和(或)方法使用静态、最终、静态最终参数。据我了解: final:类似于c++中的const参数。它基本上意味着值(或在方法中 - 返回值)不会改变。 静态:这意味着值(或方
我正在做一个编程类(class)项目,用于 400 行矩阵本身的矩阵乘法。我让它以顺序模式工作,该项目的目标是编写并行实现。 我有以下代码,当然,当我尝试引用内部类中的计数器 j 时,我收到一个关于
也许这是简单的问题,但我找不到答案。 声明为final的经典变量是否包含与非final变量不同的内存段? 最佳答案 我想说,谈到局部变量,基于 this answer 是不行的。 : The trut
考虑以下代码: #include class B { virtual void f() { std::cout << "Base" << '\n'; } }; class D
这个问题已经有答案了: java: Is it possible to set a lambda expression for an array of Buttons is a for loop? I
考虑这个代码片段 public class ConstantFolding { static final int number1 = 5; static final int numbe
我确定在内部类中访问的变量应该声明为final 或有效final。但在下面的情况下我很困惑。不知道是不是我理解错了。 public class MyClass { private boolea
我必须将一个 java.sql.Connection 对象传递给一个匿名内部类,这意味着我必须对它进行 final 引用。但是,我担心任何资源泄漏。 public static String foo(
我收到 SonarQube 错误:“强烈建议在此方法实现结束时调用 super.finalize(),以防父实现也必须释放一些系统资源。” 但我发现 Object 类没有实现 finalize方法。
我一般认为资源清理是在 finally block 中完成的, 最近我在一个类中发现了这个特定的代码片段,它覆盖了 Object 类'finalize()方法。 protected void fina
让我们在父类中使用这个方法: public void calculateSum(int a, final int b) { } 子类有: public void calculateSum(int a,
我是一名优秀的程序员,十分优秀!