无法使用Spring Data JPA使用manyToOne获取数据

时间:2017-02-02 19:44:12

标签: java hibernate jpa spring-data many-to-one

我使用的是Spring Data,JPA,无法使用manyToOne关系获取数据。

我有2个实体EmployeeEmployeeTransferRequest

@Entity
@Table(name = "employee_detail")
public class EmployeeDetail{

private Employee employee;

private Long empId;

@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "emp_id", insertable = false, nullable = false,updatable = false)
    public Employee getEmployee() {
       return _student;
    }

    public void setEmployee(Employee employee) {
      this.employee = employee;
    }


    @Column(name = "emp_id", updatable = false)
    public Long getEmpId() {
       return empId;
    }


    public void setEmpId(Long empId) {
       this.empId = empId;
    }

    }
   @Entity
    @Table(name = "employee")
    class Employee
    {
        @Override
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = " emp_id")
        public Long getEmpId() {
            return _id;
            // Other Fields of Employee class ..
        }
    }

public interface EmployeeTransferRequestRepository extends JpaRepository<EmployeeTransferRequest, UUID>, QueryDslPredicateExecutor<EmployeeTransferRequest>
    {
        default EmployeeTransferRequest findOneUUID  transferRequestId) {
           return findOne(employeeTransferRequest.id.eq(transferRequestId);
        }
    }

我没有从EmployeeTransferRequest实体方面映射Employee。它仅uni-directional仅来自EmployeeTransferRequest

当我致电findOne时,我可以从EmployeeTransferRequest获取所有信息,但无法获取Employee信息,获取null

你能帮忙吗?

1 个答案:

答案 0 :(得分:0)

这是因为您已将映射指定为insertable=false。这意味着当您创建转移请求时,该关系不会被保留,在您将来查询请求时会产生null