加入CriteriaQuery?

时间:2014-05-26 12:08:03

标签: java jpa criteria-api vaadin7

我尝试使用CriteriaQuery进行JOIN来获取数据库的信息,但返回一个关于无法解析属性的异常并且不起作用。我正在寻找解决方案,但仍然没有找到。 我的代码如下。

@Entity
@Table(name="cargo")
public class Cargo implements Serializable{
    private static final long serialVersionUID = 1L;

    @Id @GeneratedValue
    private Integer idCargo;    

    @NotNull @NotEmpty @Column(unique=true) 
    private String cargo;
}


@Entity
@Table(name="curriculum")
public class Curriculum implements Serializable{
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    private Long idCurriculum;

    @Transient @OneToOne @JoinColumn(name="idCurriculum")
    private Curriculum curriculum;

    @Temporal(TemporalType.DATE)
    private Date dataCad;



    @Size(min=5, max=50)
    @NotNull
    @NotEmpty   
    private String nome;

    @NotEmpty
    private String sexo;

    @Email
    @NotEmpty
    @NotNull
    @Size(max=250)
    @Column(unique=true)
    private String email;

    @NotNull
    @NotEmpty
    @Size(min=14, max=14)
    @Column(unique=true)
    private String cpf;

    @NotEmpty
    @NotNull
    @Size(min=8, max=8)
    private String senha;

    @OneToOne//(cascade = CascadeType.ALL)
    @NotNull
    @JoinColumn(name="idCargo")
    private Cargo cargo;

    private String ativado = "N";
}


@Entity
@Table(name="curriculum2")
public class Curriculum2 implements Serializable{
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    private Long idC2; //NOT NULL AUTO_INCREMENT,

    @NotNull @OneToOne(cascade=CascadeType.ALL,orphanRemoval=true) 
    @JoinColumn(name="idCurriculum")    
    private Curriculum curriculum; //NOT NULL,

    @NotNull @Temporal(TemporalType.DATE)
    private Date dataNasceu; //NOT NULL,

    @NotNull @NotEmpty @Size(min=5, max=50)
    private String cidade; //` varchar(50) NOT NULL,

    @NotNull @NotEmpty @Size(min=5, max=50)
    private String endereco; //` varchar(50) NOT NULL,

    @NotNull 
    private int numero; //` int(11) NOT NULL,

    @NotNull @NotEmpty @Size(min=5, max=50)
    private String bairro; //` varchar(50) NOT NULL,


    private String complemento;

    //14.790-000
    private String cep;

    private String estado;


    private String fone; //varchar(15)


    private String celular;


    private String pai;

    @NotNull @NotEmpty @Size(min=5, max=50)
    private String mae;//NOT NULL,

    @NotNull @NotEmpty @Size(min=5, max=50)
    private String nacionalidade; // NOT NULL,

    @NotNull @NotEmpty @Size(min=5, max=50)
    private String naturalidade; //NOT NULL,

    @NotNull @NotEmpty @Size(min=5, max=50)
    private String estcivil; //NOT NULL,


    private String rg;

    @NotNull @NotEmpty @Size(min=5, max=20)
    private String cartprof; //NOT NULL,

    @NotNull @NotEmpty @Size(min=2, max=20)
    private String serie; //NOT NULL,


    private String reservista; 
    private String titeleitor;
    private String zona;
    private String carthabilita;
    private String categoria;
    private BigDecimal ultimosalario;

    @NotNull @NotEmpty @Size(min=5, max=50)
    private String salariopretendido; //not null

    private String observacoes;    
}


@Entity
@Table(name="escolaridade")
public class Escolaridade implements Serializable{
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    private Integer idEscola;

    @NotNull @NotEmpty @Size(min=5,max=50)
    private String escola;

    @NotNull @NotEmpty @Size(min=5,max=50)
    private String cidade;

    @NotNull
    private String estado;

    @NotNull @Temporal(TemporalType.DATE)
    private Date inicio;

    @Temporal(TemporalType.DATE)
    private Date conclusao;

    @NotNull 
    private String ensino;

    @NotNull @OneToOne @JoinColumn(name="idCurriculum")
    private Curriculum curriculum;
}


@Entity
@Table(name="experiencia")
public class Experiencia implements Serializable{
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    private Integer idExperiencia;  
    @NotNull @NotEmpty @Size(min=5,max=50)
    private String empresa; 
    @NotNull @NotEmpty @Size(min=5,max=50)
    private String endereco;    
    @NotNull @NotEmpty @Size(min=5,max=50)
    private String cidade;  
    @NotNull @NotEmpty @Size(min=5,max=50)
    private String bairro;  
    @NotNull @NotEmpty
    private String estado;      
    @NotNull @Temporal(TemporalType.DATE) 
    private Date entrada;   
    @Temporal(TemporalType.DATE)
    private Date saida; 
    @NotNull @NotEmpty @Size(min=5,max=50)
    private String cargo;   
    private String funcoesDesemp;   
    private String fone;
    private String nomeEncarregado; 
    @NotNull @OneToOne @JoinColumn(name="idCurriculum")
    private Curriculum curriculum;
}

@Entity
@Table(name="aperfeicoamento")
public class Aperfeicoamento implements Serializable{
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    private Integer idAperfeicoamento;

    @NotNull @NotEmpty @Size(min=5, max=50)
    private String aperfeicoamento;

    @NotNull @NotEmpty @Size(min=5, max=50)
    private String entidadeensino;

    @NotNull @NotEmpty @Size(min=5, max=50)
    private String cidade;

    @NotNull 
    private String estado;

    @NotNull @Temporal(TemporalType.DATE)
    private Date inicio;

    @Temporal(TemporalType.DATE)
    private Date conclusao;

    @OneToOne 
    @JoinColumn(name="idCurriculum")    
    private Curriculum curriculum;
}


//return List<Curriculum>
public List<Curriculum> getCurriculumReport(Long id){
        List<Curriculum> lista = new ArrayList<Curriculum>();

        EntityManager em = datasource.getEntityProvider().getEntityManager();       
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Curriculum> c = cb.createQuery(Curriculum.class);        
        Root<Curriculum> root = c.from(Curriculum.class);
        Join<Curriculum, Cargo> joinCCargo = root.join("curriculum");
        Join<Curriculum, Curriculum2> joinCC2 = root.join("curriculum");
        Join<Curriculum, Escolaridade> joinCE = root.join("curriculum");
        Join<Curriculum, Aperfeicoamento> joinCA = root.join("curriculum");
        Join<Curriculum, Experiencia> joinCExp = root.join("curriculum");

        c.where(cb.equal(joinCCargo.get("idCurriculum"), cb.parameter(Long.class, "id")));
        TypedQuery q = em.createQuery(c);
        q.setParameter("id", id);

        lista = q.getResultList();

        return lista;
    }


//Exception returns
Caused by: java.lang.IllegalArgumentException: Unable to resolve attribute [curriculum] against path
    at org.hibernate.ejb.criteria.path.AbstractPathImpl.unknownAttribute(AbstractPathImpl.java:120)
    at org.hibernate.ejb.criteria.path.AbstractPathImpl.locateAttribute(AbstractPathImpl.java:229)
    at org.hibernate.ejb.criteria.path.AbstractFromImpl.join(AbstractFromImpl.java:411)
    at org.hibernate.ejb.criteria.path.AbstractFromImpl.join(AbstractFromImpl.java:397)
    at br.ind.ibg.dao.RelatorioDAO.getCurriculumReport(RelatorioDAO.java:33)
    at br.ind.ibg.reports.Report.<init>(Report.java:23)
    at br.ind.ibg.views.ViewCurriculum.buttonClick(ViewCurriculum.java:413)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:508)
    ... 37 more

任何想法??

1 个答案:

答案 0 :(得分:0)

根据您的实体类,这没有意义。

    Join<Curriculum, Cargo> joinCCargo = root.join("curriculum");

root的类型为Curriculum,确实具有curriculum属性,但该属性的类型不是Cargo,而是Curriculum }。

这看起来像你想要的。

    Join<Curriculum, Cargo> joinCCargo = root.join("cargo");

- 编辑 - 等等其他协会。如果您需要加入未映射的实体,那么您的最佳解决方案是映射实体。将Experiencia个对象的集合映射到您的Curriculum类,其属性名称为&#39; experienciaList&#39;然后通过执行

将其加入您的查询中
   root.join("experienciaList");

Hibernate将知道如何加入表,因为你的映射将定义它。

此外,当您想要使用它们来创建对查询的限制时,您实际上只需要创建这些命名的Join对象。如果你只想告诉Hibernate获取关联,就这样做。

   root.join("cargo").join("foo").join("bar");