0

我正在编写系统以允许用户设置通知选项。我创建了两个类,Notification 和 NotificationOption。通知用于指定要发送通知的不同事件以及默认值,通知选项将包含用户选择的首选项。这些是 hbm 文件:

通知.hbm

<hibernate-mapping>
    <class name="com.entities.Notification" table="engine_notification">
        <id name="id">
            <generator class="sequence">
                <param name="sequence">seq_engine_notification_id</param>
            </generator>
        </id>

        <property name="name" not-null="true"/>
        <property name="descriptor" not-null="true"/>
        <property name="defaultValue" column="default_value"/>
    </class>
</hibernate-mapping>

通知选项.hbm

<hibernate-mapping>
    <class name="com.entities.NotificationOption" table="engine_notification_option">
        <composite-id>
           <key-many-to-one name="player"
                     class="com.entities.Profile"
                     foreign-key="id"
                     lazy="false" />
           <key-many-to-one name="notification"
                     class="com.entities.Notification"
                     foreign-key="id"
                     lazy="false" />
       </composite-id>
        <property name="value" not-null="true"/>
    </class>
</hibernate-mapping>

现在我不想每次创建新的 Notification 类型时都在 NotificationOption 表中创建一行,所以我希望能够在 Notification 表上执行查询,在 Notification.id = NotificationOption.notification 上 LEFT JOINs NotificationOption .

使用以下 SQL,我得到了预期的结果:

SELECT * FROM engine_notification n
LEFT JOIN 
  (SELECT o.* FROM engine_notification_option o WHERE o.player = :playerid) nop
ON n.ID = nop.notification
ORDER BY n.ID;

所以我在 hbm 文件中添加了以下内容:

<sql-query name="notificationsForPlayer">
    <return alias="option" class="com.otto.engine.entities.NotificationOption"/>
    <return-join alias="notification" property="option.notification" />
    select
        n.id as {notification.id},
        n.name as {notification.name},
        n.descriptor as {notification.descriptor},
        n.default_Value as {notification.defaultValue},
        nop.player as {option.player},
        nop.value as {option.value}
    from engine_notification n
    left join (select o.* from engine_notification_option o where o.player = :playerID) nop
    on n.id = nop.notification
    order by n.ID
</sql-query>

然而,这给了我以下信息:

java.sql.SQLException: Invalid column name

任何想法如何解决它,或不同的解决方案来实现我想要实现的目标?

谢谢

4

1 回答 1

0

我猜select o.*在加入是问题所在。这种连接并不是真正的标准。

这个选择怎么样:

select
    n.id as {notification.id},
    n.name as {notification.name},
    n.descriptor as {notification.descriptor},
    n.default_Value as {notification.defaultValue},
    nop.player as {option.player},
    nop.value as {option.value}
from engine_notification n
left join engine_notification_option nop
on n.id = nop.notification
where nop.player = :playerID
order by n.ID
于 2012-05-04T16:43:13.050 回答