当前位置: 首页 > 知识库问答 >
问题:

为什么我的实体在JPA中保持独立?

史商震
2023-03-14

我正在使用Spring、JPA和Hibernate。这是一个玩具示例,我正在使用它来调试一个更大的与JPA相关的问题。但是,这个试图持久化一个非常基本的实体的简单示例hellobean似乎创建了分离的对象。据我所知,在EntityManager对象上调用persist应该将实体设置为由持久性上下文管理。但是,它不是,而且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));
    }
}
@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);
    }
}
@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;
    }
}
<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>
<?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>

共有1个答案

乐宜民
2023-03-14

虽然helloservice是事务性的,但一旦服务的方法返回,事务就会提交。这就是为什么实体不再在hello#main中管理的原因。

尝试将output语句添加到helloservice的方法中,您将看到实体附加在这些方法中。

编辑:服务层通常需要事务性。很少建议在一个事务中做所有的事情。注释main方法不是一个好主意,因为只有在main方法返回之后才会刷新持久性上下文--即在应用程序终止之后。

 类似资料:
  • 问题内容: 总是有很多与独立实体有关的问题! 首先,它们经常导致hibernate。是的,还有另一个持久性提供程序,它们不抛出异常,但是我认为它们在一致性方面存在一些问题。考虑我们有和实体,从到那里的引用()必须为非null。 我们开始了会话,加载了实例,然后关闭了会话。之后,我们尝试获取对的引用。并假设另一笔交易只是删除了我们和实例。因此,当我们从数据库查询时,我们找不到合适的实例并获取! 因此

  • null 我能找到的使用JPA/Hibernate的唯一有效解决方案是执行以下步骤。这是因为hibernate将实体保留在第一级缓存中,直到事务结束。 创建新实体 强制快速刷新() 分离实体 这样做的开销是 null 使用JDBC或JdbcTemplate,但您必须为实体编写自己的插入 所以问题是:是否存在同时坚持和分离的可能性,或者甚至更好地坚持而不成为被管理的?

  • 问题内容: 我试图保留与已经保留的其他对象具有多对多关系的对象。 这是我的持久化对象(它们已经持久化在数据库中,这是一个MySql): Product 这是我尝试创建的没有持久性的对象 这是我的class,它接收产品名称数组,使用名称查找产品并将其放入保留对象。 这是我的ProductDAO界面: 这是我的spring配置文件: 这是完整的堆栈跟踪: 问题答案: 我有同样的问题,并通过删除来解决。

  • 问题内容: 我将ReactJs与Redux一起使用,在一些教程和代码上,我看到人们建议并使用normalizr 保持状态平坦 。但是,保持平坦的真正优势是什么?如果没有,我会遇到任何问题吗?有必要吗 ? 问题答案: 三个主要原因: 不变地更新嵌套的Javascript对象通常会导致难以维护的丑陋代码,除非您使用实用程序库来打包过程 不变地更新嵌套数据要求您返回嵌套层次结构中所有项目的新副本。由于组

  • 我有一个将Apache Ignite用作单节点集群的应用程序。也就是说,Ignite由应用程序启动和停止,其生命周期与应用程序匹配。 Ignite缓存同时启用了持久存储和读通。所以 首先调用cache.get 所有这一切似乎都运转得很好。这是我的问题:有时(经常)当应用程序被跳转或重新部署时,持久存储区数据目录就Apache Ignite而言仍然保持锁定状态。因此,Ignite会无声地创建一个新的

  • 我的问题可能看起来很傻。我是JPA的新手,试图理解它的基本概念。我发现有一种@multi-to-one实体关系可以在那里使用。我的问题是,为什么有人想在拥有“一对多”关系的同时使用它?我的意思是,拥有后一个就足够了解关系并发送查询了,对吗?如果没有,请解释。也许我对这两种关系的看法是完全错误的。请提供一个场景作为示例,以便我更好地理解。谢谢