为什么我在控制台中出现此错误"未知栏' worklogent0_.workerEntity'在'字段列表'?

时间:2016-11-29 15:13:27

标签: java mysql hibernate one-to-one

我正在开发一些应用程序,我使用hibernate,restEasy和mySql。当我想在我的应用程序中实现一对一的关系时,我收到了这个错误:

Caused by: java.sql.SQLSyntaxErrorException: Unknown column 'worklogent0_.workerEntity' in 'field list'
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:536)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:513)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:115)
    at com.mysql.cj.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:1983)
    at com.mysql.cj.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1826)
    at com.mysql.cj.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1923)
    at org.jboss.jca.adapters.jdbc.CachedPreparedStatement.executeQuery(CachedPreparedStatement.java:113)
    at org.jboss.jca.adapters.jdbc.WrappedPreparedStatement.executeQuery(WrappedPreparedStatement.java:504)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:70)
    ... 199 more

我在mysql中有两个表:worker和worklog。 这是我的workerEntity类:

@Entity
@Table(name ="worker")
public class WorkerEntity extends Worker implements Serializable {
    public WorkerEntity(String worker_name, String worker_surname, int worker_salary, String worker_task) {
        super(worker_name, worker_surname, worker_salary, worker_task);
        // TODO Auto-generated constructor stub
    }

    private static final long serialVersionUID = 5364531535728847001L;

    @OneToOne(mappedBy="workerEntity")
    private WorklogEntity workLogEntity;

    public WorkerEntity(){
        super();
        }

    public WorklogEntity getWorkLogEntity() {
        return workLogEntity;
    }

    public void setWorkLogEntity(WorklogEntity workLogEntity) {
        this.workLogEntity = workLogEntity;
    }

    @Override
    public int getWorker_salary() {
        // TODO Auto-generated method stub
        return super.getWorker_salary();
    }

    @Override
    public void setWorker_salary(int worker_salary) {
        // TODO Auto-generated method stub
        super.setWorker_salary(worker_salary);
    }

    @Override
    public String getWorker_task() {
        // TODO Auto-generated method stub
        return super.getWorker_task();
    }

    @Override
    public void setWorker_task(String worker_task) {
        // TODO Auto-generated method stub
        super.setWorker_task(worker_task);
    }
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Override
    public int getWorker_id() {
        // TODO Auto-generated method stub
        return super.getWorker_id();
    }

    @Override
    public void setWorker_id(int worker_id) {
        // TODO Auto-generated method stub
        super.setWorker_id(worker_id);
    }

    @Override
    public String getWorker_name() {
        // TODO Auto-generated method stub
        return super.getWorker_name();
    }

    @Override
    public void setWorker_name(String worker_name) {
        // TODO Auto-generated method stub
        super.setWorker_name(worker_name);
    }

    @Override
    public String getWorker_surname() {
        // TODO Auto-generated method stub
        return super.getWorker_surname();
    }

    @Override
    public void setWorker_surname(String worker_surname) {
        // TODO Auto-generated method stub
        super.setWorker_surname(worker_surname);
    }

    @Override
    public int hashCode() {
        // TODO Auto-generated method stub
        return super.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        // TODO Auto-generated method stub
        return super.equals(obj);
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        // TODO Auto-generated method stub
        return super.clone();
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return super.toString();
    }

    @Override
    protected void finalize() throws Throwable {
        // TODO Auto-generated method stub
        super.finalize();
    }

}

这是我的worklogEntity类:

@Entity
@Table(name ="worklog")
public class WorklogEntity extends Worklog implements Serializable {

    private static final long serialVersionUID = 5364531535728847001L;

    @OneToOne
    @JoinColumn(name="worker_id")
    private WorkerEntity workerEntity;

    public WorkerEntity getWorkerEntity() {
        return workerEntity;
    }

    public void setWorkerEntity(WorkerEntity workerEntity) {
        this.workerEntity = workerEntity;
    }

    public WorklogEntity() {
        super();
    }

    public WorklogEntity(Long id, Integer minutes, Date date, String task, String description) {
        super(id, minutes, date, task, description);
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Override
    public Long getId() {
        return super.getId();
    }

    @Temporal(TemporalType.DATE)
    @Override
    public Date getDate() {
        return super.getDate();
    }

    @Override
    public Integer getMinutes() {
        return super.getMinutes();
    }

    @Override
    public String getTask() {
        return super.getTask();
    }

    @Override
    public String getDescription() {
        return super.getDescription();
    }

    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(getId()).append(" ").append(getDate()).append(" ")
            .append(getMinutes()).append(" ").append(getTask()).append(" ")
            .append(getDescription()).append(" ");
        return stringBuilder.toString();
    }
}

这是我的Worker.java类:

public class Worker {
    private int worker_id;
    private String worker_name;
    private String worker_surname;
    private int worker_salary;
    private String worker_task;
    public Worker(String worker_name, String worker_surname, int worker_salary, String worker_task) {
        super();
        this.worker_name = worker_name;
        this.worker_surname = worker_surname;
        this.worker_salary = worker_salary;
        this.worker_task = worker_task;
    }
    public Worker() {
        // TODO Auto-generated constructor stub
    }
    public int getWorker_salary() {
        return worker_salary;
    }
    public void setWorker_salary(int worker_salary) {
        this.worker_salary = worker_salary;
    }
    public String getWorker_task() {
        return worker_task;
    }
    public void setWorker_task(String worker_task) {
        this.worker_task = worker_task;
    }
    public int getWorker_id() {
        return worker_id;
    }
    public void setWorker_id(int worker_id) {
        this.worker_id = worker_id;
    }
    public String getWorker_name() {
        return worker_name;
    }
    public void setWorker_name(String worker_name) {
        this.worker_name = worker_name;
    }
    public String getWorker_surname() {
        return worker_surname;
    }
    public void setWorker_surname(String worker_surname) {
        this.worker_surname = worker_surname;
    }

}

这是我的WorkLog课程:

public class Worklog {

    private Long id;
    private Integer minutes;
    private Date date;
    private String task;
    private String description;

    public Worklog() {
    }

    public Worklog(Long id, Integer minutes, Date date, String task, String description) {
        super();
        this.id = id;
        this.minutes = minutes;
        this.date = date;
        this.task = task;
        this.description = description;
    }

    public Long getId() {
        return id;
    }

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

    public Integer getMinutes() {
        return minutes;
    }

    public void setMinutes(Integer minutes) {
        this.minutes = minutes;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getTask() {
        return task;
    }

    public void setTask(String task) {
        this.task = task;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

}

我错在哪里?

1 个答案:

答案 0 :(得分:1)

您正在混合方法和字段上的JPA注释,这是不允许的。由于@Id annotation是on方法,Hibernate将选择那些并忽略字段上的注释,例如@JoinColumn。

重写方法上的注释,它应该可以工作。

WorkLogEntity:

private WorkerEntity workerEntity;

@OneToOne
@JoinColumn(name="worker_id")
public WorkerEntity getWorkerEntity() {
    return workerEntity;
}

WorkerEntity:

private WorklogEntity workLogEntity;

@OneToOne(mappedBy="workerEntity")
public WorklogEntity getWorkLogEntity() {
    return workLogEntity;
}

当然,您可以决定将所有JPA注释移到字段而不是方法上,它们的工作原理相同。