Java RESTWeb服务获取方法可以持久化实体

Java RESTWeb服务获取方法可以持久化实体,java,rest,jpa,service,get,Java,Rest,Jpa,Service,Get,在我尝试使用NetBeans 8.2研究REST web服务时,我在从提供的示例数据库自动创建的实体上创建了一个REST web服务 在测试find(带注释的@GET)方法的URI时,我试图通过在从数据库中检索实体后对其进行更改来混淆或屏蔽我正在检索的实体的一个属性 以下是已更改的查找方法: @GET @Path("{id}") @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public Manufactur

在我尝试使用NetBeans 8.2研究REST web服务时,我在从提供的示例数据库自动创建的实体上创建了一个REST web服务

在测试find(带注释的@GET)方法的URI时,我试图通过在从数据库中检索实体后对其进行更改来混淆或屏蔽我正在检索的实体的一个属性

以下是已更改的查找方法:

@GET
@Path("{id}")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Manufacturer find(@PathParam("id") Integer id) {
    Manufacturer mfg = super.find(id);

    // seems like this line merges the entity in the database. Why?! 
    mfg.setAddressline2("Suite ABC"); 

    return mfg; 
    // return super.find(id); -- this line would prove the obvious that somehow the entity was merged
}
在调用find(id)之前,数据库中Addressline2的值类似于“suite100”

要测试查找,请在浏览器上使用此URL:

其中19985678是表的PK值

输出为:

<manufacturer>
<addressline1>5 81st Street</addressline1>
<addressline2>**Suite ABC**</addressline2>
<city>Mountain View</city>
<email>happysearching@example.com</email>
<fax>408-555-0103</fax>
<manufacturerId>19985678</manufacturerId>
<name>Happy End Searching</name>
<phone>650-555-0102</phone>
<rep>John Snow</rep>
<state>CA</state>
<zip>94043</zip>
</manufacturer>

第81街5号
**ABC套房**
山景
happysearching@example.com
408-555-0103
19985678
寻欢作乐
650-555-0102
约翰斯诺
加利福尼亚州
94043
令我惊讶的是,当我只调用GET方法时,我看到数据库中的值变成了修改后的值-suiteabc

我已经在Oracle数据库、GlassFish和WebLogic服务器上尝试过了。症状是一致的

这是持久性配置,没有添加到普通xml中

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.1" 
 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence 
 http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="REST01PU" transaction-type="JTA">
<jta-data-source>jdbc/sample</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties/>
</persistence-unit>
</persistence>

jdbc/示例
假的
有人能解释一下将实体留在幕后的机制吗

多谢各位


Dan

数据库受到影响,因为当您调用
Manufacturer mfg=super.find(id)时
mfg
对象成为实体管理器管理的对象。这意味着任何直接更改都将反映在数据库中


有多种方法可以断开实体的连接。例如,您可以调用super.detach(mfg)(我假定在super-exists中实体管理器方法访问)

谢谢您,Henrique。我以前从未尝试过更改EntityManager发现的对象,这让我大吃一惊,因为我认为@GET将保证分离。。。我想这是我的幻觉。干杯另外,回到基本情况,一旦找到,实体就处于托管状态,这确保了持久性。完全被忽视了。