当我运行测试时,我在日志中看到Executing identity-insert immediately为什么hibernate立即运行插入?我希望在提交时插入。
On TransactionImpl creation, JpaCompliance#isJpaTransactionComplianceEnabled == false
begin
Executing identity-insert immediately
/* insert MyJpaClass
*/ insert
into
MyJpaClass
(id, message)
values
(null, ?)
Hibernate:
/* insert MyJpaClass
*/ insert
into
MyJpaClass
(id, message)
values
(null, ?)
Natively generated identity: 1
HHH000387: ResultSet's statement was not registered
committing
Processing flush-time cascades
Dirty checking collections
Flushed: 0 insertions, 0 updates, 0 deletions to 1 objects
Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
Listing entities:
MyJpaClass{id=1, message=text}
Initiating JDBC connection release from afterTransaction
Initiating JDBC connection release from afterTransaction这是我的测试示例Persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<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="HelloWorldPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test;MODE=MySQL;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE"/>
<property name="javax.persistence.jdbc.user" value="sa"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.use_sql_comments" value="true"/>
<property name="hibernate.transaction.factory_class"
value="org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory"/>
<property name="hibernate.id.new_generator_mappings" value="true"/>
<property name="hibernate.connection.autocommit" value="false"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
</properties>
</persistence-unit>
</persistence>测试
import static org.junit.Assert.assertNotNull;
import java.io.FileNotFoundException;
import java.sql.SQLException;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class PersistTest {
Logger LOG = LogManager.getLogger(PersistTest.class);
protected static EntityManagerFactory emf;
protected static EntityManager em;
@BeforeClass
public static void init() throws FileNotFoundException, SQLException {
emf = Persistence.createEntityManagerFactory("HelloWorldPU");
em = emf.createEntityManager();
}
@AfterClass
public static void tearDown(){
em.clear();
em.close();
emf.close();
}
@Test
public void testPersist_success() {
em.getTransaction().begin();
MyJpaClass o = new MyJpaClass();
o.setMessage("text");
em.persist(o);
em.getTransaction().commit();
assertNotNull(o.getId());
LOG.debug(o.getId());
}
}实体
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class MyJpaClass {
@Id
@GeneratedValue (strategy= GenerationType.IDENTITY)
private Long id;
private String message;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}发布于 2018-12-27 05:05:26
因为您使用的是GenerationType.IDENTITY,所以插入是直接执行的,而无需等待提交,因为您需要获取id。为了获得id,您必须在数据库中执行插入操作。
这就是为什么GenerationType.SEQUENCE更高效的原因,插入可以被推迟,并且您可以使用此策略启用批处理。
有关不同策略的更多信息:https://vladmihalcea.com/hibernate-identity-sequence-and-table-sequence-generator/
发布于 2018-12-25 16:07:00
Executing identity-insert immediately中的单词immediately的意思是:
确实需要在方法执行后立即访问标识符(如果不需要,如果我们在事务之外,插入后样式id生成器可能会被推迟)。
当您在事务中时,您需要在方法执行后立即使用标识符。所以立即为你提供它。
当您在isolation level上持久化插入基础时,您可以访问或不访问其他会话中的数据。
https://stackoverflow.com/questions/53919733
复制相似问题