当前位置: 首页 > 知识库问答 >
问题:

Karaf 4.0.0 M2、Hibernate 4.3.6 和 org.hibernate.PropertyAccessException

满昊然
2023-03-14

我正在以下网址关注Christian Schneider的Karaf /Database教程:http://www.liquid-reality.de/display/liquid/2012/01/13/Apache Karaf教程第6部分 - 数据库访问

我对本教程的更改是使用MySql和Hibernate而不是H2和OpenJpa。话虽如此,我让我的自定义捆绑包加载,但Hibernate无法使用反射来访问我的实体类中的属性。我得到的堆栈跟踪如下。

我有一些具体问题:

  1. 我是否需要一个hibernate.properties文件,因为持久性.xml文件引用数据源并允许设置hibernate属性
  2. 我的 pom 文件中是否有我需要的其他依赖项.xml
  3. 是否还有其他原因与 pom.xml 或 hibernate.properties 无关,从而导致下面显示的异常?

提前感谢您的任何建议。兰迪

2015-06-05 16:40:04,088 | ERROR | nsole user karaf | ShellUtil                        | 41 - org.apache.karaf.shell.core - 4.0.0.M2 | Exception caught while executing command
java.lang.reflect.UndeclaredThrowableException
    at com.sun.proxy.$Proxy72.persist(Unknown Source)[146:org.apache.aries.jpa.container.context:1.0.4]
    at net.lr.tutorial.karaf.db.examplejpa.impl.PersonServiceImpl.add(PersonServiceImpl.java:23)[167:leonard-orm-native:1.0.0]
    at Proxyead5dc4a_ed6c_46e9_8aad_e93af2278046.add(Unknown Source)[:]
    at net.lr.tutorial.karaf.db.examplejpa.command.AddPersonCommand.execute(AddPersonCommand.java:42)[167:leonard-orm-native:1.0.0]
    at org.apache.karaf.shell.commands.basic.AbstractCommand.execute(AbstractCommand.java:34)[41:org.apache.karaf.shell.core:4.0.0.M2]
    at org.apache.karaf.shell.compat.CommandTracker$1.execute(CommandTracker.java:109)[41:org.apache.karaf.shell.core:4.0.0.M2]
    at org.apache.karaf.shell.impl.console.osgi.secured.SecuredCommand.execute(SecuredCommand.java:67)[41:org.apache.karaf.shell.core:4.0.0.M2]
    at org.apache.karaf.shell.impl.console.osgi.secured.SecuredCommand.execute(SecuredCommand.java:87)[41:org.apache.karaf.shell.core:4.0.0.M2]
    at org.apache.felix.gogo.runtime.Closure.executeCmd(Closure.java:480)[41:org.apache.karaf.shell.core:4.0.0.M2]
    at org.apache.felix.gogo.runtime.Closure.executeStatement(Closure.java:406)[41:org.apache.karaf.shell.core:4.0.0.M2]
    at org.apache.felix.gogo.runtime.Pipe.run(Pipe.java:108)[41:org.apache.karaf.shell.core:4.0.0.M2]
    at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:182)[41:org.apache.karaf.shell.core:4.0.0.M2]
    at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:119)[41:org.apache.karaf.shell.core:4.0.0.M2]
    at org.apache.felix.gogo.runtime.CommandSessionImpl.execute(CommandSessionImpl.java:94)[41:org.apache.karaf.shell.core:4.0.0.M2]
    at org.apache.karaf.shell.impl.console.ConsoleSessionImpl.run(ConsoleSessionImpl.java:267)[41:org.apache.karaf.shell.core:4.0.0.M2]
    at java.lang.Thread.run(Thread.java:745)[:1.7.0_60-ea]
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)[:1.7.0_60-ea]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)[:1.7.0_60-ea]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)[:1.7.0_60-ea]
    at java.lang.reflect.Method.invoke(Method.java:606)[:1.7.0_60-ea]
    at org.apache.aries.jpa.container.context.transaction.impl.JTAEntityManagerHandler.invoke(JTAEntityManagerHandler.java:185)[146:org.apache.aries.jpa.container.context:1.0.4]
    ... 16 more
Caused by: java.lang.reflect.UndeclaredThrowableException
    at com.sun.proxy.$Proxy76.persist(Unknown Source)
    ... 21 more
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)[:1.7.0_60-ea]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)[:1.7.0_60-ea]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)[:1.7.0_60-ea]
    at java.lang.reflect.Method.invoke(Method.java:606)[:1.7.0_60-ea]
    at org.apache.aries.jpa.container.impl.EntityManagerProxyFactory$EMHandler.invoke(EntityManagerProxyFactory.java:31)
    ... 22 more
Caused by: javax.persistence.PersistenceException: org.hibernate.PropertyAccessException: could not get a field value by reflection getter of net.lr.tutorial.karaf.db.examplejpa.Person.name
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1763)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1683)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1187)
    ... 27 more
Caused by: org.hibernate.PropertyAccessException: could not get a field value by reflection getter of net.lr.tutorial.karaf.db.examplejpa.Person.name
    at org.hibernate.property.DirectPropertyAccessor$DirectGetter.get(DirectPropertyAccessor.java:60)
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:346)
    at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:4746)
    at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:4465)
    at org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:243)
    at org.hibernate.event.internal.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:511)
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:116)
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:75)
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:811)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:784)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:789)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1181)
    ... 27 more
Caused by: java.lang.IllegalArgumentException: Can not set java.lang.String field net.lr.tutorial.karaf.db.examplejpa.Person.name to net.lr.tutorial.karaf.db.examplejpa.Person
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:164)[:1.7.0_60-ea]
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:168)[:1.7.0_60-ea]
    at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:55)[:1.7.0_60-ea]
    at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36)[:1.7.0_60-ea]
    at java.lang.reflect.Field.get(Field.java:379)[:1.7.0_60-ea]
    at org.hibernate.property.DirectPropertyAccessor$DirectGetter.get(DirectPropertyAccessor.java:57)
    ... 38 more

blueprint.xml

xsi:schemaLocation="
        http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0 
        http://www.w3.org/2001/XMLSchema-instance http://www.w3.org/2001/XMLSchema-instance 
        http://aries.apache.org/xmlns/jpa/v1.0.0 http://aries.apache.org/xmlns/jpa/v1.0.0 
        http://aries.apache.org/xmlns/transactions/v1.0.0 http://aries.apache.org/xmlns/transactions/v1.0.0 ">

<bean id="personService" class="net.lr.tutorial.karaf.db.examplejpa.impl.PersonServiceImpl">
    <jpa:context unitname="Leonard" property="entityManager" />
    <tx:transaction method="*" value="Required" />
</bean>

<service ref="personService" interface="net.lr.tutorial.karaf.db.examplejpa.PersonService" />

<command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.0.0">
    <command name="person/add">
        <action class="net.lr.tutorial.karaf.db.examplejpa.command.AddPersonCommand">
            <property name="personService" ref="personService" />
        </action>
    </command>
    <command name="person/list">
        <action class="net.lr.tutorial.karaf.db.examplejpa.command.ListPersonsCommand">
            <property name="personService" ref="personService" />
        </action>
    </command>
    <command name="person/deleteAll">
        <action class="net.lr.tutorial.karaf.db.examplejpa.command.DeleteAllPersonsCommand">
            <property name="personService" ref="personService" />
        </action>
    </command>
</command-bundle>

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="Leonard" transaction-type="JTA">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <jta-data-source>osgi:service/Leonard</jta-data-source>
        <class>net.lr.tutorial.karaf.db.examplejpa.Person</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
        </properties>
    </persistence-unit>
</persistence>

人.java

package net.lr.tutorial.karaf.db.examplejpa;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "Person")
public class Person
{
    private String name;
    private String twitterName;

    public Person()
    {
    }

    public Person(String name, String twitterName)
    {
        super();
        this.name = name;
        this.twitterName = twitterName;
    }

    @Id
    @Column(name = "name", unique = true)
    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    @Column(name = "twitterName")
    public String getTwitterName()
    {
        return twitterName;
    }

    public void setTwitterName(String twitterName)
    {
        this.twitterName = twitterName;
    }
}

啪.xml

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>info.leonard.orm</groupId>
    <artifactId>leonard-orm-native</artifactId>
    <version>1.0.0</version>
    <packaging>bundle</packaging>

    <properties>
        <log4j-version>1.2.16</log4j-version>
        <slf4j-version>1.6.1</slf4j-version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.apache.karaf.shell</groupId>
            <artifactId>org.apache.karaf.shell.console</artifactId>
            <version>4.0.0.M2</version>
        </dependency>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.core</artifactId>
            <version>4.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.enterprise</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j-version}</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-core</artifactId>
            <version>4.3.6.Final</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-osgi</artifactId>
            <version>4.3.6.Final</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.3.6.Final</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate.common</groupId>
            <artifactId>hibernate-commons-annotations</artifactId>
            <version>4.0.4.Final</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.35</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.7</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j-version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j-version}</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>2.5.3</version>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
                        <Meta-Persistence>META-INF/persistence.xml</Meta-Persistence>
                        <Import-Package>
                            org.apache.felix.service.command;version="[0.6,1)",
                            org.apache.felix.gogo.commands;version="[0.6,1)",
                            org.apache.karaf.shell.console;version="[2.2,4)",
                            *,
                            org.hibernate.jpa,
                            org.hibernate.proxy,
                            javassist.util.proxy
                        </Import-Package>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

PersonServiceImpl.java

package net.lr.tutorial.karaf.db.examplejpa.impl;

import java.util.List;
import javax.persistence.EntityManager;
import net.lr.tutorial.karaf.db.examplejpa.Person;
import net.lr.tutorial.karaf.db.examplejpa.PersonService;

public class PersonServiceImpl implements PersonService
{    
    private EntityManager em;

    public void setEntityManager(EntityManager em)    
    {
        this.em = em;
    }

    public void add(Person person)
    {
        em.persist(person);
        em.flush();
    }

    public void deleteAll()
    {
        em.createQuery("delete from Person").executeUpdate();
        em.flush();
    }

    public List<Person> getAll()
    {
        return em.createQuery("select p from Person p", Person.class).getResultList();
    }
}

我已经重新启动了我的karaf实例,并查看了启动时的日志。以下项目引起了我的注意:

2015-06-06 04:17:02,988 | WARN  | FelixStartLevel  | aries                            | 144 - org.apache.aries.jpa.blueprint.aries - 1.0.4 | Managed persistence context support is no longer available for use with the Aries Blueprint container.

注意,我的例子是使用容器管理的JPA。看起来很奇怪,这实际上是不受支持的,但将在周末尝试一个非托管的JPA实现。

共有1个答案

都博裕
2023-03-14

我已经将本文中列出的代码部署到Apache Karaf 3.0.3实例中,代码运行良好。这个问题与Apache Karaf 4.0.0 M2有关,更具体地说是与Apache Aries 1 . 0 . 4有关

如前所述,白羊座1.0.4在Karaf 4.0.0 M2中启动时会生成以下警告

2015-06-06 04:17:02,988 | WARN  | FelixStartLevel  | aries                            | 144 - org.apache.aries.jpa.blueprint.aries - 1.0.4 | Managed persistence context support is no longer available for use with the Aries Blueprint container.

此警告表示Aries现在在蓝图中声明工件时存在问题。xml文件。

克里斯蒂安·施耐德写了一篇文章,指出白羊座现在如何使用注释来声明上下文:白羊座的http://www.liquid-reality.de/display/liquid/New设计

我还没有尝试过这些注释,但会留给其他人来确认白羊座1.0.4(因此Karaf 4.0.0)不再支持在蓝图.xml文件中声明上下文。

 类似资料:
  • 问题内容: 关于它们有很多传说。我想知道真相。以下两个示例之间有什么区别? 问题答案: 不确定从何处获得传说,但: 提交按钮 与: IE6将在标记之间提交此按钮的所有文本,其他浏览器将仅提交值。使用可使您在按钮的设计上享有更大的布局自由度。从各种意图和目的看,它乍一看似乎很棒,但是各种浏览器怪癖使它有时很难使用。 在您的示例中,IE6将发送到服务器,而其他大多数浏览器将不发送任何内容。要使其跨浏览

  • 什么区别以及如何正确重写代码?

  • 我试图理解为什么下面两个代码块会产生不同的结果。 代码块1按预期工作,并返回从数据库中查找的提供程序的数组。另一方面,代码块2返回函数数组。在理解promissione.all()和async/await时,我觉得缺少了一些简单的东西。 代码块的差异如下: > 块1:创建许诺函数数组,然后使用map运算符将其包装在异步函数中。 块2:许诺函数的数组被创建为异步函数。因此,不调用map运算符。 如果

  • 问题内容: 我才刚刚开始研究SQL。 我有一个SQL Server 2008r2数据库,它将返回两个字段DocDate和InvValue。我需要将InvValues汇总为今天的MTD和YTD,所以看起来像 我已经做了大量的Google搜寻,并且可以使用SUM&DATEPART进行一项或多项,但是我坚持尝试两者兼而有之。 有人可以给我一些伪代码,以帮助我进一步谷歌。 谢谢@戈登·利诺夫(Gordon

  • 这个示例代码来自一本Java书籍,从墙上的99瓶啤酒到没有啤酒打印出这首歌。问题是,当墙上是1瓶啤酒时,它仍然写着瓶子。我试图通过在末尾添加部分来解决这个问题。但是,它仍然显示1瓶啤酒在墙上,我瓶啤酒在墙上。 我不知道该改变什么来解决这个问题。是否创建另一个while部分? 如果你能给他们一个提示,这样我就可以自己解决它,那也很酷!因为我知道我的实际歌曲输出是在第一个if部分,但我不知道我应该在哪

  • 原文地址:https://cesiumjs.org/tutorials/cesium-and-webpack/ Cesium 和 Webpack Webpack是非常强大非常流行的JavaScript 模块打包工具。它可以让开发人员以一种简单直观的 require 方式去加载各种页面需要的文件,极大的方便了开源人员对代码和资源文件进行结构化设计。当编译的时候,它会跟踪代码依赖性,把所有的模型打包到

  • 问题内容: 我一直在学习redis和node.js。我有两个问题,找不到令人满意的答案。 我的第一个问题是关于在node.js中重用Redis客户端。我找到了这个问题和答案:如何在socket.io中重用Redis连接,但还不足以让我满意。 现在,如果我在连接事件中创建redis客户端,它将为每个连接生成。因此,如果我有2万个并发用户,那么将有2万个Redis客户端。 如果我将其放在连接事件之外,

  • 我需要控制日志框架的最大磁盘空间量。 例如,在log4j中,如果有如下所示的追加器,我可以很容易地估计需要多少磁盘空间: 对于maxFileSize和maxBackupIndex,我知道最多需要10x100KB。 我注意到DailyRollingFileAppender,但它不支持maxFileSize。 除了RollingFileAppender之外,是否还有其他fileappender可以实现