java - JPA 实体创建
问题描述
我一直在尝试使用Mysql Sample Database的模式创建 JPA 实体,Eclipse JPA 实体的 JPA 工具是使用我为以下场景和表建立实体关系而生成的
员工表:员工表有字段reportsto,它引用员工,即一个员工报告给另一个员工
办公桌:一个办公室可以有多个员工
以下是 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 的正确实体是什么
解决方案
推荐阅读
- postgresql - PostgreSQL 中是否提供类似于 Microsoft SQL Server 的更改跟踪功能?
- angular - 如何使用延迟加载模块的命名空间
- python - 如何将 numpy ndarrays 列表放入熊猫数据框的列中?
- facebook - Facebook 图形 API。无效范围:pages_manage_posts
- c# - 如何截取完整的桌面Windows c#
- abap - 在 SALV 类中使用没有转换出口的输出
- excel - 将范围复制并粘贴到下一个可用列中
- text - 从复制的文本中删除除可见字符之外的所有字符(不可见的控制字符破坏代码)
- blazor-server-side - JSRuntime.InvokeAsync("open", ...) 抛出 TaskCanceledException
- google-cloud-platform - 为什么在数据融合中运行数据管道时没有配置它?