Karaf 4.0.0 M2, Hibernate 4.3.6, and org.hibernate.PropertyA



我在以下url遵循Christian Schneider的Karaf/Database教程:http://www.liquid-reality.de/display/liquid/2012/01/13/Apache + Karaf +教程+ + 6 + - +数据库+部分访问

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

我有一些具体的问题:

  1. 我需要休眠吗?属性文件,因为persistence.xml文件引用数据源并允许设置hibernate属性
  2. 是否有额外的依赖我需要在我的pom.xml文件
  3. 是否存在与pom.xml或hibernate无关的其他原因?属性导致如下所示的异常?

提前感谢任何建议。兰迪

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>

位于

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;
    }
}

pom.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实现。

我已经将这篇原始文章中列出的代码部署到Apache Karaf 3.0.3实例中,代码运行良好。这个问题是Apache Karaf 4.0.0 M2,更具体地说,是Apache Aries v1.0.4。

如前所述,白羊座1.0.4在卡拉夫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现在在blueprint.xml文件中声明工件时存在问题。

Christian Schneider写了一篇文章,指出白羊座现在是如何使用注解来声明上下文的:http://www.liquid-reality.de/display/liquid/New +设计+ +白羊座+ jpa

我还没有对这些注释进行实验,但将留给其他人来确认白羊斯1.0.4(因此是Karaf 4.0.0)不再支持在blueprint.xml文件中声明上下文。

相关内容

  • 没有找到相关文章

最新更新