如何使用JPA连接表和列?

时间:2017-02-10 14:21:56

标签: java hibernate jpa java-ee

我有下一个代码:

当我想将一列加入员工表时,联接成功!这里是代码的工作示例:

Employee.class:

@Entity
@Table (name = "employee")
public class Employee {

public Employee(int id, String name, String lastname, double salary, String spec) {
    this.id = id;
    this.name = name;
    this.salary = salary;
    this.specialization = spec;
    this.lastname = lastname;
}

public int getId() {
    return id;
}

public String getName() {
    return name;
}

public double getSalary() {
    return salary;
}

public void setId(int id) {
    this.id = id;
}

public void setName(String name) {
    this.name = name;
}

public void setSalary(double salary) {
    this.salary = salary;
}

@Override
public String toString() {
    return "Employee{" +
            "id=" + id +
            ", name='" + name + '\'' +
            ", salary=" + salary +
            ", spec='" + specialization + '\'' +
            '}';
}

public String getLastname() {
    return lastname;
}

public void setLastname(String lastname) {
    this.lastname = lastname;
}

@Id
@Column(name = "id")
private int id;

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

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

@Column(name = "salary")
private double salary;

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

public Managers getManagers() {
    return manager;
}

public void setManagers(Managers manager) {
    this.manager = manager;
}

@OneToOne(cascade = {CascadeType.ALL})
@JoinColumn(name = "fk_from_managers_manager_id")
private Managers manager;

public Employee(){}
}

经理班:

@Entity
@Table(name = "managers")
public class Managers {

public Managers(){}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getLastname() {
    return lastname;
}

public void setLastname(String lastname) {
    this.lastname = lastname;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public Managers(String lastname, String name) {
    this.lastname = lastname;
    this.name = name;
}

@Override
public String toString() {
    return "Managers{" +
            "lastname='" + lastname + '\'' +
            ", name='" + name + '\'' +
            ", id=" + id +
            '}';
}

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

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

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "manager_id")
private int id;
}

enter image description here

当我想连接多列的表时,我得到了异常,这里是代码:

Employee.class:

@Entity
@Table (name = "employee")
public class Employee {

public Employee(int id, String name, String lastname, double salary, String spec) {
    this.id = id;
    this.name = name;
    this.salary = salary;
    this.specialization = spec;
    this.lastname = lastname;
}

public int getId() {
    return id;
}

public String getName() {
    return name;
}

public double getSalary() {
    return salary;
}

public void setId(int id) {
    this.id = id;
}

public void setName(String name) {
    this.name = name;
}

public void setSalary(double salary) {
    this.salary = salary;
}

@Override
public String toString() {
    return "Employee{" +
            "id=" + id +
            ", name='" + name + '\'' +
            ", salary=" + salary +
            ", spec='" + specialization + '\'' +
            '}';
}

public String getLastname() {
    return lastname;
}

public void setLastname(String lastname) {
    this.lastname = lastname;
}

@Id
@Column(name = "id")
private int id;

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

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

@Column(name = "salary")
private double salary;

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

public Managers getManagers() {
    return manager;
}

public void setManagers(Managers manager) {
    this.manager = manager;
}

@OneToOne(cascade = {CascadeType.ALL})
@JoinTable(name = "managers" ,joinColumns = {@JoinColumn(name = "fk_from_managers_manager_id"), @JoinColumn(name = "name")})
private Managers manager;

public Employee(){}
}

例外:

Caused by: org.hibernate.AnnotationException: A Foreign key refering employee.Employee from employee.Employee has the wrong number of column. should be 1

无法理解外键的问题,在使用指定列加入表时,场景后面发生了什么?

0 个答案:

没有答案