java - Hibernate 一对一 XML 映射

标签 java spring hibernate

我找到了以下链接:

但似乎没有任何效果。

我有 2 个实体:

class User {
    Integer userId;
    Profile userProfile;
}

class Profile {
    Integer profileId;
    User user;
}

使用 XML 映射:

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

这里的问题是,User 必须恰好有一个 Profile,但 Profile 不一定有一个 User 因此,Profile 可能具有 null User

现在的问题是,每次我获取 User 及其关联的 Profile 时,检索到的 Profile 就是 Profile code> 的 profileIduserId 相同,也就是说,如果 UseruserId 4 则 检索到的配置文件也是profileId 4的配置文件,即使它应该检索userId 4而不是Profile profileId 4.

更新:添加 Dao 代码

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

最佳答案

终于找到解决办法了。最初我必须设置 userProfile每次我需要手动获取关联的 userProfile 时的User只是为了临时解决方法。但我刚刚找到了这个链接:http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/associations.html#assoc-bidirectional-121

所以基本上我只需要添加 unique="true" not-null="false"many-to-one user的在Profile xml 并添加 property-ref="user"one-to-one userProfileUser 。我认为这里的关键是 property-ref="user"

关于java - Hibernate 一对一 XML 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18413783/

相关文章:

mysql - 多对多失败, 'on clause' 中的未知列

java - Hibernate <generator class =""> 什么时候最好使用什么?

java - 如果给定的条件为真,则做某事。如果为假,则重试

java - 如何通过Java驱动在MongoDB中创建复合索引?

java - 调用wait()方法后锁被释放?

java - MongoDb Criteria 查询 LTE 不适用于数字

java - 将 Spring 批处理连接到 Spring 集成工作流程

java - maven-metadata.xml 构建错误

java - 即使我声明了映射类,未知实体也会 hibernate

java - 检查用户是否连接到互联网,如果没有则提示用户开启数据服务android