OneToOne自定义连接查询

时间:2012-03-13 00:13:27

标签: java hibernate join one-to-one

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

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

@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

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

修改:已更新ManyToOne - >到OneToOne的{​​{1}} 我尝试使用在AnyContainer类和@Loader上注释的命名查询来O,但未使用它。另外,我只尝试在ResourceObjectShadow类上使用它,但根本没有使用它。

如何处理AnyContainerOneToOne中该O关系的自定义加入?有没有办法以编程方式(例如通过自定义tuplizer或类似的东西)?

1 个答案:

答案 0 :(得分:0)

如果您想基于多个列加入,可以使用 HQL

中的with运算符
from AnyBean as a join a.b with b.type = 0

现在,如果您想创建这样的映射,可以使用formula标记。

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

此映射将始终基于两列连接表。

阅读示例here或API文档here

相关问题