0

我有 2 个表员工和订单:控制台屏幕_执行前 现在我正在尝试映射列表集合以获取每个员工的所有订单。我的映射:

@Entity
@Table(name = "employees", schema = "public")
public class Employees implements java.io.Serializable {

         /**
          *
          */
         private static final long serialVersionUID = 1885071569771542035L;
         private short employeeid;
         //...

         private List<Orders> ordersList;
         @OneToMany(mappedBy = "employees")
         public List<Orders> getOrders() {
                 return ordersList;
         }

         public void setOrders(List<Orders> orders) {
                 this.ordersList = orders;
         }

.

@Entity
@Table(name = "orders", schema = "public")
public class Orders implements java.io.Serializable {

         /**
          *
          */
         private static final long serialVersionUID = 405644896073449066L;

         private short orderid;
         //...

         private Employees employees;

         @ManyToOne
         public Employees getEmployees() {
                 return employees;
         }

         public void setEmployees(Employees employees) {
                 this.employees = employees;
         }

         public Orders() {
         }

         public Orders(short orderid) {
                 this.orderid = orderid;
         }

但不幸的是,我什么也没有。我一直在尝试检测对数据库的 sql 查询,我认为 hibernate 创建了新列:sql result

大概需要参考:

orders0_.employeeid as employeeid1_0_,

orders0_.employees_employeeid as employees15_1_0_,

但我不知道如何强迫他这样做。(我正在使用 postgresql)

4

1 回答 1

0

您没有为用于映射关联的连接列指定任何名称,因此 Hibernate 使用默认名称,如 JPA 规范中所指定:employees_employeeid. 要employeeid改为使用,您必须告诉 Hibernate:它无法猜测:

@ManyToOne
@JoinColumn(name = "employeeid")
public Employees getEmployees() {
    return employees;
}

此外,Employees 的实例是单个员工,因此该类应命名为 Employee,而不是Employees。订单也一样。属性也是order.employees如此,这意味着订单有多个雇员,尽管它只有一个。该属性应命名为employee

@ManyToOne
@JoinColumn(name = "employeeid")
public Employee getEmployee() {
    return employee;
}
于 2013-06-02T16:07:24.900 回答