开发者

Null objects after database query (using hibernate + smartgwt)

This is my first post here, hope I'm doing it right. Let me see how can I explain my problem better, so that you guys can understand it.

[Using: Eclipse, Hibernate, Smartgwt, Java language]

Among others, i have 2 tables in my database (postgreSQL), which are related to one another.

  • OrganizerUsers (organizeruser_id [PK], user_id, organizer_id [FK]) - those FKs correlate the table to 2 other unimportant ones.
  • Conferences (conference_id [PK], title, organizeruser_id [FK])

Basically, I need to get all the rows in Conferences table. So I'm expecting that after the query, I should get something like a Set<Conferences>, each object in the set containing the info in the table. Now, I'm using Hibernate and Smartgwt to build this web app (new to them). My mapping files were created automatically in Eclipse. My class files were also created automatically by Hibernate's code generation..thingy. Noticed that hibernate does the following thing: Each object within the Set<Conferences> is an Conferences object, and it has 3 attributes:

  • int conference_id
  • String title
  • an Organizeruser object

That last object is basically the corresponding object taken from the OrganizerUsers table (and was picked according to the relationship between Conferences and Organizerusers tables - a Join operation was made, basically).

THE QUERY:

@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;
    }

The result is sent via a RPC (in smartgwt), to the client (a webpage). This above query is basically part of a kind of servlet.

THE RESULT:

Client receives the Set<Conferences> conferences object. While outputting data in the webpage, i notice:

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**

The confereces.getOrganizeruser() should have returned a valid object, taken from the organizeruser table. There is a corresponding valid row there.

This is just an example, but everywhere I have a relationship with FKs between two tables, i get this problem. Doing an HQL query in that built-in HQL Editor for the same example, seems to give me the right information though o_O [in Hibernate Configurations Tab]. Is it cause of hibernate or.. the SmartGwt RPC call? I doubt it. BY THE WAY, I had a similar problem before, with a null set being outputted after the query. I fixed it by fetching it eagerly rather than lazy (default). But I didn't get this to work.

Additional info: Conferences.hbm.xml mapping file [edited - cut out unimportant stuff, and translated the class names which were written in 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 class [edited - cut out unimportant stuff, and translated the class names which were written in 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;
    }

}


This link helped me to fix my problem. So if there's anyone that has a similar issue, I recommend you start reading there (too bad I didn't find it earlier).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜