Hibernate OneToOne为列添加前缀

时间:2012-04-27 15:45:09

标签: hibernate

首先,抱歉我的英语不好。

我是一名Java程序员,但我没有足够的背景来使用Hibernate。因为在办公室我们正在尝试学习一些新的技术,我试图使用Hibernate来协助我们处理数据库问题。

我有两个班,一个叫做Processamento,另一个叫做Fila

表格过程在Fila有一个注册表,由fila.id

定义

Processamento class

@Entity
@Table(name="ra_fila_processamento", schema = "processamento")
public class Processamento implements java.io.Serializable {

    private static final long serialVersionUID = 1L;

    private int idProcessamento;
    private int idFila;
    private int idServidor;

    private Fila fila;

    @Column(name="rfp_id")
    public int getIdProcessamento() {
        return idProcessamento;
    }

    public void setIdProcessamento(int idProcessamento) {
        this.idProcessamento = idProcessamento;
    }

    @Id
    @Column(name="rfp_rf_id")
    public int getIdFila() {
        return idFila;
    }

    public void setIdFila(int idFila) {
        this.idFila = idFila;
    }

    @Column(name="rfp_ser_id")
    public int getIdServidor() {
        return idServidor;
    }

    public void setIdServidor(int idServidor) {
        this.idServidor = idServidor;
    }

    @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinTable(name="ra_fila", schema="processamento", joinColumns = {@JoinColumn(name="rf_id", unique = true)})
    public Fila getFila() {
        return fila;
    }

    public void setFila(Fila fila) {
        this.fila = fila;
    }

}

Fila课程

@Entity
@Table(name="ra_fila", schema="processamento")
public class Fila implements java.io.Serializable {

    private static final long serialVersionUID = 1L;

    public Fila() {}

    private int idFila;
    private String arquivoFonte;
    private String status;

    @Id
    @Column(name = "rf_id", unique = true, nullable = false)
    @GeneratedValue
    public int getIdFila() {
        return idFila;
    }

    public void setIdFila(int idFila) {
        this.idFila = idFila;
    }

    @Column(name="rf_arquivo_fonte")
    public String getArquivoFonte() {
        return arquivoFonte;
    }

    public void setArquivoFonte(String arquivoFonte) {
        this.arquivoFonte = arquivoFonte;
    }

    @Column(name="rf_status")
    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

}

问题是,当我运行此代码时:

    Session session = HibernateUtil.getSessionFactory().openSession();

    session.beginTransaction();

    try {

        Query query = session.createQuery("from Processamento");

        @SuppressWarnings("unchecked")
        List<Fila> fila = query.list();

        session.getTransaction().commit();

        if (fila.size() == 0) return false;

    } catch (HibernateException e) {
        e.printStackTrace();
    } finally {
        session.flush();
        session.close();
}

创建的HQL就是这个:

select processame0_.rfp_rf_id as rfp1_3_, processame0_.rfp_id as rfp2_3_, processame0_.rfp_ser_id as rfp3_3_, processame0_1_.fila_rf_id as fila4_2_ from processamento.ra_fila_processamento processame0_ left outer join processamento.ra_fila processame0_1_ on processame0_.rfp_rf_id=processame0_1_.rf_id

这个HQL中的错误就是这个部分:

processame0_1_.fila_rf_id as fila4_2_

fila _ 不应该在那里,我怎么能正确地做这个关系OneToOne?我只想获取Processamento列并获得它的Fila。 Processamento在Fila只有一个结果。

感谢。

最好的关注。

[解决]

主要错误是我不应该使用JoinTable,JoinTable就像一个union子句,我想。删除JoinTable并添加子句JoinColumn解决了我的问题,select会触发另一个select并使关系存在。

1 个答案:

答案 0 :(得分:0)

您为Processamento撰写了查询并提取List<Fila>这不行。

Query query = session.createQuery("from Processamento");
List<Processamento> processamentos = query.list();

for(Processamento processamento: processamentos){
   System.out.println(processamento.getFila().getIdFila();
}