Spring-hibernate mapping problem

Posted by James on Stack Overflow See other posts from Stack Overflow or by James
Published on 2010-03-24T23:55:57Z Indexed on 2010/03/25 0:03 UTC
Read the original article Hit count: 493

I have a spring-hibernate application which is failing to map an object properly: basically I have 2 domain objects, a Post and a User. The semantics are that every Post has 1 corresponding User.

The Post domain object looks roughly as follows:

class Post {

  private int pId;
  private String attribute;
  ...
  private User user;

  //getters and setters here

}

As you can see, Post contains a reference to User. When I load a Post object, I want to corresponding User object to be loaded (lazily - only when its needed).

My mapping looks as follows:

<class name="com...Post" table="post">
    <id name="pId" column="PostId" />
    <property name="attribute" column="Attribute" type="java.lang.String" />

    <one-to-one name="User" fetch="join"
        class="com...User"></one-to-one>
</class>

And of course I have a basic mapping for User set up.

As far as my table schema is concerned, I have a table called post with a foreign UserId which links to the user table.

I thought this setup should work, BUT when I load a page that forces the lazy loading of the User object, I notice the following Hiberate query being generated:

Select ... from post this_ left outer join user user2_ on this.PostId=user2_.UserId ...

Obviously this is wrong: it should be joining UserId from post with UserId from user, but instead its incorrectly joining PostId from post (its primary key) with UserId from user.

Any ideas? Thanks!

© Stack Overflow or respective owner

Related posts about hibernate

Related posts about hibernate-mapping