2012-03-13 73 views
0

我想對OneToOne關係進行自定義連接查詢。我需要添加一個更多的子句,因爲沒有它我得到More than one row with the given identifier was found: [email protected], for class: com.example.AnyContainer是否有可能這樣做?OneToOne自定義連接查詢

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 - >到OneToOneAnyContainer 我試圖用戶@Loader帶註釋的O類命名查詢和ResourceObjectShadow,但沒有使用。此外,我只嘗試在AnyContainer課程上使用它,但完全沒有使用它。

如何處理OResourceObjectShadowOneToOne關係的自定義加入?有沒有辦法做到這一點編程(例如通過自定義tuplizer或類似的東西)?

回答

0

當你想聯接基礎上多列,你可以在HQL

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

使用with運營商現在,如果你想創建這樣的映射,您可以使用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

+0

我試圖添加'@Formula(「..某些hql ..」)'到'O.extension'映射,但它沒有被使用,因爲我想'AnyContainer'仍然只能通過'owner_oid'和'owner_id','ownerType'沒有被使用。 @OneToOne批註可以被刪除,並且通過某種Tuplizer或類似的東西以某種方式通過編程映射這種關係? – viliam 2012-03-13 14:18:39

+0

我找不到任何有關注釋的示例,請參閱[this](http://stackoverflow.com/questions/2334676/hibernate-many-to-one-using-formula)有所幫助。 – ManuPK 2012-03-13 14:48:50