java-为什么将实体保留在JPA中后,它们仍然保持分离状态?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-为什么将实体保留在JPA中后,它们仍然保持分离状态?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5830字,纯文字阅读大概需要9分钟。
内容图文
我正在使用Spring,JPA和Hibernate.这是我用来调试与JPA相关的较大问题的一个玩具示例.但是,这个简单的示例试图保留一个非常基本的实体HelloBean,似乎创建了分离的对象.据我所知,在EntityManager对象上调用persist应该将实体设置为由持久性上下文管理.但是,它不起作用,并且合并对此也不起作用.输出如下:
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>
解决方法:
尽管您的HelloService是事务性的,但是一旦服务的方法返回,便会提交事务.这就是为什么不再在Hello#main内部管理实体的原因.
尝试将输出语句添加到HelloService的方法中,您将看到这些方法内部已附加了实体.
编辑:通常在服务层需要事务性.很少建议在事务中进行所有操作.注释main方法不是一个好主意,因为仅在main方法返回之后(即在应用程序终止之后),才会刷新持久性上下文.
您可以通过事务服务方法(例如,HelloService中的对象)对连接的实体(您现在可能正在做的事情)进行任何所需的处理.此方法可以直接使用EntityManager或通过DAO使用.您可以使用@Transactional(propagation = Propagation.MANDATORY)注释DAO的方法,以确保事务(由服务启动)可用.
在其他层(如UI)上的分离实体上工作很少有问题.如果一个分离的实体在服务层之上的一个层中发生了变化,那么您之后将需要合并它.如果实体可能已在数据库中更改,则需要刷新/刷新它.
内容总结
以上是互联网集市为您收集整理的java-为什么将实体保留在JPA中后,它们仍然保持分离状态?全部内容,希望文章能够帮你解决java-为什么将实体保留在JPA中后,它们仍然保持分离状态?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。