org.springframework.dao.invaliddataaccessresourceusageexception

k0pti3hp  于 2021-07-05  发布在  Java
关注(0)|答案(1)|浏览(505)

嗨,我目前正在使用一个应用程序,然后我在这里遇到了相同的问题springframework.dao.invaliddataaccessresourceusageexception hibernate.sqlgrammareexception mysqlsyntaxerrorexception我遵循了这里的说明,但仍然得到了错误我不知道该怎么做了:
这是我的模型:@entity public class employee implements serializable{

private static final long serialVersionUID = 1L;

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "emp_id")
  private Long empId;

  @Column(name = "first_name")
  private String firstName;

  @Column(name = "middle_name")
  private String middleName;

  @Column(name = "last_name")
  private String lastName;

  @Column(name = "gender")
  private String gender;

  @Column(name = "address")
  private String address;

  @Column(name = "email")
  private String email;

  @Column(name = "birthday")
  private Date birthday;

  @Column(name = "contract")
  private Long contact;

  @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy= "employee")
  @JoinColumn(name = "emp_id")
  private Work work;

  @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "employee")
  @JoinColumn(name = "emp_id")
  private Finance finance;
  //setters and getters
}

工作.java

@Entity
  public class Work implements Serializable{

  private static final long serialVersionUID = 1L;

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "work_id")
  private Long workId;

  @Column(name = "department")
  private String department;

  @Column(name = "position")
  private String position;

  @OneToOne
  @JsonBackReference
  @JsonIgnore
  private Employee employee;
  //setters and getters
}

财务.java

@Entity
  public class Finance implements Serializable {

  private static final long serialVersionUID = 1L;

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "finance_id")
  private Long financeId;

  @Column(name = "assets")
  private int assets;

  @Column(name = "profit")
  private int profit;

  @Column(name = "loss")
  private int loss;

  @Column(name = "revenue")
  private int revenue;

  @Column(name = "cost")
  private int cost;

  @Column(name = "f_date")
  private Date fDate;

  @OneToOne
  @JsonBackReference
  @JsonIgnore
  private Employee employee;

  @JsonBackReference
  @PrimaryKeyJoinColumn
  @Column(name = "cust_id")
  private Long custId;

  @OneToOne
  @JsonIgnore
  private Customer customer;
  //setters and getters
}

以下是存储库:

@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long>{
  long countByGender(String gender);
  List<Employee> findTop5ByOrderByFinance_RevenueDesc();
  List<Employee> findTop5ByOrderByFinance_ProfitDesc();
  @Query(
        value = "SELECT address, COUNT(*) AS address FROM employee GROUP BY address "
                + "ORDER BY address DESC LIMIT 5",
                nativeQuery=true)
  List<Employee> findByAddress();
}

这是错误

java.sql.SQLException: Column 'emp_id' not found.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:965) ~[mysql-connector-java-5.1.45.jar:5.1.45]
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:898) ~[mysql-connector-java-5.1.45.jar:5.1.45]
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:887) ~[mysql-connector-java-5.1.45.jar:5.1.45]
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:861) ~[mysql-connector-java-5.1.45.jar:5.1.45]
at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1080) ~[mysql-connector-java-5.1.45.jar:5.1.45]
at com.mysql.jdbc.ResultSetImpl.getLong(ResultSetImpl.java:2738) ~[mysql-connector-java-5.1.45.jar:5.1.45]
o0lyfsai

o0lyfsai1#

我做了这样的事,结果成功了

@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long>{
  long countByGender(String gender);
  List<Employee> findTop5ByOrderByFinance_RevenueDesc();
  List<Employee> findTop5ByOrderByFinance_ProfitDesc();
  @Query(
    value = "SELECT address, COUNT(*) AS tally FROM employee GROUP BY address "
            + "ORDER BY address DESC LIMIT 5",
            nativeQuery=true)
  List<?> findByAddress();
}

我把雇员改成了“?”,把别名地址改成了tally

相关问题