Announcement Announcement Module
Collapse
No announcement yet.
simple problem spring/jpa: entity not stored in database Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • simple problem spring/jpa: entity not stored in database

    Hello,

    I'm trying a very simple exercise with JPA and Spring.
    I use OpenJPA and HSQLDB.

    However, for some reason the code below compiles and executes, but nothing is inserted in the database.
    Someone sees what I'm doing wrong or what I forget???

    FYI: I started this exercise, based on this article. Apparently it works with them, but not with me:
    http://blog.springsource.com/2006/08...encing-spring/

    Thank you!



    PHP Code:
    package src;

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.FileSystemXmlApplicationContext;

    public class 
    JpaMain {
        public static 
    void main(String[] args) {
            
    Message Message = new Message();
            
    Message.setMsg("hello world");
            
    Message.setId(1);
            
    ApplicationContext context = new FileSystemXmlApplicationContext(".\\conf\\config-ex4.xml");
            
    MessageDao pMessageDao = (MessageDaocontext.getBean("MessageDao");
            
    pMessageDao.updateTable(Message);
        }

    PHP Code:
    package src;

    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;

    @
    Entity
    public class Message {

        private 
    int id;
        private 
    String message;

        public 
    String getMessage() {
            return 
    message;
        }

        public 
    void setMessage(String message) {
            
    this.message message;
        }

        public 
    int getId() {
            return 
    id;
        }

        public 
    void setId(int id) {
            
    this.id id;
        }


    PHP Code:
    package src;

    public interface 
    MessageDao {
        
    void updateTable(Message Message);

    PHP Code:
    package src;

    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;

    import org.springframework.stereotype.Repository;

    @
    Repository
    public class MessageDaoImpl implements MessageDao {
        @
    PersistenceContext
        
    private EntityManager entityManager;
        
        public 
    void updateTable(Message Message) {
            
    entityManager.persist(Message);
            
    System.out.println("ok");
        }
        



    PHP Code:
    <?xml version="1.0"?>
    <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 persistence_1_0.xsd" version="1.0">
      <persistence-unit name="msg" transaction-type="RESOURCE_LOCAL">
        <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
        <class>src.Message</class>
        <properties>
          <!--<property name="openjpa.DynamicEnhancementAgent" value="true"/>-->
          <property name="openjpa.ConnectionURL" value="jdbc:hsqldb:hsql://localhost/xdb"/>
          <property name="openjpa.ConnectionDriverName" value="org.hsqldb.jdbcDriver"/>
          <property name="openjpa.ConnectionUserName" value="sa"/>
          <property name="openjpa.ConnectionPassword" value=""/>
          <property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO"/>
        </properties>
      </persistence-unit>
    </persistence>



    PHP Code:
    <?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="msg" />
            </bean>
                   
            <bean
                class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
           
            <bean id="MessageDao"
                  class="src.MessageDaoImpl">
            </bean>
            
        </beans>


    The Code output:
    PHP Code:
    7-mei-2010 17:14:24 org.springframework.context.support.AbstractApplicationContext prepareRefresh
    INFO
    Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@15f7107display name [org.springframework.context.support.FileSystemXmlApplicationContext@15f7107]; startup date [Fri May 07 17:14:24 CEST 2010]; root of context hierarchy
    7
    -mei-2010 17:14:24 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    INFO
    Loading XML bean definitions from file [C:\Eclipse\workspace\SpringJPA\.\conf\config-ex4.xml]
    7-mei-2010 17:14:24 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
    INFO
    Bean factory for application context [org.springframework.context.support.FileSystemXmlApplicationContext@15f7107]: org.springframework.beans.factory.support.DefaultListableBeanFactory@cf829d
    7
    -mei-2010 17:14:25 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
    INFO
    Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@cf829ddefining beans [entityManagerFactory,org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor#0,MessageDao]; root of factory hierarchy
    7-mei-2010 17:14:25 org.springframework.orm.jpa.LocalEntityManagerFactoryBean createNativeEntityManagerFactory
    INFO
    Building JPA EntityManagerFactory for persistence unit 'msg'
    ok 
    Last edited by Smik; May 7th, 2010, 11:44 AM.

  • #2
    .... I thought already that the issue had something to do with transactions, so I changed this:
    PHP Code:
    package src;

    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;

    import org.springframework.stereotype.Repository;


    public class 
    MessageDaoImpl implements MessageDao {
        @
    PersistenceContext
        
    private EntityManager entityManager;
        
        public 
    void updateTable(Message Message) {
            
    entityManager.persist(Message);
            
    System.out.println("ok");
        }
        

    to this:
    PHP Code:
    package src;

    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;

    import org.springframework.stereotype.Repository;


    public class 
    MessageDaoImpl implements MessageDao {
        @
    PersistenceContext
        
    private EntityManager entityManager;
        
        public 
    void updateTable(Message Message) {
        
    entityManager.getTransaction().begin();
        
    entityManager.persist(Message);
        
    entityManager.getTransaction().commit(); 
            
    System.out.println("ok");
        }
        


    and change the xml configuration file to this:
    (just add a transction manager)
    PHP Code:
    <?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="msg" />
            </bean>
            
            <bean class="org.springframework.orm.jpa.JpaTransactionManager">
                <property name="entityManagerFactory"
                    ref="entityManagerFactory" />
            </bean>
            
            <bean
                class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
           
            <bean id="MessageDao"
                  class="src.MessageDaoImpl">
            </bean>
            
        </beans>
    however, this gave me the error:
    java.lang.IllegalStateException: Not allowed to create transaction on
    shared EntityManager - use Spring transactions or EJB CMT instead
    Last edited by Smik; May 7th, 2010, 07:53 PM.

    Comment


    • #3
      thanks to this site, i finally found the solution:
      http://groups.google.com/group/sprin...b8b60e97?pli=1

      Apparently this is the good class:

      PHP Code:
      package src;

      import javax.persistence.EntityManager;
      import javax.persistence.PersistenceContext;

      import org.springframework.stereotype.Repository;


      public class 
      MessageDaoImpl implements MessageDao {
          private 
      EntityManager entityManager;
          
          private 
      EntityManagerFactory emf
          @
      PersistenceUnit
          
      public void setEntityManagerFactory(EntityManagerFactory emf) {
              
      this.emf emf;
          } 
          
          public 
      void updateTable(Message Message) {
          
      entityManager.getTransaction().begin();
          
      entityManager.persist(Message);
          
      entityManager.getTransaction().commit();
              
      System.out.println("ok");
          }
          


      but now my question is WHY ???????
      Why should I have to add a setter for the setEntityManagerFactory and why not on the original article http://blog.springsource.com/2006/08...ncing-spring// ?????
      Last edited by Smik; May 7th, 2010, 01:22 PM.

      Comment


      • #4
        Get your original class and configure transactions, trying to store/update/delete data from the database is useless without transactions.

        Configure a JpaTransactionManager and add configuration to declare methods transactional. I suggest chapter 9 of the reference guide.


        Why should I have to add a setter for the setEntityManagerFactory and why not on the original article http://blog.springsource.com/2006/08...ncing-spring// ?????
        Because you didn't configure it properly.

        Comment

        Working...
        X