1
首先,抱歉我的英文不好。Hibernate OneToOne將列表添加到列
我是一名Java程序員,但我沒有足夠的背景來使用Hibernate。由於在辦公室我們正在嘗試學習一些新的技術,我正嘗試使用Hibernate來幫助我們處理數據庫問題。
我有兩個班,一個叫Processamento和另一個叫菲拉
表processamento具有在菲拉一個註冊表,由fila.id
Processamento類中定義
@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_
This fila_不應該在那裏,我該怎麼做這個關係OneToOne正確?我只想獲取Processamento列並獲得它的Fila。在Fila,Processamento只有一個結果。
謝謝。
最好的問候。
[解決]
主要錯誤是,我不應該使用JoinTable,JoinTable就像一個工會條款,我想。刪除JoinTable並添加子句JoinColumn解決了我的問題,並且select可以觸發另一個選擇並使關係存在。
很好,固定它,但我偷了與名稱錯誤相關的問題。也許關係設置錯誤或查詢?謝謝 – 2012-04-27 17:09:01