2

I got two tables. Table_A and Table_B.
Table_B has the foreignkey table_a_fk. What I'm trying to do is the following: "Give me everything from Table_B where the id(PK) of Table_A is like table_a_fk of Table_B " In MYSQL it's not that hard. I wrote:

       select * from Table_B b, Table_A a where b.table_a_fk = a.id

but I cannot figure out how to do that wit JPQL.

I got something like:

    @NamedQuery(name=Table_B.findAll, query="SELECT b FROM Table_B b WHERE b.table_a=:table_a") 

but the where condition is false.

I would be very thankful if someone can give me a hint, especially because I'm sure, that there is a simple solution I cannot find.

Ok, i just called it Table A and B because I thought it would be easier. But here are my actual tables:

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

}

and

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

}

here I execute th query:

        @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;
}
Melle
  • 23
  • 5
  • Post the code of the entities, and the code where you execute that query. – JB Nizet Jan 05 '16 at 10:35
  • Your query should work fine. Make sure testproject has the correct ID. Turn on SQL logging and check which SQL query is generated. Also, why don't you simply use `testproject.getTasks()` to get the tesks of the project? – JB Nizet Jan 05 '16 at 11:58

0 Answers0