Atenica Atenica - 5 days ago 4
MySQL Question

Why I have this error in console "Unknown column 'worklogent0_.workerEntity' in 'field list'?

I am developing some app where I use hibernate, restEasy and mySql. When I wanted to implement one-to-one relationship in my app i got this error:

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


I have two tables in mysql: worker and worklog.
This is my workerEntity class:

@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();
}

}


And this is my worklogEntity class:

@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();
}
}


This is mine Worker.java class:

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;
}

}


This is mine WorkLog class:

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;
}

}


Where am I mistaking?

Answer

You are mixing JPA annotations on methods and on fields, this is not allowed. Since @Id annotation is on method, Hibernate will pick those ones and ignore the annotations on fields, such as @JoinColumn.

Rewrite your annotation on method and it should work.

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;
}

Of course you can decide to move all your JPA annotations on fields instead of methods, and it will work the same.

Comments