简体   繁体   中英

not-null property references a null or transient value in my Controller

I have a Entity:

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class Employee {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;

private String firstname;
private String lastname;

@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date dateOfBirth;

private String city;
private String address;
private String phone;
private String mobile;
private String email;
private String photo;
private String username;

@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date hireDate;

@OneToMany
@JoinColumn(name = "teamsId", updatable = false, insertable = false)
private List<Teams> teams;
private Integer teamsId;
}

Repository:

@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Integer> {


}

Service:

@Service
public class EmployeeService {
@Autowired
private EmployeeRepository employeeRepository;
public Employee saveEmployeeAndReturnId(Employee employee){
    return employeeRepository.saveAndFlush(employee);
}
}

And in controller

@Autowired
private EmployeeService employeeService;
@PostMapping("/users/addNew")
public RedirectView addNew(Employee employee, RedirectAttributes redirectAttributes){
    System.out.println(employee.toString());

    Employee newEmployee = employeeService.saveEmployeeAndReturnId(employee);

    System.out.println(newEmployee.getId().toString());

result:

Employee(id=null, firstname=user, lastname=useruser, dateOfBirth=Tue Aug 24 00:00:00 CEST 1999, city=A, address=A, phone=A, mobile=add, email=user@gmail.com, photo=null, username=userr, hireDate=null, teams=null, teamsId=null)

2022-10-07 12:41:49.420 ERROR 11388 --- [nio-8081-exec-2] oac.c.C.[.[.[/].[dispatcherServlet]: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value: com.mk.atmosfera.hr.models.Employee._employee_employeeIdBackref; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value: com.mk.atmosfera.hr.models.Employee. employee employeeIdBackref ] with root cause

org.hibernate.PropertyValueException: not-null property references a null or transient value: com.mk.atmosfera.hr.models.Employee._ employee_employeeIdBackref

Anyone know where it comes from and where am I making a mistake?

在此处输入图像描述

I do not create the employee_id field anywhere and it appears, does anyone know where it may come from?

It looks like your entity relationship is wrong:

@OneToMany
@JoinColumn(name = "teamsId", updatable = false, insertable = false)
private List<Teams> teams;
private Integer teamsId;
}

It should be something like:

@ManyToOne
@JoinColumn(name = "teamsId", nullable = true)
private Teams teams;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM