1

私は使っている

  • 春 3.1.1
  • JPA2
  • H2
  • 休止状態

スタンドアロン Java アプリケーションで。アプリケーションを実行するとトランザクションが実行され、データをクエリするとデータが取得されます。ただし、アプリケーションを閉じるとデータは保存されません。

私を助けてください。

これがスプリング構成です。

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd">

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml" />
    </bean>

    <bean id="sharedEntityManager"
        class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <tx:annotation-driven />

</beans>

Persistence.xml

<?xml version="1.0" encoding="UTF-8"?>

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    version="1.0">
    <persistence-unit name="sling">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>com.sling.data.Trend</class>
        <class>com.sling.data.Gc</class>
        <properties>
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.connection.driver_class" value="org.h2.Driver" />
            <property name="hibernate.connection.url" value="jdbc:h2:sling;DB_CLOSE_DELAY=-1" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
        </properties>
    </persistence-unit>
</persistence>

DAOクラス、

@Repository
public class GcDao {

  @PersistenceContext
  private EntityManager em;

  public GcDao() {
  }

  @Transactional
  public void add(Gc gc){
    em.persist(gc);
  }

  @SuppressWarnings("unchecked")
  @Transactional(readOnly = true)
  public List<Gc> getGc(){
    String queryText = " from Gc";
    Query query = em.createQuery(queryText);
    return query.getResultList();
  }
}
4

1 に答える 1

0

tx:anotation-driven要素の前に、daoパッケージにcontext:component-scanを追加する必要があると思います。

<context:component-scan base-package="com.sling.dao" />
<tx:annotation-driven />
于 2013-01-10T17:17:52.903 に答える