Select * from Table and No Select * from Entityname - JPA JAVA

Asked

Viewed 340 times

2

My query was built like this, but when it does the request:

@Query(value = "SELECT CT.id FROM CursoTurno AS CT 
                LEFT JOIN Curso AS C ON C.id = CT.curso_id 
                INNER JOIN Turno AS T ON T.id = CT.turno_id 
                WHERE C.id = :curso AND T.id = :turno",
                nativeQuery = true)

public CursoTurno findByCursoeTurno(@Param("curso") long curso, @Param("turno") long turno);

Results in that mistake

Caused by: org.postgresql.util.Psqlexception: ERROR: relation "cursoturno" does not exist

Travel Entity

@Entity
@JsonIgnoreProperties({ "cursoTurnos", "unidade" })
@Table(name = "curso")
public class Curso implements Serializable {

    @NotNull
    @Valid
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "curso", orphanRemoval=true)
    private List<CursoTurno> cursoTurnos;

Shift Entity

@Entity
@Table(name = "turno")
@JsonIgnoreProperties({ "horarios","cursoTurnos" })
public class Turno implements Serializable {

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "turno")
    private List<CursoTurno> cursoTurnos;

Entity de Cursoturno

@Entity
@Table(name = "curso_turno")
public class CursoTurno implements Serializable {
  @NotNull
  @ManyToOne
  @JoinColumn(name = "curso_id", referencedColumnName = "id")
  private Curso curso;

  @NotNull
  @ManyToOne(fetch = FetchType.EAGER)
  @JoinColumn(name = "turno_id", referencedColumnName = "id")
  private Turno turno;

He’s taking the name of Entity and not the name of the table. I researched how to fix it there and I found nothing convincing.

  • Are you using Spring Data? (if yes mention the question). nativeQuery = true if your intention is to use JPQL. Also note that in JPQL you do not need to specify the Ids manually in joins, e. g., FROM CursoTurno AS CT LEFT JOIN Curso AS C ON C.id = CT.curso_id flipped FROM CursoTurno ct LEFT JOIN ct.curso c.

  • Finally: The relationship table curso_turno has attributes beyond curso_id and turno_id? Otherwise I would eliminate the entity CursoTurno and would use a relationship @ManyToMany.

  • We resulted in 'SELECT ct FROM Cursoturno ct WHERE ct.curso.id = :idCurso AND ct.turno.id = :idTurno' worked, simple novice knife .... Qqr forma valeuzao ai Anthony by help.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.