- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我使用 spring-boot-1.5.3 和 spring-data-jpa,有两个实体,它们在 cloumn tags
和 nicknames
上都有 JPA Annoatation ElementCollection
,Hibernate 会根据实体自动生成表。参见Job.java
表的默认CollectionTable是主键job_id
,但我更改为job_name
,集合tags
可以为空。那就是异常发生的时候。
名为 Job2 的作业没有任何标签,这意味着 Settags 为空。当我使用 jobDao.findOne(3); 获取 Job2 时,Hibernate 抛出异常org.hibernate.property.access.spi.PropertyAccessException
引起:java.lang.IllegalArgumentException:无法将 java.lang.String 字段 hello.entity.Job.name 设置为 java.lang.Integer
当我将 @ElementCollection(fetch=FetchType.EAGER) 更改为 @ElementCollection(fetch = FetchType.LAZY) 时,一切顺利。但我不知道为什么当一个实体的 Collection 列中没有元素时 FetchType.EAGER 会导致异常。并且,即使 hibernate 应该知道使用 name 而不是 id,它仍然尝试将 job.name 转换为 job.id完整的堆栈跟踪是
org.springframework.orm.jpa.JpaSystemException: Error accessing field [private java.lang.String hello.entity.Job.name] by reflection for persistent property [hello.entity.Job#name] : 3; nested exception is org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private java.lang.String hello.entity.Job.name] by reflection for persistent property [hello.entity.Job#name] : 3
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:333)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:244)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:488)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:57)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy115.findOne(Unknown Source)
at hello.ApplicationTests.testJob2(ApplicationTests.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
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:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
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.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private java.lang.String hello.entity.Job.name] by reflection for persistent property [hello.entity.Job#name] : 3
at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:43)
at org.hibernate.tuple.component.AbstractComponentTuplizer.getPropertyValue(AbstractComponentTuplizer.java:58)
at org.hibernate.type.ComponentType.getPropertyValue(ComponentType.java:419)
at org.hibernate.type.ComponentType.getHashCode(ComponentType.java:242)
at org.hibernate.engine.spi.CollectionKey.generateHashCode(CollectionKey.java:64)
at org.hibernate.engine.spi.CollectionKey.<init>(CollectionKey.java:58)
at org.hibernate.engine.spi.CollectionKey.<init>(CollectionKey.java:43)
at org.hibernate.engine.loading.internal.CollectionLoadContext.getLoadingCollection(CollectionLoadContext.java:95)
at org.hibernate.loader.plan.exec.process.internal.CollectionReferenceInitializerImpl.finishUpRow(CollectionReferenceInitializerImpl.java:105)
at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.readRow(AbstractRowReader.java:121)
at org.hibernate.loader.plan.exec.internal.EntityLoadQueryDetails$EntityLoaderRowReader.readRow(EntityLoadQueryDetails.java:239)
at org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:122)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:122)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:86)
at org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader.load(AbstractLoadPlanBasedEntityLoader.java:167)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:4019)
at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:508)
at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:478)
at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:219)
at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:278)
at org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:121)
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:89)
at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1129)
at org.hibernate.internal.SessionImpl.access$2600(SessionImpl.java:164)
at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.load(SessionImpl.java:2696)
at org.hibernate.internal.SessionImpl.get(SessionImpl.java:975)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:1075)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:1039)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:298)
at com.sun.proxy.$Proxy101.find(Unknown Source)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findOne(SimpleJpaRepository.java:241)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:504)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:489)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:461)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
... 39 more
Caused by: java.lang.IllegalArgumentException: Can not set java.lang.String field hello.entity.Job.name to java.lang.Integer
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36)
at java.lang.reflect.Field.get(Field.java:393)
at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:39)
... 88 more
mysql> select * from employee;
+----+------+
| id | name |
+----+------+
| 2 | E1 |
| 3 | E2 |
+----+------+
2 rows in set
mysql> select * from employee_nick_names;
+-------------+------------+
| employee_id | nick_names |
+-------------+------------+
| 2 | E1A |
| 2 | E1B |
+-------------+------------+
2 rows in set
mysql> select * from job;
+----+------+
| id | name |
+----+------+
| 2 | Job1 |
| 3 | Job2 |
+----+------+
2 rows in set
mysql> select * from job_tags;
+----------+---------+
| job_name | job_tag |
+----------+---------+
| Job1 | Tag1 |
| Job1 | Tag2 |
+----------+---------+
2 rows in set
作业.java
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Job implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Integer id;
private String name;
@ElementCollection(fetch=FetchType.EAGER)
// @ElementCollection(fetch = FetchType.LAZY)
@CollectionTable(name = "job_tags", joinColumns = @JoinColumn(name = "job_name", referencedColumnName = "name"))
@Column(name = "job_tag")
private Set<String> tags = new HashSet<String>();
}
员工.java
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Employee implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Integer id;
private String name;
@ElementCollection(fetch=FetchType.EAGER)
// @ElementCollection(fetch = FetchType.LAZY)
private Set<String> nickNames = new HashSet<String>();
}
JobDao.java
public interface JobDao extends JpaRepository<Job, Integer> {}
EmployeeDao.java
public interface EmployeeDao extends JpaRepository<Employee, Integer> {}
ApplocationTest.java
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {
@Autowired
JobDao jobDao;
@Autowired
EmployeeDao employeeDao;
@Test
public void testEmployee() {
employeeDao.findAll().forEach(e -> employeeDao.delete(e));
Employee e1 = new Employee();
e1.setName("E1");
e1.setNickNames(Sets.newSet("E1A", "E1B"));
employeeDao.save(e1);
Employee e2 = new Employee();
e2.setName("E2");
employeeDao.save(e2);
}
@Test
@Transactional
public void testEmployee2() {
Employee employee1 = employeeDao.findOne(2);
System.out.println(employee2);
Employee employee2 = employeeDao.findOne(3);
System.out.println(employee3);
}
@Test
public void testJob() {
jobDao.findAll().forEach(e -> jobDao.delete(e));
Job job1 = new Job();
job1.setName("Job1");
job1.setTags(Sets.newSet("Tag1", "Tag2"));
jobDao.save(job1);
Job job2 = new Job();
job2.setName("Job2");
jobDao.save(job2);
}
@Test
@Transactional
public void testJob2() {
Job job1 = jobDao.findOne(2);
System.out.println(job2);
Job job2 = jobDao.findOne(3);
System.out.println(job3);
}
}
最佳答案
当您查询职位时
渴望
hibernate将立即获取“job_tags”
并获取名称字段的值,
但是name字段是私有(private)的==>访问被拒绝,您可以尝试将name字段更改为公开
使用 LAZY
hibernate获取作业表单数据库(并且不获取“job_tags”)
当你隐式调用 getter getJobTags 时,job_tags 将被加载
关于java - Hibernate ElementCollection(fetch=FetchType.EAGER) 导致 PropertyAccessException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45504495/
显然,可以实现 Haskell,使其在不改变语言语义的情况下急切地求值。如果这是真的,如何处理无限数据结构? http://csg.csail.mit.edu/pubs/haskell.html Th
有没有什么方法可以像@ManagedBean(eager=true ) 来自 javax.faces 包? @Named @ApplicationScoped public Mail() { ...
使用@ApplicationScoped @Named @Eager,我的@EJB注入(inject)的@Stateless bean未正确实例化并评估为空。 我有一个@ApplicationScop
我必须删除两个分隔符之间的字符串,即 从“123XabcX321”我想要“123321”。 对于一个简单的案例,我很好: $_=<>; s/X(.*)X//; print; 但是,如果像“123Xab
我有一个对象层次结构,订单,联系人,地址: public class Order { public virtual Contact BillingContact { get; set; }
Eager 模式仅支持 TF native 优化器我在以下尝试过的每个优化器中都会遇到此错误: def create_model(): model = tf.keras.models.Sequ
我有这样的数据设计:交易属于授权,而授权又属于客户。或者用结构体表示: type Transaction struct { shared.Transaction // transaction
我想在 Scheme 中做一个懒惰的列表。这是我到目前为止。 ;; Constructor for Pairs (define (cons-stream a b) (cons a (λ() b))
今天是个好日子 我是 Laravel 的新手,目前正在用它构建我的第一个简单的新闻应用程序 我有一个简单的查询,使用 laravel 的预先加载功能 但是在这个急切的加载功能中,我想根据某些条件获取某
我正在制作一个动态表单,让用户使用 vue.js 自动生成带有游戏统计信息的图像,并使用 Keen-ui。我是 vue.js 的新手,过去两周开始学习它。 现在,我在使用 eager-ui 进行选择下
我 fork 了 eager/dashboards github 存储库,并尝试创建一个 Dockerfile 以在 Docker 容器中运行仪表板。 我的 fork :https://github.
我的实体: @Entity @NoArgsConstructor public class Company { @Id @Getter @GeneratedValue(strategy =
在我的实体中,我必须使用 fetch = FetchType.EAGER 因为我有一个错误: nested exception is org.hibernate.LazyInitializationE
我在 Java 中使用 play 2.0.4。我正在尝试从 play 提供的内存数据库中保存和检索值(Ebean 是底层 JPA 实现)。下面是一些示例代码: @NoobDisclaimer ("I'
在 Pro .NET Performance - Optimize Your C# Applications 的第 96 页上,它谈到了 GC 急切根收集: For each local variab
我无法从数据库加载对象及其相对映射对象。可能我的“ map 系统”是错误的。 在数据库中保存对象(及其相对映射对象)时,一切正常,因此对象似乎已正确映射。 Utente是我的“主”对象 publ
我需要使用急切查询在 Laravel 中执行子查询,但结果始终为空。 查询: $project = Log::where('project_id', $id)->with(['log_occurenc
我有这个程序: var planetStream = require('../../'); var app = require('http').createServer(handler); var i
我需要与FetchType.EAGER(下面的示例)建立关系,但我有异常(exception)。如果我将 FetchType.EAGER 更改为 FetchType.LAZY 一切正常,但我确实需要
我正在使用 EF6 并尝试急切获取对象的整个结构。问题是我正在使用继承。 假设我有这个类(class)。 数据库上下文 DbSet A { get; set; } 示例类 public class A
我是一名优秀的程序员,十分优秀!