我想對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
- >到OneToOne
在AnyContainer
我試圖用戶@Loader
帶註釋的O
類命名查詢和ResourceObjectShadow
,但沒有使用。此外,我只嘗試在AnyContainer
課程上使用它,但完全沒有使用它。
如何處理O
和ResourceObjectShadow
中OneToOne
關係的自定義加入?有沒有辦法做到這一點編程(例如通過自定義tuplizer或類似的東西)?
我試圖添加'@Formula(「..某些hql ..」)'到'O.extension'映射,但它沒有被使用,因爲我想'AnyContainer'仍然只能通過'owner_oid'和'owner_id','ownerType'沒有被使用。 @OneToOne批註可以被刪除,並且通過某種Tuplizer或類似的東西以某種方式通過編程映射這種關係? – viliam 2012-03-13 14:18:39
我找不到任何有關注釋的示例,請參閱[this](http://stackoverflow.com/questions/2334676/hibernate-many-to-one-using-formula)有所幫助。 – ManuPK 2012-03-13 14:48:50