Isa*_*ski 5 java hibernate quarkus quarkus-panache
我正在使用 Quarkus 和 PanacheRepository 进行一些测试,但在更新实体数据时遇到了麻烦。更新不起作用,字段值未更新。
简而言之:我创建一个实体并保留数据,然后在另一个请求中我使用从数据库中获取实体repository.findById(id);,更改某些字段值,但新值未保留在数据库中。我试过打电话repository.persist(person); after 但行为是一样的,数据没有更新。
我用 Quarkus 版本 1.9.0.Final, 1.9.0.CR1, 1.8.3.Final 试过这个
我正在使用 postgreSQL 12。我也尝试过使用 mysql 5.7.26
我仅使用 Eclipse 2020-06 (4.16.0) 来编写代码,并在命令行中运行该应用程序,使用: ./mvnw compile quarkus:dev
我创建了一个全新的简单应用程序,行为是相同的。以下是主要配置和一些代码片段:
pom.xml
<properties>
<compiler-plugin.version>3.8.1</compiler-plugin.version>
<maven.compiler.parameters>true</maven.compiler.parameters>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<quarkus-plugin.version>1.9.0.Final</quarkus-plugin.version>
<quarkus.platform.artifact-id>quarkus-universe-bom</quarkus.platform.artifact-id>
<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
<quarkus.platform.version>1.9.0.Final</quarkus.platform.version>
<surefire-plugin.version>3.0.0-M5</surefire-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jsonb</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-orm-panache</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-postgresql</artifactId>
</dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)
应用程序属性:
quarkus.datasource.db-kind = postgresql
quarkus.datasource.username = theusername
quarkus.datasource.password = thepassword
quarkus.datasource.jdbc.url = jdbc:postgresql://localhost:5432/testpanache
# drop and create the database at startup (use `update` to only update the schema)
quarkus.hibernate-orm.database.generation = drop-and-create
Run Code Online (Sandbox Code Playgroud)
实体:
@Entity
public class Person {
@Id @GeneratedValue public Long id;
public String name;
@Override
public String toString() {
return "Person [id=" + id + ", name= '" + name + "']";
}
}
Run Code Online (Sandbox Code Playgroud)
休息资源:
@Path("/people")
@Produces(MediaType.APPLICATION_JSON)
public class PersonResource {
@Inject
PersonRepository repository;
@GET
@Produces(MediaType.TEXT_PLAIN)
public List<Person> hello() {
return repository.listAll();
}
@POST
@Transactional
public void create() {
Person person = new Person();
person.name = "some name";
repository.persist(person);
}
@PUT
@Path("{id}")
@Transactional
public Person update(@PathParam("id") Long id) {
Person person = repository.findById(id);
person.name = "updated updated updated"; // does not work
// repository.persist(person); // does not work
// repository.persistAndFlush(person); // does not work
repository.getEntityManager().merge(person); // does not work
return person;
}
}
Run Code Online (Sandbox Code Playgroud)
存储库:
@ApplicationScoped
public class PersonRepository implements PanacheRepository<Person> {
}
Run Code Online (Sandbox Code Playgroud)
我使用 curl 提出了一些请求来演示行为:
$ curl -w "\n" http://localhost:8080/people
[]
$ curl -X POST http://localhost:8080/people
$ curl -w "\n" http://localhost:8080/people
[{"id":1,"name":"some name"}]
$ curl -X PUT http://localhost:8080/people/1
{"id":1,"name":"updated updated updated"}
$ curl -w "\n" http://localhost:8080/people
[{"id":1,"name":"some name"}]
Run Code Online (Sandbox Code Playgroud)
因此,列表开始为空,第二个 POST 请求创建了一个具有“某个名称”的 Person,如第三个请求所示;第四个请求执行了一个 PUT,旨在将名称更改为“updated updated updated”,但第五个请求显示名称未更新。
虽然它不是必需的,但我尝试了repository.persist(person);, repository.persistAndFlush(person);, 甚至repository.getEntityManager().merge(person);(每次一个),如PersonResource上面的代码片段所示。但它们都没有产生效果。
我缺少什么?
PS.:我尝试将我的实体更改为 extendsPanacheEntity并用于Person.findById(id);查找实体,这样后续更新确实生效。但这不是我的观点,我想使用 PanacheRepository 并想了解我缺少的东西。
请考虑使用 getter/setter 访问您的实体,以便 Hibernate 代理能够正常工作。
@Entity
public class Person {
@Id @GeneratedValue public Long id;
private String name; // <--- private field
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person [id=" + id + ", name= '" + name + "']";
}
}
Run Code Online (Sandbox Code Playgroud)
并在您的资源中:
@PUT
@Path("{id}")
@Transactional
public Person update(@PathParam("id") Long id) {
Person person = repository.findById(id);
person.setName("updated updated updated"); // <--- this way works
repository.persist(person);
return person;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4189 次 |
| 最近记录: |