1

I found the following links:

But nothing seems to work.

I have 2 entities:

class User {
    Integer userId;
    Profile userProfile;
}

class Profile {
    Integer profileId;
    User user;
}

With XML mapping:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="model.User" table="User" catalog="Proj1" dynamic-update="true">
        <id name="userId" type="java.lang.Integer">
            <column name="userId" />
            <generator class="identity" />
        </id>
        <one-to-one name="userProfile" class="model.Profile">
        </one-to-one>
    </class>
</hibernate-mapping>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Jun 12, 2013 7:51:22 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="model.Profile" table="Profile" catalog="Proj1" dynamic-update="true">
        <id name="profileId" type="java.lang.Integer">
            <column name="profileId" />
            <generator class="identity" />
        </id>
        <many-to-one name="user" class="model.Users" unique="true">
            <column name="userId" />
        </many-to-one>
    </class>
</hibernate-mapping>

The thing here is, the User must have exactly one Profile but the Profile doesn't necessarily to have one User so a Profile may have null User.

Now the problem is every time I fetch a User with its associated Profile, the Profile retrieved is the Profile with the profileId the same as userId, that is if the User has userId 4 the Profile retrieved is the profile with profileId 4 as well even though it is supposed to retrieve Profile with userId 4 not profileId 4.

Update: add Dao code

public User findById( int id ) {
    log.debug("getting User instance with id: " + id);
    try {
        Criteria userCriteria = this.sessionFactory.getCurrentSession().createCriteria(User.class);
        userCriteria.add(Restrictions.idEq(id));

        userCriteria.setFetchMode("userProfile", FetchMode.JOIN);

        userCriteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
        Users instance = (Users) userCriteria.uniqueResult();

        if(instance == null)
            log.debug("get successful, no instance found");
        else
            log.debug("get successful, instance found");
        return instance;
    }
    catch(RuntimeException re) {
        log.error("get failed", re);
        throw re;
    }
}
Community
  • 1
  • 1
Agustinus Verdy
  • 7,267
  • 6
  • 26
  • 28
  • can you share you DAO code? – Juned Ahsan Aug 24 '13 at 00:43
  • I always tend to map `one-to-one` properties as `many-to-one` with a **unique** constraint in DB foreign key. I don't know if that's supposed to solve your problem, but you could try mapping the `userProfile` in your `User` in a similar way you have `user` in your `Profile` entity. – Aritz Aug 24 '13 at 17:40

1 Answers1

0

Finally I found the solution. Originally I had to set the userProfile manually every time I need to fetch the associated userProfile of a User just for a temporary workaround. But I just found this link: http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/associations.html#assoc-bidirectional-121

So basically I just have to add unique="true" not-null="false" to the many-to-one of the user in Profile xml and add property-ref="user" to the one-to-one userProfile in User. I think the key here is the property-ref="user"

Agustinus Verdy
  • 7,267
  • 6
  • 26
  • 28