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

Time:10-09

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 protected], photo=null, username=userr, hireDate=null, teams=null, teamsId=null)

2022-10-07 12:41:49.420 ERROR 11388 --- [nio-8081-exec-2] o.a.c.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.employeeemployeeIdBackref] 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?

enter image description here

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

CodePudding user response:

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;
  • Related