0

我对 Spring 和 JPA 有疑问。基本上,我尝试以纯方法或更好的方式将 JPA 与 Spring 结合使用,而不在代码中明确引用 Spring 框架,@Transactional 除外。所以我想知道哪里错了。我的 persistence.xml 是:

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0">
    <persistence-unit name="fb-persistence" transaction-type="RESOURCE_LOCAL">  
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>it.synclab.fb.jpa.entity.Plugin</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
            <!-- da utilizzare solo in caso di creazione dello schema <property name="hibernate.hbm2ddl.auto" value="create-drop"/>-->
            <property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/>  
            <property name="hibernate.show_sql" value="true"/>  
            <property name="hibernate.connection.username" value="flussibatch"/>  
            <property name="hibernate.connection.password" value="caposele"/>  
            <property name="hibernate.connection.url" value="jdbc:oracle:thin:@localhost:1521:XE"/> 
        </properties>
   </persistence-unit>
</persistence>

我的应用程序上下文是:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="fb-persistence" />
    </bean>

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
    <bean name="pluginDao" class="it.synclab.fb.jpa.dao.impl.PluginDaoImpl" />
</beans>

我的 DAO 界面是:

import it.synclab.fb.jpa.entity.Plugin;

public interface PluginDao {

    public Plugin load (int id);

    public void save(Plugin plg);
}

我的实现是:

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.transaction.annotation.Transactional;
import it.synclab.fb.jpa.entity.Plugin;


public class PluginDaoImpl implements PluginDao {

    private EntityManager entityManager;

    @PersistenceContext (unitName="fb-persistence")
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    @Override
    public Plugin load(int id) {
        return entityManager.find(Plugin.class, id);
    }

    @Override
    @Transactional
    public void save(Plugin plg) {
        entityManager.persist(plg);     
    }
}

这是我的“恐惧”:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: fb-persistence] Unable to configure EntityManagerFactory
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1420)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:563)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at it.synclab.fb.jpa.test.PluginTest.main(PluginTest.java:26)
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: fb-persistence] Unable to configure EntityManagerFactory
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:378)
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:56)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48)
    at org.springframework.orm.jpa.LocalEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalEntityManagerFactoryBean.java:92)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:308)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
    ... 12 more
Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: it.synclab.fb.jpa.entity.StoreFileGet.idTransaction in it.synclab.fb.jpa.entity.Transaction.listStoreFileGet
    at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:685)
    at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:645)
    at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:65)
    at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1689)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1396)
    at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1348)
    at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1522)
    at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:193)
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:1100)
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:282)
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:366)
    ... 18 more
4

2 回答 2

1

引起:org.hibernate.AnnotationException: mappedBy 引用了一个未知的目标实体属性:it.synclab.fb.jpa.entity.StoreFileGet.idTransaction in it.synclab.fb.jpa.entity.Transaction.listStoreFileGet

这意味着在您的实体中,您有这样的注释

@OneToMany(mappedBy="something")

在这种情况下,“某事”必须是另一个实体的相关字段的名称。

于 2011-07-12T09:34:21.687 回答
0

您只将该it.synclab.fb.jpa.entity.Plugin实体放在了您的 persistence.xml 中,但该实体与另一个实体 ( it.synclab.fb.jpa.entity.StoreFileGet) 有关联,该实体未列出。必须列出所有实体。

于 2011-07-12T09:32:31.590 回答