OneToOne自定义联接查询 [英] OneToOne custom join query

查看:354
本文介绍了OneToOne自定义联接查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对OneToOne关系进行自定义联接查询.我需要在where部分添加一个子句,因为如果没有它,我会得到More than one row with the given identifier was found: com.example.Container_$$_javassist_0@17156065, for class: com.example.AnyContainer是否可以做到这一点?

I want to have custom join query for OneToOne relationships. I need to add one more clause to where part, because without it I get More than one row with the given identifier was found: com.example.Container_$$_javassist_0@17156065, for class: com.example.AnyContainer Is it possible to do that?

Container.java

Container.java

@Entity
@Table(name = "container")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Container implements Serializable {

    private String oid;
    private Long id;

    @Id
    @GeneratedValue(generator = "ContainerIdGenerator")
    @GenericGenerator(name = "ContainerIdGenerator", strategy = "com.example.ContainerIdGenerator")
    @Column(name = "id")
    public Long getId() {
        return id;
    }

    @Id
    @GeneratedValue(generator = "OidGenerator")
    @GenericGenerator(name = "OidGenerator", strategy = "com.example.OidGenerator")
    @Column(unique = true, nullable = false, updatable = false, length = 36)
    public String getOid() {
        return oid;
    }
    ..other getters/setters
}

O.java:Hibernate将使用select c.ownerType, c.owner_oid, c.owner_id from any c where c.owner_oid=? and c.owner_id=?查询,但是在这里我想使用类似from AnyContainer as c where c.owner = ? and c.ownerType = 0的名称作为联接查询.并为?像往常一样应该有所有者.我在这里添加了另一个条件-> ownerType = 0

O.java: Hibernate will use select c.ownerType, c.owner_oid, c.owner_id from any c where c.owner_oid=? and c.owner_id=? query but here I want to use something like this from AnyContainer as c where c.owner = ? and c.ownerType = 0 for as join query. and for ? there should be owner as usual. I added there one more condition -> ownerType = 0

@Entity
@Table(name = "object")
@ForeignKey(name = "fk_container")
public abstract class O extends Container {

    private AnyContainer extension;

    @OneToOne(optional = true, mappedBy = "owner")
    @ForeignKey(name = "none")
    @Cascade({org.hibernate.annotations.CascadeType.ALL})
    public AnyContainer getExtension() {
        return extension;
    }
    ...other getters/setters
}

ResourceObjectShadow.java:Hibernate将使用select c.ownerType, c.owner_oid, c.owner_id from any c where c.owner_oid=? and c.owner_id=?查询,但在这里我想使用类似from AnyContainer as c where c.owner = ? and c.ownerType = 1的名称作为联接查询.并为?像往常一样应该有所有者.我在这里添加了另一个条件-> ownerType = 1

ResourceObjectShadow.java: Hibernate will use select c.ownerType, c.owner_oid, c.owner_id from any c where c.owner_oid=? and c.owner_id=? query but here I want to use something like this from AnyContainer as c where c.owner = ? and c.ownerType = 1 for as join query. and for ? there should be owner as usual. I added there one more condition -> ownerType = 1

@Entity
@Table(name = "resource_shadow")
@ForeignKey(name = "fk_resource_object_shadow")
public class ResourceObjectShadow extends O {

    private AnyContainer attributes;

    @OneToOne(optional = true, mappedBy = "owner")
    @Cascade({org.hibernate.annotations.CascadeType.ALL})
    public AnyContainer getAttributes() {
        return attributes;
    }
    ...other getters/setters
}

@Entity
@Table(name = "any")
public class AnyContainer implements Serializable {

    private RContainerType ownerType;
    private Container owner;

    @ForeignKey(name = "fk_reference_owner")
    @MapsId("owner")
    @OneToOne(fetch = FetchType.LAZY)
    @PrimaryKeyJoinColumns({
            @PrimaryKeyJoinColumn(name = "owner_oid", referencedColumnName = "ownerOid"),
            @PrimaryKeyJoinColumn(name = "owner_id", referencedColumnName = "id")
    })
    public Container getOwner() {
        return owner;
    }
    ..other getters/setters
}

RContainerType.java

RContainerType.java

public enum RContainerType {   
    O, RESOURCE_OBJECT_SHADOW
}

AnyContainer中将ManyToOne->更新为OneToOne 我尝试使用在类OResourceObjectShadow上标注了命名查询的用户@Loader,但未使用它.另外,我仅尝试作为在AnyContainer类上使用它的测试,但根本没有使用.

Updated ManyToOne -> to OneToOne in AnyContainer I tried to user @Loader with named query annotated on class O and ResourceObjectShadow, but it wasn't used. Also I tried only as test to use it on AnyContainer class, but wasn't used at all.

如何处理OResourceObjectShadow中该OneToOne关系的自定义联接?有没有办法以编程方式(例如通过自定义tuplizer或类似工具)来做到这一点?

How to handle custom join for that OneToOne relationship in O and ResourceObjectShadow? Is there way to do it programmatically (for example through custom tuplizer or something like that)?

推荐答案

如果要基于多个列进行联接,可以在 HQL

When you want join based on multiple columns you can use the with operator in HQL

from AnyBean as a join a.b with b.type = 0

现在,如果要创建这样的映射,则可以使用formula标记.

Now if you want to create a mapping like this, you can use the formula tag.

<one-to-one name="one2oneSubRef"
class="com.manu.hibernate.mappings.domain.RefOnlyAMain" property-ref="parentARef"
             cascade="all" >

<formula>'A'</formula>

<formula>a_id</formula>

</one-to-one>

此映射将始终基于两列联接表.

This mapping will always join the table based on two columns.

此处或API文档中阅读示例此处.

Read an example here or the API Docs here.

这篇关于OneToOne自定义联接查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆