0

I have a problem with Spring and JPA. Basically I try to use JPA with Spring with a pure approach, or better, without explicit references in the code to Spring framework with the exception of the @Transactional. So I wanted to know where wrong. My persistence.xml is:

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

My applicationContext is:

<?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>

my DAO interface is:

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

public interface PluginDao {

    public Plugin load (int id);

    public void save(Plugin plg);
}

and my implementation is:

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

This is my "horror":

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
giulius
  • 409
  • 3
  • 11
  • 25

2 Answers2

1

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

This means that in your entity you have an annotation like this

@OneToMany(mappedBy="something")

In this case, "something" has to be the name of the relevant field of the other entity.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • you are so quick to post the answer ;) – Kowser Jul 12 '11 at 09:38
  • Thanks, perhaps from exhaustion did not read the horror: P Now that we have, can I ask if I did everything right? That is, in any case however I need to define a class attribute as an EntityManager? – giulius Jul 12 '11 at 10:01
  • Friends, I posted the code that does not commit when calling the save method, why? – giulius Jul 12 '11 at 10:45
  • 1
    @giulius a) please post a new question b) you should probably not mark dao methods `@Transactional`. Transactions should be started in the service layer, not the dao layer. (see [this question](http://stackoverflow.com/q/1079114/342852) and many others) – Sean Patrick Floyd Jul 12 '11 at 11:04
0

You have only put the it.synclab.fb.jpa.entity.Plugin entity in your persistence.xml, but this entity has an association to another entity (it.synclab.fb.jpa.entity.StoreFileGet), which is not listed. All the entities must be listed.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255