我想对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