select * from Table_B b, Table_A a where b.table_a_fk = a.id
但是我无法弄明白如何用JPQL做这件事。
我有类似的东西:
@NamedQuery(name=Table_B.findAll, query="SELECT b FROM Table_B b WHERE b.table_a=:table_a")
但where条件为false。
如果有人能给我一个提示,我会非常感激,特别是因为我确信,有一个我找不到的简单解决方案。
好的,我只是称它为表A和B,因为我觉得它会更容易。但这是我的实际表格:
@NamedQueries({
@NamedQuery(name=Task.findAll, query="SELECT ta FROM Task ta WHERE ta.testproject =:testproject"),
})
@Entity
public class Task {
public static final String findAll ="Task.findAll";
@GeneratedValue
@Id
private String name;
private String description;
private Status status;
//private Boolean statusRequested;
private String tool;
private String comment;
private String start;
private String end;
@NotNull
@JoinColumn(name="testproject_fk")
@ManyToOne
private Testproject testproject;
public enum Status{
DONE,IN_PROCESS,NOT_DONE;
}
public Testproject getTestproject() {
return testproject;
}
public void setTestproject(Testproject testproject) {
this.testproject = testproject;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
public String getTool() {
return tool;
}
public void setTool(String tool) {
this.tool = tool;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public String getStart() {
return start;
}
public void setStart(String start) {
this.start = start;
}
public String getEnd() {
return end;
}
public void setEnd(String end) {
this.end = end;
}
}
和
@Entity
public class Testproject {
public static final String findAll ="Testproject.findAll";
@GeneratedValue
@Id
private Long id;
@NotNull()
@Size(min= 2, max = 30 ,message = "Bitte geben Sie einen Namen ein")
private String name;
@NotNull
@Size(min= 5, max = 300 ,message = "Bitte geben Sie eine Beschreibung ein")
private String description;
@NotNull
@Size(min= 5, max = 300 ,message = "Bitte tragen Sie den Namen des Erstellers ein")
private String creator;
@NotNull
@Pattern(regexp = "[0-9]{2}.[0-9]{2}.[0-9]{4}",message = "Bitte geben Sie ein Datum ein")
private String creationDate;
@OneToMany(mappedBy = "testproject")
private List<Task> listTasks;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getCreator() {
return creator;
}
public void setCreator(String creator) {
this.creator = creator;
}
public String getCreationDate() {
return creationDate;
}
public void setCreationDate(String creationDate) {
this.creationDate = creationDate;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public List<Task> getListTasks() {
return listTasks;
}
public void setListTasks(List<Task> listTasks) {
this.listTasks = listTasks;
}
public void add(Task task) {
listTasks.add(task);
task.setTestproject(this);
}
这里我执行查询:
@Override
public List<Task> getAllTasks(Testproject testproject){
TypedQuery<Task> query =
entityManager.createNamedQuery(Task.findAll, Task.class);
query.setParameter("testproject", testproject);
List<Task> tasks = query.getResultList();
return tasks;
}