Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.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
Spring JPA@Version导致非null属性引用一个临时值_Spring_Hibernate_Jpa_Jhipster - Fatal编程技术网

Spring JPA@Version导致非null属性引用一个临时值

Spring JPA@Version导致非null属性引用一个临时值,spring,hibernate,jpa,jhipster,Spring,Hibernate,Jpa,Jhipster,我有一个使用JHipster 6.x生成的应用程序。除非为版本列添加@Version,否则我可以成功创建分支。我无法理解乐观锁定和多人关系之间的关系。我也尝试了不同的级联选项,但都没有成功 单面类: @Entity @Table(name = "institute") @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) @org.springframework.data.elasticsearch.annotations.D

我有一个使用JHipster 6.x生成的应用程序。除非为版本列添加@Version,否则我可以成功创建分支。我无法理解乐观锁定和多人关系之间的关系。我也尝试了不同的级联选项,但都没有成功

单面类:

@Entity
@Table(name = "institute")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@org.springframework.data.elasticsearch.annotations.Document(indexName = "institute")
public class Institute extends AbstractAuditingEntity implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@org.springframework.data.elasticsearch.annotations.Field(type = FieldType.Keyword)
private Long id;

@NotNull
@Size(min = 1, max = 15)
@Column(name = "code", length = 15, nullable = false)
private String code;

@NotNull
@Size(min = 3, max = 100)
@Column(name = "name", length = 100, nullable = false)
private String name;

@Version
@Column(name = "version")
private Integer version;

@OneToMany(mappedBy = "institute")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
private Set<Branch> branches = new HashSet<>();
...
控制器代码

@PostMapping("/branches")
public ResponseEntity<BranchDTO> createBranch(@Valid @RequestBody BranchDTO branchDTO) throws URISyntaxException {
    log.debug("REST request to save Branch : {}", branchDTO);
    if (branchDTO.getId() != null) {
        throw new BadRequestAlertException("A new branch cannot already have an ID", ENTITY_NAME, "idexists");
    }
    BranchDTO result = branchService.save(branchDTO);
    return ResponseEntity.created(new URI("/api/branches/" + result.getId()))
        .headers(HeaderUtil.createEntityCreationAlert(applicationName, false, ENTITY_NAME, result.getId().toString()))
        .body(result);
}
映射器(结构映射)


请显示发生异常的代码和语句。为了避免冗长,我跳过了它。问题中添加了代码的其余部分
@PostMapping("/branches")
public ResponseEntity<BranchDTO> createBranch(@Valid @RequestBody BranchDTO branchDTO) throws URISyntaxException {
    log.debug("REST request to save Branch : {}", branchDTO);
    if (branchDTO.getId() != null) {
        throw new BadRequestAlertException("A new branch cannot already have an ID", ENTITY_NAME, "idexists");
    }
    BranchDTO result = branchService.save(branchDTO);
    return ResponseEntity.created(new URI("/api/branches/" + result.getId()))
        .headers(HeaderUtil.createEntityCreationAlert(applicationName, false, ENTITY_NAME, result.getId().toString()))
        .body(result);
}
public BranchDTO save(BranchDTO branchDTO) {
    log.debug("Request to save Branch : {}", branchDTO);
    Branch branch = branchMapper.toEntity(branchDTO);
    branch = branchRepository.save(branch);
    BranchDTO result = branchMapper.toDto(branch);
    branchSearchRepository.save(branch);
    return result;
}
@Mapper(componentModel = "spring", uses = {InstituteMapper.class})
public interface BranchMapper extends EntityMapper<BranchDTO, Branch> {

@Mapping(source = "institute.id", target = "instituteId")
@Mapping(source = "institute.name", target = "instituteName")
BranchDTO toDto(Branch branch);

@Mapping(source = "instituteId", target = "institute")
@Mapping(target = "classGroups", ignore = true)
@Mapping(target = "removeClassGroup", ignore = true)
Branch toEntity(BranchDTO branchDTO);

default Branch fromId(Long id) {
    if (id == null) {
        return null;
    }
    Branch branch = new Branch();
    branch.setId(id);
    return branch;
}
@SuppressWarnings("unused")
@Repository
public interface BranchRepository extends JpaRepository<Branch, Long>, JpaSpecificationExecutor<Branch> {

}
org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation : c.v.e.domain.Branch.institute -> c.v.e.domain.Institute; nested exception is java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation : c.v.e.domain.Branch.institute -> c.v.e.domain.Institute