3

Spring、JPA、および Hibernate を使用しています。これは、より大きな JPA 関連の問題をデバッグするために使用しているおもちゃの例です。ただし、非常に基本的なエンティティを永続化しようとするこの単純な例では、HelloBean切り離されたオブジェクトが作成されているようです。私が知る限りpersistEntityManagerオブジェクトを呼び出すと、エンティティが永続コンテキストによって管理されるように設定する必要があります。ただし、これは機能しませんmerge。出力は次のとおりです。

Starting...
Service found in context as: net.solasistim.hello.HelloService@3b7a687b
Hibernate: insert into HelloBean (message) values (?)
Is bean attached? = false
Hibernate: select hellobean0_.id as id1_0_0_, hellobean0_.message as message2_0_0_ from HelloBean hellobean0_ where hellobean0_.id=?
Is bean attached? = false

簡潔にするためにインポートは省略されています。

Hello.java::

public class Hello {
    public static void main(String[] args) throws Exception {

        ApplicationContext context = 
            new ClassPathXmlApplicationContext("beans.xml");

        System.out.println("Starting...");

        HelloService svc = (HelloService) context.getBean("helloService");
        System.out.println("Service found in context as: " + svc);

        HelloBean bean1 = new HelloBean();
        bean1.setMessage("This is bean 1.");

        HelloBean bean2 = new HelloBean();
        bean2.setMessage("This is bean 2.");

        svc.persist(bean2);

        System.out.println("Is bean attached? = " + svc.isAttached(bean2));

        HelloBean newBean = svc.merge(bean2);
        System.out.println("Is bean attached? = " + svc.isAttached(newBean));
    }
}

HelloService.java::

@Service
@Transactional
public class HelloService {
    @PersistenceContext private EntityManager em;

    public void persist(HelloBean entity) {
        em.persist(entity);
    }

    public HelloBean merge(HelloBean entity) {
        return em.merge(entity);
    }

    public void delete(HelloBean entity) {
        em.remove(entity);
    }

    public boolean isAttached(HelloBean entity) {
        return em.contains(entity);
    }
}

HelloBean.java::

@Entity
public class HelloBean {
    @Id
    @GeneratedValue
    private long id;
    private String message;

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

    public String getMessage() {
        return this.message;
    }
}

pom.xml::

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>net.solasistim.hello</groupId>
  <artifactId>hello</artifactId>
  <version>1</version>
  <packaging>jar</packaging>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>3.2.2.RELEASE</version>
    </dependency> 

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-orm</artifactId>
      <version>3.2.2.RELEASE</version>
    </dependency> 

    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>4.2.0.Final</version>
    </dependency>

    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>4.2.0.Final</version>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.24</version>
    </dependency>
  </dependencies>
</project>

beans.xml::

<?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: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/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-3.1.xsd">

   <context:component-scan base-package="net.solasistim.hello"/>
   <context:annotation-config />

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

   <!-- These two are needed for @Transactional annotations to be processed.
   Nothing will be committed without them. -->
   <tx:annotation-driven/>
   <bean id="transactionManager"
         class="org.springframework.orm.jpa.JpaTransactionManager">
     <property name="entityManagerFactory" ref="entityManagerFactory" />
   </bean>
</beans>

persistence.xml::

<?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 http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
        version="2.0">
    <persistence-unit name="my-persistence-unit">
          <provider>org.hibernate.ejb.HibernatePersistence</provider>
          <properties>
            <property name="hibernate.dialect"
                      value="org.hibernate.dialect.MySQLDialect" />
            <property name="hibernate.connection.driver_class"
                      value="com.mysql.jdbc.Driver" />
            <property name="hibernate.connection.url"
                      value="jdbc:mysql://localhost:3306/jpa_test" />
            <property name="hibernate.connection.username"
                      value="root" />
            <property name="hibernate.connection.password"
                      value="" />

            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="create" />
          </properties>
    </persistence-unit>
</persistence>
4

1 に答える 1

4

あなたHelloServiceはトランザクションですが、サービスのメソッドが戻ると、トランザクションはコミットされます。そのため、エンティティは 内で管理されなくなりましたHello#main

のメソッドに出力ステートメントを追加してみてくださいHelloService。エンティティがそれらのメソッド内にアタッチされていることがわかります。

編集: 通常、サービス層ではトランザクション性が必要です。トランザクションですべてを実行することはお勧めできません。main永続化コンテキストは、メイン メソッドが返された後、つまりアプリケーションが終了した後にのみフラッシュされるため、メソッドに注釈を付けることはお勧めできません。

mainトランザクション サービス メソッド ( のようなもの) では、アタッチされたエンティティ (おそらく で現在行っていること) に対して必要なことは何でも行うことができますHelloService。このメソッドは、EntityManager直接または DAO を介して使用できます。DAO のメソッドにアノテーションを付けて、@Transactional(propagation = Propagation.MANDATORY)(サービスによって開始された) トランザクションが利用可能であることを確認できます。

UI などの他のレイヤーで切り離されたエンティティを操作することが問題になることはめったにありません。切り離されたエンティティがサービス レイヤーの上のレイヤーで変更された場合は、後でマージする必要があります。エンティティが DB で変更された可能性がある場合は、エンティティを再取得/更新する必要があります。

于 2013-04-03T10:22:10.447 に答える