I’ve read several topics about this as well as the API and I feel like I am on the right track; however, I must still not be understanding enough to get it working, because.. well it doesn’t work!
I have the following 2 tables: TODO and JOBTYPE
TODO table has TDTDKEY (PK) and TDJTKEY(FK?)
JOBTYPE table has JTJTKEY (PK) and JTCODE

As you can see Todo.tdjtkey corresponds to jobtype.jtjtkey and my goal is to retrieve the jtcode rather than the key.
The Entity classes look partially like this:
Todo.java
@Id
@Basic(optional = false)
@Column(name = "TDTDKEY")
private String primaryKey;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "TDJTKEY", insertable = true, updatable = true)
private JobType jobType;
public Todo() {
}
public Todo(String primaryKey) {
this.primaryKey = primaryKey;
}
public String getPrimaryKey() {
return primaryKey;
}
public void setPrimaryKey(String primaryKey) {
this.primaryKey = primaryKey;
}
public JobType getJobType() {
return jobType;
}
public void setJobType(JobType jobType) {
this.jobType = jobType;
}
JobType.java
@Id
@Basic(optional = false)
@Column(name = "JTJTKEY")
private String primaryKey;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "jobType")
private Todo todo;
@Column(name = "JTCODE")
private String jobCode;
public JobType() {
}
public JobType(String primaryKey) {
this.primaryKey = primaryKey;
}
public String getPrimaryKey() {
return primaryKey;
}
public void setPrimaryKey(String primaryKey) {
this.primaryKey = primaryKey;
}
public String getJobCode() {
return jobCode;
}
public void setJobCode(String jobCode) {
this.jobCode = jobCode;
}
So when I do a named query for Todo.findAll it gives me TDTDKEY (i.e. TD00000001) and the associated TDJTKEY CODE (i.e. PICS PGM).
Now, now that I finally got my select working, I am trying to see how I can add/edit.
So for example, if I am on TD00000001 and I want to change PROG to SUPPORT.
If I try it the normal way of sv.setJobType(txtJobType.getText()); it doesn’t work and gives me:
method setJobType in class entity.Todo cannot be applied to given types;
required: entity.JobType
found: java.lang.String
reason: actual argument java.lang.String cannot be converted to entity.JobType by method invocation conversion
So that’s more or less understandable to me, but I am clueless on how I could perform an update to Todo table? Somehow I need it to find SUPPORT in JobType.java and then get the JTJTKEY of it and put it as a new TDJTKEY in the Todo.java?
Any help/hints are appreciated!
Edit: The following code works as per answer! Thanks!
String jpql = "select jobType from JobType jobType where jobType.jobCode = :code";
JobType otherJobType = WWEntityManager.entityManager.createQuery(jpql, JobType.class).setParameter("code", txtJobType.getText()).getSingleResult();
todoEntity.getJobType().removeTodo(todoEntity);
todoEntity.setJobType(otherJobType);
otherJobType.addTodo(todoEntity);
And my entity classes look like this now
Todo.java
@ManyToOne
@JoinColumn(name = "TDJTKEY", insertable = true, updatable = true)
private JobType jobType;
JobType.java
@OneToMany(targetEntity=Todo.class, mappedBy="jobType")
private Collection jobTypes;
Of course I had to add the appropriate methods for remove and add.
First of all, you have 2 TODO rows with the same job key. So, either the data are wrong, or they’re right but your association should then be ManyToOne/OneToMany instead of OneToOne/OneToOne.
Now, regarding your question. If you want to change the code of the job of a todo, the answer is very simple:
If, in fact, you want the TODO to refer to another, existing JobType (supposing the association is a ManyToOne):
Now the question is: how to get the reference to this
otherJobType. If you have its ID, the answer is simple:If you only have its code, you’ll need to execute a query to find it. Assuming the code is unique: