Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Spring数据在eclipselink中执行更新而不选择_Java_Spring_Eclipselink_Spring Data Jpa - Fatal编程技术网

Java 使用Spring数据在eclipselink中执行更新而不选择

Java 使用Spring数据在eclipselink中执行更新而不选择,java,spring,eclipselink,spring-data-jpa,Java,Spring,Eclipselink,Spring Data Jpa,我使用的是Spring数据(ver1.9)-JPA-EclipseLink(2.6.1)-ApacheTomcat。我想在不检查实体对象是否存在的情况下更新实体对象。 示例实体 @Entity @Table(schema = "customSchema") public class Person implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Colum

我使用的是Spring数据(ver1.9)-JPA-EclipseLink(2.6.1)-ApacheTomcat。我想在不检查实体对象是否存在的情况下更新实体对象。
示例实体

@Entity
@Table(schema = "customSchema")
public class Person implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(nullable = false)
    private Integer id;

    @Column(nullable = false, length = 60)
    private String firstName;

    public Person() {
        this.id = null;
        this.firstName = "";
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
}  
示例Spring数据存储库

@Repository
public interface PersonRepository extends JpaRepository<Person, Integer> {
}
persistence.xml(为线程安全禁用了第2层缓存)


org.eclipse.persistence.jpa.PersistenceProvider
package.to.entity.Person
假的
没有一个
我的问题存在于saveAndFlush命令中。EclipseLink使用SELECT sql命令检查表行中的字段,以确定其中哪些字段已更改。这有助于创建仅包含已编辑字段的更新命令。
删除命令也会发生同样的情况

问题
有没有办法避免执行update(saveAndFlush)或delete(delete)命令的select sql 如果我希望更新表的整行,该怎么办?不检查哪些字段已更改


我见过另一个类似的例子,但这个解决方案不适用于我的例子。我对Person实体使用了@ExistenceChecking(value=ExistenceType.succession\u EXISTENCE)注释,在执行saveAndFlush命令时没有任何更改。

EclipseLink有两种不同的方法

第一个是持久化单元缓存。这是一个共享缓存(L2)或第2层缓存。此缓存是您禁用的缓存

<shared-cache-mode>NONE</shared-cache-mode>

小心点,@Transactional注释在控制器方法上不起作用。您必须为此使用服务。

您使用的缓存选项是什么?如果执行了查找或查询,则应该已将对象读入缓存,因此无需进行第二次选择。您要调用的代码应该对两个调用使用相同的EntityManager,以避免再次读取实体。非常感谢您的评论。我编辑这个问题。我将persistence.xml作为示例源代码。我不使用缓存。我认为如果没有缓存或使用某种扩展的持久性单元,那么find方法中使用的EntityManager与save方法中使用的EntityManager是相同的,那么这将无法工作。它们在相同的事务上下文中吗?我没有使用服务层,并且不能对这两个事务使用相同的EntityManager。我将业务逻辑实现到控制器中。将业务逻辑分离到服务层,我可以对两个存储库调用使用相同的EntityManager。如果我使用EntityManager缓存(第1层),那么这就是我问题的答案。这个问题也帮了我()
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="my_peristenceUnit" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>package.to.entity.Person</class>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <shared-cache-mode>NONE</shared-cache-mode>
  </persistence-unit>
</persistence>
<shared-cache-mode>NONE</shared-cache-mode>
@Service
public class MyEnityService {

    @Transactional
    public Boolean create() {
        int id = 5; // This id is existing in my database.
        Person Person = personRepository.findOne(id);
        // SQL: One SELECT is executed. This is normal AND CACHED FROM YOUR ENTITY MANAGER.

        person.setFirstName("aNewFirstName");

        personRepository.saveAndFlush(person);
        // SQL: ONLY ONE update is executed. NOTHING ELSE.

        return true;
    }
}