[英]Entity partially saved when using JOINED inheritance strategy and setting spring.jpa.properties.hibernate.jdbc.batch_size
我有一个使用Spring Data的Java Spring Boot应用程序,一个MySql数据库以及两个我想使用JOINED继承策略保留在数据库中的小类。
这是一些代码:
pom文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>oli</groupId>
<artifactId>BugInheritanceBatch</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>BugInheritanceBatch</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
App类:
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
BaseTable实体:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class BaseTable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private long value;
// getters and setters ...
}
SubTable实体:
@Entity
public class SubTable extends BaseTable {
private String name;
// getters and setters ...
}
DAO:
public interface BaseTableDao extends CrudRepository<BaseTable, Long> {
List<BaseTable> findAll();
}
使用此测试,一切正常:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)
public class BaseTableDaoTest {
@Autowired
private BaseTableDao dao;
@Test
public void test() {
SubTable item = new SubTable();
item.setName("name");
item.setValue(33);
item = dao.save(item);
// make sure our inserted item received an ID
Long id = item.getId();
Assert.assertNotNull(id);
// make sure we have 1 item in the table
List<BaseTable> items = dao.findAll();
Assert.assertEquals(1, items.size());
// make sure the item's type is correct and contains the expected values
Assert.assertTrue(items.get(0) instanceof SubTable);
SubTable insertedItem = (SubTable) items.get(0);
Assert.assertEquals((long) id, (long) insertedItem.getId());
Assert.assertEquals("name", insertedItem.getName());
Assert.assertEquals(33, insertedItem.getValue());
}
}
但是,如果我在application.properties中添加以下行:
spring.jpa.properties.hibernate.jdbc.batch_size=20
然后测试失败并显示
JpaSystemException: Cannot instantiate abstract class or interface: BaseTable...
在进行了一些调查并检查了数据库本身之后,我看到dao.findAll()语句引发了此异常,因为dao.save()部分保存了实体(仅更新了base_table,而未更新sub_table)。 此外,我得到了一个奇怪的日志形式休眠:
HHH000010: On release of batch it still contained JDBC statements
但是Hibernate记录的INSERT语句是正确的。
您有解决的办法吗?
谢谢。
问题已解决。 我只需要使用最新版本的Hibernate。
我更新了pom.xml如下:
<properties>
...
<hibernate.version>5.4.4.Final</hibernate.version>
</properties>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.