数据库查询后使用空对象(使用hibernate + smartgwt)
这是我在这里的第一篇文章,希望我做对了。让我看看如何更好地解释我的问题,以便你们能理解它。数据库查询后使用空对象(使用hibernate + smartgwt)
[使用:Eclipse中,休眠,SmartGWT的,Java语言]
其中,我在我的数据库(PostgreSQL系统),这是关系到一个另外2台。
- OrganizerUsers(organizeruser_id [PK],USER_ID,organizer_id [FK]) - 那些FKS表格2点其他不重要的相关。
- 会议(conference_id [PK],标题,organizeruser_id [FK])
基本上,我需要在会议表中的所有行。所以我期待在查询之后,我应该得到类似于Set<Conferences>
的东西,集合中的每个对象都包含表中的信息。 现在,我使用Hibernate和Smartgwt来构建这个Web应用程序(对他们来说是新的)。 我的映射文件是在Eclipse中自动创建的。我的类文件也是由Hibernate代码生成自动创建的。 注意到休眠执行以下一点:Set<Conferences>
内的每个对象是会议对象,它有3个属性:
- INT conference_id
- 字符串标题
- 一个Organizeruser对象
最后一个对象基本上是取自OrganizerUsers表中的相应对象或者会议和之间的关系主办单位表 - 基本上进行了联合操作。
查询:
@SuppressWarnings("unchecked")
@Override
public List getConferences() throws IllegalArgumentException {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
List conferences = null;
try{
transaction = session.beginTransaction();
conferences = session.createQuery("from Conferences").list();
transaction.commit();
}catch(Exception e){
transaction.rollback();
e.printStackTrace();
}
finally {
session.close();
}
return conferences;
}
结果通过一个RPC(在SmartGWT的)发送给客户端(网页)。这个上面的查询基本上是一种servlet的一部分。
其结果是:
客户机接收到Set<Conferences>
会议对象。而在网页中输出数据,我注意到:
Set<Conferences> conferences = [the result from the RPC call];
conferences.getTitle() //works fine, returns title
conferences.getConferenceId() //works fine, returns the id
conferences.getOrganizeruser() //**NULL OBJECT**
的confereces.getOrganizeruser()应返回一个有效的对象,从organizeruser表中获取。那里有一个相应的有效行。
这只是一个例子,但无论我在哪里与两个表之间的FK有关系,我都会遇到这个问题。 在该内置HQL编辑器中为相同示例执行HQL查询似乎通过o_O [在Hibernate配置选项卡中]提供了正确的信息。是因为休眠还是.. SmartGwt RPC调用?我对此表示怀疑。 BY THE WAY之前,我有一个类似的问题,在查询后输出空集。我通过热切地取得它而不是懒惰(默认)来修复它。但我没有得到这个工作。
附加信息: Conferences.hbm.xml映射文件[编辑 - 切出不重要的东西,并翻译它写在RO的类名称]
<hibernate-mapping>
<class name="com.test.project.shared.Conferences" table="conferences" schema="public">
<id name="conferenceId" type="int">
<column name="conference_id" />
<generator class="assigned" />
</id>
<many-to-one name="organizerusers" class="com.test.project.shared.Organizerusers" fetch="select">
<column name="organizerusers_id" not-null="true" />
</many-to-one>
<property name="title" type="string">
<column name="title" />
</property>
</class>
</hibernate-mapping>
Conference.java类[编辑 - 剪下不重要的东西,并翻译了RO中写的类名]
@Entity
@Table(name = "conferences", schema = "public")
public class Conferinte extends LightEntity implements java.io.Serializable {
private int conferenceId;
private Organizerusers organizerusers;
private String title;
public Conferences() {
}
public Conferences(int conferenceId, Organizerusers organizerusers{
this.confereceId = conferenceId;
this.organizerusers = organizerusers;
}
public Conferences(int conferenceId, Organizerusers organizerusers, String title){
this.conferenceId = conferenceId;
this.organizerusers = organizerusers;
this.title = title;
}
@Id
@Column(name = "conference_id", unique = true, nullable = false)
public int getConferenceId() {
return this.conferenceId;
}
public void setConferenceId(int conferenceId) {
this.conferenceId = conferenceId;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "organizeruser_id", nullable = false)
public Organizerusers getOrganizerusers() {
return this.organizerusers;
}
public void setOrganizerusers(
Organizerusers organizerusers) {
this.organizerusers = organizerusers;
}
@Column(name = "title")
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
}