延迟加载的@ManyToOne列在调用@ Repository.save()

时间:2018-12-28 18:38:08

标签: java spring hibernate spring-boot spring-data-jpa

在我的Spring Boot项目中,我有以下@ManyToOne关系字段,它引用与父对象相同的@Entity

@Entity
@Table(name="workspace")
public class Workspace extends AbstractEntity {
    @NaturalId
    @Column(nullable=false, unique=true)
    private String code;

    @ManyToOne(fetch=FetchType.LAZY, optional=true)
    @LazyToOne(LazyToOneOption.NO_PROXY)
    @JoinColumn(name="default_workspace", referencedColumnName="code")
    private Workspace parent;

    private String name;
    private String shortName;
}

延迟加载行为正在按预期方式工作。但是,每次我在此@Repository.save()上调用@Entity时,default_workspace字段都会设置为NULL

更新:在注释中包括一些要求的配置

要使用Hibernate启用延迟加载,我只是在hibernate-enhance-maven-plugin中加入了pom.xml插件。

<?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>com.ft</groupId>
  <artifactId>com.ft.admin</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>com.ft</groupId>
      <artifactId>com.ft.common</artifactId>
      <version>${project.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>

      <plugin>
        <groupId>org.hibernate.orm.tooling</groupId>
        <artifactId>hibernate-enhance-maven-plugin</artifactId>
        <version>${hibernate.version}</version>
        <executions>
          <execution>
            <configuration>
              <failOnError>true</failOnError>
              <enableLazyInitialization>true</enableLazyInitialization>
            </configuration>
            <goals>
              <goal>enhance</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

</project>

要在我的Workspace上执行CRUD过程,请使用以下@Repository

@Repository
public interface WorkspaceRepo extends CrudRepository<Workspace, Integer> {
    Optional<Workspace> findByCode(String workspaceCode);
}

默认情况下,在Spring Boot中,事务的边界保留在对@Repository的每次调用中。因此,为了能够访问我的延迟加载字段而不会遇到LazyInitializationException: no Session,我在@Transactional上添加了@Service来扩展事务边界,如下所示。

@Service
@Transactional
public class AppAdminService {
    @Autowired
    private WorkspaceRepo workspaceRepo;

    public void updatePsyWorkspace() {
        Workspace childWorkspace = workspaceRepo.findByCode("PSY").get();
        childWorkspace.setShortName("PSY SHORT");
        workspaceRepo.save(childWorkspace);
    }
}

当我执行WorkspaceRepo.findByCode(code)时,我会得到一个带有有效非空parent的子记录。但是,每当我执行WorkspaceRepo.save(childWorkspace)时,即使我只更新了parentNULL也会被设置为default_workspace,并且NULL列也将在数据库中变为ShortNameString字段,如上例所示。

更新:临时解决方案

作为一个临时解决方案,我必须为default_workspace列添加一个普通的parent字段,并从Hibernate操作中排除@Entity @Table(name="workspace") @Getter @Setter public class Workspace extends AbstractEntity { ... @ManyToOne(fetch=FetchType.LAZY, optional=true) @LazyToOne(LazyToOneOption.NO_PROXY) @JoinColumn( name="default_workspace", referencedColumnName="code", insertable=false, updatable=false) private Workspace parent; @Getter(AccessLevel.NONE) @Setter(AccessLevel.NONE) @Column(name="default_workspace") private String defaultWorkspace; public void setParent(Workspace parent) { this.parent = parent; this.defaultWorkspace = (parent != null) ? parent.getCode() : null; } ... } 字段,如下所示。

AccessLevel.NONE

通过使用parent,我将该字段完全隐藏在外部世界之外。任何用于更新defaultWorkspace字段的调用都会另外更新// declaring a comparator method $scope.filterBy = function(actual, expected) { return _.contains(expected, actual); // uses underscore library contains method }; var employees = [{name: 'a'}, {name: 'b'}, {name: 'c'}, {name: 'd'}]; // filter employees with name matching with either 'a' or 'c' var filteredEmployees = $filter('filter')(employees, {name: ['a','c']}, $scope.filterBy); 字段。即使此解决方案有效,但对我来说还是不合适。

如果您能向我展示实现同一目标的更好方法,我将不胜感激。

0 个答案:

没有答案