Note that there are some explanatory texts on larger screens.

plurals
  1. POOneToOne custom join query
    primarykey
    data
    text
    <p>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 <code>More than one row with the given identifier was found: com.example.Container_$$_javassist_0@17156065, for class: com.example.AnyContainer</code> Is it possible to do that?</p> <p>Container.java</p> <pre><code>@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 } </code></pre> <p>O.java: Hibernate will use <code>select c.ownerType, c.owner_oid, c.owner_id from any c where c.owner_oid=? and c.owner_id=?</code> query but here I want to use something like this <code>from AnyContainer as c where c.owner = ? and c.ownerType = 0</code> for as join query. and for ? there should be owner as usual. I added there one more condition -> <code>ownerType = 0</code></p> <pre><code>@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 } </code></pre> <p>ResourceObjectShadow.java: Hibernate will use <code>select c.ownerType, c.owner_oid, c.owner_id from any c where c.owner_oid=? and c.owner_id=?</code> query but here I want to use something like this <code>from AnyContainer as c where c.owner = ? and c.ownerType = 1</code> for as join query. and for ? there should be owner as usual. I added there one more condition -> <code>ownerType = 1</code></p> <pre><code>@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 } </code></pre> <p>RContainerType.java</p> <pre><code>public enum RContainerType { O, RESOURCE_OBJECT_SHADOW } </code></pre> <p>Edit: Updated <code>ManyToOne</code> -> to <code>OneToOne</code> in <code>AnyContainer</code> I tried to user <code>@Loader</code> with named query annotated on class <code>O</code> and <code>ResourceObjectShadow</code>, but it wasn't used. Also I tried only as test to use it on <code>AnyContainer</code> class, but wasn't used at all.</p> <p>How to handle custom join for that <code>OneToOne</code> relationship in <code>O</code> and <code>ResourceObjectShadow</code>? Is there way to do it programmatically (for example through custom tuplizer or something like that)?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload