我正在设计一个应用程序:Eclipse,Jsf,Hibernate,WildFly,Primefaces。
而且我遇到了“未映射”的休眠问题,是的,我知道我们应该使用表名的实体名称。 我是新来的,不确定如何正确处理。
自动创建的表是aluno_turma,尽我所能,对此表无能为力。
我查看了所有关于“未映射”的问题的答案,并尝试了所有问题,但无法使其发挥作用。
感谢您的帮助。
代码如下:
Aluno.java
@Entity
@ManagedBean
@Table(name = "Aluno")
public class Aluno implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
...
@ManyToMany
@JoinTable(name="aluno_turma",
joinColumns={ @JoinColumn(name="aluno_id")},
inverseJoinColumns={ @JoinColumn(name="turma_id")})
private List<Turma> turmas = new ArrayList<Turma>();
...
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
Turma.java
@Entity
@ManagedBean
public class Turma implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
private String nome;
private Integer diaSemana;
private String hora;
private String observacao;
private Boolean inativo;
@ManyToOne
private Modalidade modalidade;
@ManyToMany
@JoinTable(name="aluno_turma",
joinColumns={ @JoinColumn(name="turma_id")},
inverseJoinColumns={ @JoinColumn(name="aluno_id")})
private List<Aluno> alunos = new ArrayList<Aluno>();
...
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
alunoMBean.java
@ManagedBean(name="alunoBean")
@SessionScoped
public class AlunoMBean implements Serializable {
...
private List<Turma> turmas;
...
public List<Turma> getTurmas() {
if(this.aluno.getNome() == null) {
List<Turma> listTurma = new ArrayList<Turma>();
return listTurma;
}
if(this.turmas == null) { //Só busca na tabela a primeira vez da consulta
this.turmas = alunoDao.getTurmas(aluno, flagTurma);
}
return this.turmas;
}
alunoDao.java
public List<Turma> getTurmas(Aluno aluno, Boolean flag) {
List<Turma> turmas = new ArrayList<Turma>();
turmas = em.createQuery("from Turma where inativo = 0 and id in ( select turma_id from aluno_turma where aluno_id = "+ aluno.getId()+" ) order by nome ", Turma.class).getResultList();
//That doens't work: Not Mapped
//turmas = em.createQuery("from Turma order by nome ", Turma.class).getResultList();
//That works
return turmas;
}
错误
01:43:16,105 ERROR [org.jboss.as.ejb3.invocation] (default task-14) WFLYEJB0034: EJB Invocation failed on component AlunoDao for method public java.util.List br.com.mvtech.alumni.dao.AlunoDao.getTurmas(br.com.mvtech.alumni.modelo.Aluno,java.lang.Boolean): javax.ejb.EJBException: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: aluno_turma is not mapped [from br.com.mvtech.alumni.modelo.Turma where inativo = 0 and id in ( select turma_id from Aluno_turma where aluno_id = 2 ) order by nome ]
at org.jboss.as.ejb3.tx.CMTTxInterceptor.handleExceptionInOurTx(CMTTxInterceptor.java:187)
at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:277)
答案 0 :(得分:0)
您正在创建JPQL查询,但使用的是SQL字段。 JPQL确实使用Object结构来表示语法而不是表结构。
您可以尝试以下操作:
Query query = em.createQuery("FROM Turma WHERE inativo = false and :aluno MEMBER OF alunos order by nome");
query.setParameter("aluno", aluno);
query.getResultList();