简体   繁体   English

Hibernate查询一对一关系

[英]Hibernate query one-to-one relationship

I am new to hibernate I created the tables and mappings and I am listing all the rows in my table using the following code. 我是休眠的新手,我创建了表和映射,并使用以下代码列出了表中的所有行。

List places = session.createQuery("FROM Place").list();
for (Iterator iterator = places.iterator(); iterator.hasNext();) {
   Place place = (Place) iterator.next();
   System.out.println("-" + place.getName());
}

This code works fine however, the PLACE table has a one-to-one relationship with ADDRESS table, and this code returns null values for all the rows in the ADDRESS table and prints out: 这段代码可以正常工作,但是PLACE表与ADDRESS表具有一对一的关系,并且该代码为ADDRESS表中的所有行返回空值并打印出:

-null
-null
-null
-343
-223
-122

I only want the rows from the PLACES table. 我只想要PLACES表中的行。 How can I manage this? 我该如何处理?

Important Note: After Pierre-Henri Toussaint's answer I noticed the generated sql. 重要说明:在Pierre-Henri Toussaint的回答之后,我注意到生成的sql。 Hibernate first generates a select query from address, then a select query from place.(When I change the constraint value to false it generates a query for each row for the address table) Hibernate首先从地址生成一个选择查询,然后从位置生成一个选择查询(当我将约束值更改为false时,它将为地址表的每一行生成一个查询)

Address.hbm.xml: Address.hbm.xml:

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.places.general.Address" table="ADDRESS" schema="dbo">
        <id name="placeId" type="java.lang.Integer">
            <column name="PLACE_ID" />
            <generator class="foreign">
                <param name="property">place</param>
            </generator>
        </id>
        <one-to-one name="place" class="com.places.general.Place"
                    constrained="true">
        </one-to-one>
        <property column="PLACE_ADDRESS" length="250" name="placeAddress" type="java.lang.String"/>
        <property column="PHONE" length="50" name="phone" type="java.lang.String"/>
    </class>
</hibernate-mapping>

Place.hbm.xml: Place.hbm.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.places.general.Place" table="PLACES" schema="dbo">
        <id column="ID" name="id" type="java.lang.Integer">
            <generator class="identity"/>
        </id>        
        <property column="NAME" length="100" name="name" not-null="true" type="java.lang.String"/>
        <property column="DETAILS" length="200" name="details" type="java.lang.String"/>
    </class>
</hibernate-mapping>

Place.java: Place.java:

import java.io.Serializable;

public class Place implements Serializable{
    private Integer id;
    private String name;
    private String details;

    public Place() {
    }

    public Place(String name, String details) {
        this.name = name;
        this. details = details;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDetails() {
        return details;
    }

    public void setDetails(String details) {
        this. details = details;
    }
}

Make constrained=false. 使constrained = false。

What this means: Constrained=true means the Place can not exist without Address. 这意味着什么:Constrained = true表示没有地址就不能存在该地点。 If you want to get a place by providing place_id, first Address object should exist. 如果要通过提供place_id获得位置,则应该存在第一个Address对象。 The way to do it would be, 这样做的方式是

    Address address = (Address) session.load(Address.class, address_id);
    Place place = address.getPlace();

If you make constrained=false, you can, 如果您使constrained = false,则可以,

   Place place = (Place) session.load(Place.class, place_id);

Same applies to createQuery. 同样适用于createQuery。

Note, if you were using hibernate3, constrained=true should have been working as well.. it doesn't work on hibernate4.. 请注意,如果您使用的是hibernate3,则constrained = true应该也可以正常工作..它不适用于hibernate4。

-Maddy -妈妈

Looks like your mapping needs some changes 看起来您的映射需要一些更改

In address.hbm.xml 在address.hbm.xml中

<one-to-one name="place" class="com.places.general.Place" lazy="false" cascade="all"/>

In your place.hbm.xml 在你的place.hbm.xml

<one-to-one name="address" class="com.places.general.Address" constrained="false"/>

Don't know about the constrained keyword, but it seems that a right outer join query is performed. 不知道受constrained关键字,但似乎执行了正确的外部联接查询。

I would advice to investigate the query by enabling sql output with : 我建议通过启用sql输出来调查查询:

<property name="show_sql">true</property>

From this you can tackle the specific problem. 由此您可以解决特定的问题。

Your Place entity does not have Address entity.If you want to make one-to-one relationship correctly,please check this out. 您的地方实体没有地址实体。如果您想正确建立一对一关系,请检查一下。

Hibernate – One-To-One Example (XML Mapping) Hibernate –一对一示例(XML映射)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM