首页 > 解决方案 > JPA 实体创建

问题描述

我一直在尝试使用Mysql Sample Database的模式创建 JPA 实体,Eclipse JPA 实体的 JPA 工具是使用我为以下场景和表建立实体关系而生成的

  1. 员工表:员工表有字段reportsto,它引用员工,即一个员工报告给另一个员工

  2. 办公桌:一个办公室可以有多个员工

以下是 JPA 实体:

@Entity
@Table(name="employees")
@NamedQuery(name="Employee.findAll", query="SELECT e FROM Employee e")
public class Employee implements Serializable {
    private static final long serialVersionUID = 1L;

    private String email;

    private String extension;

    private String firstName;

    private String jobTitle;

    private String lastName;

    //bi-directional one-to-one association to Customer
    @OneToOne(mappedBy="employee")
    private Customer customer;

    //bi-directional many-to-one association to Office
    @ManyToOne
    @JoinColumn(name="officeCode", referencedColumnName="officeCode")
    private Office office;

    //bi-directional many-to-one association to Employee
    @ManyToOne
    @JoinColumn(name="reportsTo", referencedColumnName="employeeNumber")
    private Employee employee;

    //bi-directional many-to-one association to Employee
    @OneToMany(mappedBy="employee")
    private List<Employee> employees;

}

表格说明

CREATE TABLE `employees` (
  `employeeNumber` int(11) NOT NULL,
  `lastName` varchar(50) NOT NULL,
  `firstName` varchar(50) NOT NULL,
  `extension` varchar(10) NOT NULL,
  `email` varchar(100) NOT NULL,
  `officeCode` varchar(10) NOT NULL,
  `reportsTo` int(11) DEFAULT NULL,
  `jobTitle` varchar(50) NOT NULL,
  PRIMARY KEY (`employeeNumber`),
  KEY `reportsTo` (`reportsTo`),
  KEY `officeCode` (`officeCode`),
  CONSTRAINT `employees_ibfk_1` FOREIGN KEY (`reportsTo`) REFERENCES `employees` (`employeeNumber`),
  CONSTRAINT `employees_ibfk_2` FOREIGN KEY (`officeCode`) REFERENCES `offices` (`officeCode`)
)

当应用程序加载时,我们收到以下错误消息

referencedColumnNames(employeeNumber) of com.train.model.Employee.employee referencing com.train.model.Employee not mapped to a single property

当我添加字段 employeeNumber 并生成 getter 和 setter 时,我得到以下错误

referencedColumnNames(officeCode) of com.train.model.Employee.office referencing com.train.model.Office not mapped to a single property

Office JPA 实体

@Entity
@Table(name="offices")
@NamedQuery(name="Office.findAll", query="SELECT o FROM Office o")
public class Office implements Serializable {
    private static final long serialVersionUID = 1L;

    private String addressLine1;

    private String addressLine2;

    private String city;

    private String country;

    private String phone;

    private String postalCode;

    private String state;

    private String territory;

    //bi-directional many-to-one association to Employee
    @OneToMany(mappedBy="office")
    private List<Employee> employees;

}

Table Description


CREATE TABLE `offices` (
  `officeCode` varchar(10) NOT NULL,
  `city` varchar(50) NOT NULL,
  `phone` varchar(50) NOT NULL,
  `addressLine1` varchar(50) NOT NULL,
  `addressLine2` varchar(50) DEFAULT NULL,
  `state` varchar(50) DEFAULT NULL,
  `country` varchar(50) NOT NULL,
  `postalCode` varchar(15) NOT NULL,
  `territory` varchar(10) NOT NULL,
  PRIMARY KEY (`officeCode`)
)

为什么员工实体抱怨没有 officeCode 以及 Employee 和 Office 的正确实体是什么

标签: javamysqljpajpa-2.1

解决方案


推荐阅读