OneToOne custom join query

谁说胖子不能爱 提交于 2019-12-13 07:06:45

问题


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

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

public enum RContainerType {   
    O, RESOURCE_OBJECT_SHADOW
}

Edit: 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.

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


回答1:


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

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.

Read an example here or the API Docs here.



来源:https://stackoverflow.com/questions/9676649/onetoone-custom-join-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!