我有一个数据库,我需要使用CriteriaBuilder从Java加入。
到目前为止我有这个代码:
CriteriaBuilder cb = entman.getCriteriaBuilder();
CriteriaQuery<Company> query = cb.createQuery(Company.class);
Root<Employee> teacher = query.from(Employee.class);
Join<Employee, Company> employees = teacher.join("id");
query.select(employees).where(cb.equal(teacher.get("name"), ""));
List<Company> results = entman.createQuery(query).getResultList();
return results;
运行此代码后(使用springboot)我收到此错误:无法加入基本类型的属性
有谁知道我该怎么做才能让它发挥作用?
PS:如果需要,我会提供任何其他信息。 提前致谢。 company database employee database
L.E .: 雇员:
@Entity
public class Employee {
@Id
@GeneratedValue
private Long id;
@Column(nullable = false)
@Size(min = 1)
private String name;
@Column(nullable = false)
@Temporal(TemporalType.DATE)
private Date hire_date;
@ManyToOne
//@JoinColumn(name = "id")
private Company company;
public Employee() {}
public Employee(Long id, String name, Date date, Company company) {
setId(id);
setName(name);
setHire_date(date);
setCompany(company);
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getHire_date() {
return hire_date;
}
public void setHire_date(Date hire_date) {
this.hire_date = hire_date;
}
public Company getCompany() {
return company;
}
public void setCompany(Company company) {
this.company = company;
}
}
公司:
@Entity
public class Company {
@Id
@GeneratedValue
private Long id;
@Column(nullable = false)
@Size(min = 1)
private String name;
@OneToMany(mappedBy="company", cascade = CascadeType.ALL, fetch=FetchType.EAGER, orphanRemoval=true)
private Collection<Employee> employees;
public Company() {}
public Company(Long id, String name) {
setId(id);
setName(name);
}
public Company(Long id, String name, Collection<Employee> employees) {
setId(id);
setName(name);
setEmployees(employees);
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Collection<Employee> getEmployees()
{
return employees;
}
public void setEmployees(Collection<Employee> employees) {
this.employees = employees;
}
@Override
public String toString() {
return "Company [id=" + id + ", name=" + name + ", employees=" + employees.toString() + "]";
}
}
答案 0 :(得分:0)
您需要拥有Teacher
元素,而不是对id
的引用,因为您无法加入@Column
字段......
从@Column
更改ID映射:
@Column(name = "id")
private Integer id;
与@ManyToOne
(或所需的)关联:
@ManyToOne
@JoinColumn(name = "id")
private Teacher teacher;
在此之后,join
将按预期工作。